Speak["I am not a machine"]

Speak["El nau soul uma machina"]

Mathematica Interface Examples

Cell types

Brackets and Capital Letters

Palettes

Suggestion bar

Mathematica Syntax

[ ... ]  ->  Function Arguments
{ ... }  ->  Lists (vectors, matrices, tensors)
[[ ... ]] -> Elements of a list 
( ... )  ->  standard parenthesis (for algebraic calculations)

Defining variables and functions

x=2;
y=3
x+y^2

f[x_]:=(x^3)+1;
f[z]
f[x]
f[2]

(*  = vs := vs ==  *)

Plot[f[x],{x,-5,5}]

Plot[f[f[x]],{x,-5,5}]

Simple Loops

Arbitrary Precision Arithmetic

(2.^(1/10))
(2^(1/10))

((((2.^(1/10))))^10)

((((2.^(1/(10^10)))))^(10^10))-2
((((2^(1/(10^10)))))^(10^10))-2

a=(2.^(1./10))

π

N[π,500]

{(10^(1/10)),(10.^(1/10))}

{Tan[π/2.],Tan[π/2],Tan[π/4.]+Sin[3.π/2],Tan[π/4]+Sin[3π/2]}

N[Exp[-1000]]
N[Exp[-(10^15)]]
N[Exp[-(10.^16)]]

$MinNumber

Plot[BesselJ[0,1000x],{x,0,10}]

Plot[BesselJ[0,10000x]BesselJ[1,10001x],{x,0,10}]

NIntegrate[BesselJ[0,100.x]BesselJ[1,101.x],{x,0,10}]

NIntegrate[BesselJ[0,100x]BesselJ[1,101x],{x,0,10},WorkingPrecision->20]

NIntegrate[BesselJ[0,100000.x],{x,0,10}]

Integrate[BesselJ[0,100000xx],{xx,0,10}]
N[%]

NIntegrate[BesselJ[0,100000x],{x,0,10},MaxRecursion->20]

k=1.0;
Plot[ArcTan[a/k] + ArcTan[k/a] - Pi/2, {a, 0, 2 Pi},  PlotRange -> {-7 10^-17, 1.8 10^-16}]
k=1;
Plot[ArcTan[a/k] + ArcTan[k/a] - Pi/2, {a, 0, 2 Pi},  PlotRange -> {-7 10^-17, 1.8 10^-16}, WorkingPrecision -> 32]

ff[x_]:=Cos[x]-(1-x^2/2)
Plot[ff[x],{x,-(10^(-8)),(10^(-8))},WorkingPrecision->32]

Plot[ff[x],{x,-(10^(-8)),(10^(-8))}]

Everything is a Function!

Estudo de Funções

f1[x_]:=A Sin[x]
A=3; (* Comentário *) 
Plot[f1[x],{x,0,4π}]

;

f2[x_,y_]:=y Sin[x];
Plot[f2[x,5],{x,0,4π}]

TanTan[x_]:=(x^2)

f3[y_]:=3y[x];
Plot[f3[TanTan],{x,0,3π}]

Plot[{BesselY[0,x],BesselY[1,x],BesselY[2,x]},{x,0,12}]

Clear[x]
f4[a_,b_,c_]:=a[b,{x,0,c}];
f4[NIntegrate,{Tan[x],4Cos[x]},3π]

Clear[x]
f4[a_,b_,c_]:=a[b,{x,0,c}];
f4[NIntegrate,λ Cos[x],2π]

Lists

Subseção

Module[{y=2},x=(y^2)]

mylist={2,4,6,8,10};
mylist2={1,1,2,3,5,8,13,21};

mylist[[4]]

mylist2[[3]]
mylist2[[{2,7}]]

mylist2[[-1]]

mymatrix={{1,2,A,B},{2,4,6,8}}
MatrixForm[mymatrix]

MatrixForm[Transpose[mymatrix]]MatrixForm[{1,2,3,4}]

{0,1}.mymatrix . {1+I,2,3,4}

Conjugate[{1+I,2,3,4}]

Table[RandomReal[{0,1}],{i,1,5}]

Total[mylist2]
Sum[mylist2[[i]],{i,1,8}]

mylist3=Table[RandomReal[],{100000}];

AbsoluteTiming[Total[mylist3]]

Timing[Total[mylist3]]
Timing[Sum[mylist3[[i]],{i,1,1000000}]]

mylist3=Table[RandomReal[],{50000}];
Histogram[mylist3]

Dot Product

mymatrix.{1,2,3,4}

{3,0}.mymatrix.{1,2,3,4}

MatrixForm[Transpose[mymatrix]]

{3,0}.Transpose[mymatrix]
Transpose[mymatrix].{3,0}

Nova Subseção

2+2

{1,2,3,4}
MatrixForm[{{1},{2},{3},{4}}]
Transpose[{{1},{2},{3},{4}}]

Manipulate

f1[ω_]:=Plot[((Sin[ω x])/(ω x)),{x,0,3π}]
Manipulate[f1[y],{y,.1,5}]

f3[ω_,z_]:=Plot[((Sin[ω x])/(ω x)),{x,0,3π},PlotRange->{-.3,1},AxesStyle->z]
Manipulate[f3[x,z],{x,1,15,1},{z,5,30}]

Manipulate[Sum[k^n,{k,0,m}],{n,1,30,1}]

Manipulate[PolarPlot[θ,{θ,0,t},PlotRange->20],{t,1,8Pi}]

Manipulate[ContourPlot3D[x^2+y^2+a z^3==1,{x,-2,2},{y,-2,2},{z,-2,2},Mesh->None],{a,-2,2}]

Manipulate the electrostatic potential built from point charges :

Manipulate[ContourPlot[q1/Norm[{x,y}-p[[1]]]+q2/Norm[{x,y}-p[[2]]],{x,-2,2},{y,-2,2},Contours->10],{{q1,-1},-3,3},{{q2,1},-3,3},{{p,{{-1,0},{1,0}}},{-1,-1},{1,1},Locator},Deployed->True]

Manipulate Solutions of Linear Systems

A=(({{-1.1, 0.9}, {-1.4, 0.3}}));
Manipulate[ParametricPlot[Evaluate[MatrixExp[A t,#]&/@pt],{t,0,10},PlotRange->5],{{pt,{{2,0},{0,1},{-3,0}}},Locator},SaveDefinitions->True]

Differential Equations

Clear[y,x]
sol=DSolve[{y''[x]+ a ((y'[x])^1)+2==0,y[0]==2,y'[1]==0},y[x],x]

Plot[y[x]/.sol/.a->3,{x,0,5}]

DSolveValue[{y''[x]+((y'[x])^1)+2==0,y[0]==2,y'[1]==0},y[x],x]
Plot[%,{x,0,5}]

Plot[y[x]/.sol,{x,0,5}]

y[x]/.sol[[1]]

Clear[y,x]
sol=DSolveValue[{y''[x]+a y'[x]+y[x]+2==0,y[0]==1, y[1]==1},y[x],x]
z[x_,a_]=sol;

Manipulate[Plot[z[x,a],{x,0,15},PlotRange->{-5.5,1.5}],{a,0,2}]

Limit[z[x,a],a->2]

Clear[y]
sol2=NDSolveValue[{y''[x]+ y'[x]+((y[x])^2)+2==0,y[0]==1, y[1]==1},y[x],{x,0,6}]
z2[x_]=sol2;

Plot[z2[x],{x,0,5}]

Clear[sol3,aux]
sol3[a_]:=NDSolveValue[{y''[x]+a y'[x]- ((y[x])^2)+1.1==0,y[0]==1, y[1]==1},y[x],{x,0,10}];

sol3[0]

Manipulate[Plot[Evaluate[sol3[a]],{x,0,10}],{a,0,4}]

Coupled Differential Equations

Manipulate[solns=NDSolve[{ee'[t]==-I*c1/2*(ge[t]-eg[t])-c2*((ee[t])^2),gg'[t]==I*c1/2*(ge[t]-eg[t])+c2*ee[t],ge'[t]==-c2/2*ge[t]+I*c1/2*(gg[t]-ee[t])-c3/2*ge[t],eg'[t]==-c2/2*eg[t]-I*c1/2*(gg[t]-ee[t])-c3/2*eg[t],ee[0]==1,gg[0]==0,ge[0]==0,eg[0]==0},{ee,gg,ge,eg},{t,0,5}];
Plot[{ee[t]/.solns,gg[t]/.solns},{t,0,5}],{{c1,0,"Rabi Frequency"},0,10,Appearance->"Labeled"},{{c2,0,"Dephasing"},0,3,Appearance->"Labeled"},{{c3,0,"Spont. Emission"},0,10,Appearance->"Labeled"}]

Simplifications and Assumptions

We often make implicit assumptions about many variables
Examples:  x ∈ Reals, x ∈ Integers,  x > 0, x ≠ 0, etc.
Mathematica by default make no such assumptions

Important Functions: ‘Simplify’ and ‘FullSimplify’

I I  ->  ‘OR’
&& -> ‘AND’

Integrate[1/x,{x,2,y}]

Integrate[1/x,{x,2,y},Assumptions->y∉Reals]

Integrate[1/x,{x,2,y},Assumptions->y>2]

Simplify[(1/(3 (1+x)))-((-1+2 x)/(6 (1-x+(x^2))))+(2/(3 (1+(1/3) (((-1+2 x))^2))))]

Expand[(((1+x))^5)]

Simplify[Sin[x]^2+Cos[x]^2]
Simplify[2Tan[x]/(1+Tan[x]^2)]

Simplify[Sqrt[x^2]]

Simplify[Sqrt[x^2],x∈Reals]

Simplify[Sqrt[x^2],x<0]

Simplify[Cosh[x]-Sinh[x]]
FullSimplify[Cosh[x]-Sinh[x]]

Clear[a,e,f,b]

,e

FullSimplify[f[f[e,e],e]==e, ForAll[a,f[a,e]==a]]

FullSimplify[f[f[a,e],e]==e, ForAll[a,f[a,e]==a]]

Fermat' s Last Theorem

FullSimplify[x^n+y^n==z^n,Element[x|y|z|n,Integers]&&n>2&&x y z≠0]

Beautiful Plots

2D Plots

Options to be discussed : ListPlot, ContourPlot, Frame, FrameLabel, PlotStyle, Filling, FillingStyle

myplot=Plot[{BesselJ[0,x],BesselJ[1,x],BesselJ[2,x]},{x,0,16},Frame->True,FrameStyle->19,FrameLabel->{"Temperatura","Tempo"}]

Plot[{BesselJ[0,x],BesselJ[1,x]},{x,0,16},Frame->True,FrameStyle->19,FrameLabel->{"Temperatura","Tempo"},PlotStyle->{{Red,Dashing[{.1,.03}],Thickness[.01]},{LightBlue,Thickness[.01]}}]

Plot[{BesselJ[0,x],BesselJ[1,x],BesselJ[2,x]},{x,0,16},Frame->True,FrameStyle->20,FrameLabel->{"Temperature","(Subscript[J, n])[x]"}]

Plot[{2(x^2)-(x^3),2(x^2)-(x^3)+.3,2(x^2)-(x^3)-.3},{x,0,2},Frame->True,FrameStyle->20,FrameLabel->{"Temperature","ζ[x]"},PlotRange->{-.5,1.6},Filling->{2->{3}}]

myticks={{Table[.3i,{i,-10,10}],None},{{0,1,2},None}};
myplot=Plot[{2(x^2)-(x^3),2(x^2)-(x^3)+.3,2(x^2)-(x^3)-.3},{x,0,2},Frame->True,FrameStyle->20,FrameLabel->{"Temperature","ζ[x]"},Filling->{2->{3}},PlotRange->{-.5,1.6},FillingStyle->Opacity[.1,Blue],PlotStyle->{Red,{Red,Dashing[.1]},{Red,Dotted}},GridLines->{{.7,1,1.3},{.5}},Axes->False,FrameTicks->myticks]

LogLinearPlot[1/(x),{x,.01,10},Frame->True,FrameStyle->20]

g[x_]:={2(x^2)-(x^3),2(x^2)-(x^3)+.3,2(x^2)-(x^3)-.3};

g[x_]:=(x^(1/3));

mylist={.2,.4,1,2,3,4,5,6,8};
mylist=Table[{1.Log[i],1.(i^2)},{i,1,6}];
vec=Map[g,mylist]
Dimensions[vec]

myfun=Interpolation[mylist,InterpolationOrder->3]
myfun[.5]
Plot[myfun[x],{x,0,2.3}]

mylistplot=ListPlot[vec,Joined->False]

ContourPlot[BesselJ[x,y],{x,0,13},{y,0,20},Contours->{-.2,0,.2,.6},FrameStyle->24]

Manipulate[ContourPlot[Sin[x]Cos[y],{x,0,xmax},{y,0,ymax},Contours->{-.2,0,.2,.4},PlotPoints->30],{xmax,1,100},{ymax,1,100}]

Manipulate[ParametricPlot[{Sin[u],(Sqrt[u]) Cos[v u]},{u,0,2Pi}],{v,1,10,1}]

Show[mylistplot,myplot,Frame->True]

3D Plots

Plot3D[(Sqrt[1-(x^2)-2(y^2)]),{x,-1,1},{y,-1,1},PlotRange->Full,LabelStyle->20,AxesLabel->{"x","y","z"}]

ParametricPlot3D[{{Sin[θ]Cos[ϕ],Sin[θ]Sin[ϕ],Cos[θ]},{.4Cos[ϕ],.4Sin[ϕ],1.2(θ/π)}},{ϕ,0,2π},{θ,0,π}]

ParametricPlot3D[{{Sin[θ]Cos[ϕ],Sin[θ]Sin[ϕ],Cos[θ]},{.4Cos[ϕ],.4Sin[ϕ],1.2(θ/π)}},{ϕ,0,2π},{θ,0,π},PlotStyle->{Transparent,LightBlue},Mesh->6]

ParametricPlot3D[{{Sin[θ]Cos[ϕ],Sin[θ]Sin[ϕ],Cos[θ]},{.4Cos[ϕ],.4Sin[ϕ],1.2(θ/π)}},{ϕ,0,2π},{θ,.42,π},PlotStyle->{Opacity[.8],LightBlue},Mesh->6]

ParametricPlot3D[{{Sin[θ]Cos[ϕ],Sin[θ]Sin[ϕ],Cos[θ]},{.4Cos[ϕ],.4Sin[ϕ],1.2(θ/π)}},{ϕ,0,2π},{θ,.42,π}]

Importing and Exporting Data

Monitor[i=0;While[i<60,Pause[0.1];i++];i,i]

Monitor[Do[RandomReal[{0,i}](j^2),{i,1,500},{j,1,10000}];,ToString[i 100./500]<>""]

mydata=Table[0,{i,1,500},{j,1,10000}];
Monitor[Do[mydata[[i,j]]=RandomReal[{0,i}](j^2),{i,1,500},{j,1,10000}];,ToString[100i/500.]<> "%"]

Monitor[mydata=Table[Pause[.02],{i,1,5},{j,1,10}];,ToString[100i/500.]<> "%"]

lmax=6;
Monitor[mydata=Table[Integrate[SphericalHarmonicY[l,m,θ,ϕ],{θ,0,π}],{l,0,lmax},{m,-lmax,lmax}];,ToString[100.l/lmax]<> "%"]

mydata

MatrixForm[mydata]

SetDirectory[NotebookDirectory[]]

Export["myplot.png",myplot,"PNG",ImageResolution->300]

mydata2=Table[RandomReal[{0,i}](j^2),{i,1,200},{j,1,50}];
Export["myfile.txt",mydata2]
Export["myfile.list",mydata2,"Table"]
Export["myfile.r32",mydata2,"Real32"]

Dimensions[mydata2]

mydata3=Import["myfile.r32","Real32"];
mydata3=Partition[mydata3,50];
Dimensions[mydata3]

mydata3=Import["myfile.list","Table"];
Dimensions[mydata3]

Wolfram Data Base & Wolfram Alpha

mydata

MatrixForm[mydata]

CountryData[ "Brazil","GDP"]
CountryData["Brazil","HighestElevation"]
popBrazil=CountryData["Brazil",{"Population",{1914,2014}}]

DateListPlot[popBrazil,FrameStyle->19]

mygeo=FindGeoLocation[]

WeatherData[mygeo,"Temperature"]
WeatherData[mygeo,"WindSpeed"]
WeatherData[mygeo,"WindDirection"]
WeatherData[mygeo]

MinorPlanetData["Pluto","Radius"]
MinorPlanetData["Eris","Radius"]
MinorPlanetData["Pluto","Color"]

dydata=Gather[ExoplanetData[EntityClass["Exoplanet",All],"DiscoveryYear"]];

With[{data=Sort[Join[{#[[1]]["Year"],Length[#]}&/@dydata,{{1990,0},{1991,0}}]]},BarChart[data[[All,2]],ChartLabels->Placed[data[[All,1]],Axis,Rotate[#,90Degree]&],PlotLabel->"number of exoplanet discoveries per year"]]

SNdata=Gather[SupernovaData[EntityClass["Supernova",All],"DiscoveryYear"]];

SNlist=Sort[{#[[1]]["Year"],Length[#]}&/@SNdata]
Total[SNlist[[All,2]]]

snshortlist=Drop[Drop[SNlist,50],-1]

Fit[snshortlist,c Exp[((x-a)/b)],{x,a,b}]

fun[a_,b_,c_,x_]:=c Exp[((x-a)/b)];
Map[fun[]]

chisquared[a_,b_,c_]:=Total[(((fun[a,b,c]-dado))^2)];
FindMinimum[chisquared]

plot1=ListPlot[Drop[SNlist,50]]
plot2=Plot[25Exp[((x-1988)/6)],{x,1970,2025},PlotStyle->Red];
Show[plot1,plot2,FrameStyle->20,Frame->True]

With[{data=Drop[SNlist,50]},BarChart[data[[All,2]],ChartLabels->Placed[data[[All,1]],Axis,Rotate[#,90Degree]&],PlotLabel->"number of Supernovae discoveries per year"]]

min=WeatherData[mygeo, "MinTemperature", {{2014, 1, 1}, {2014, 12, 31}, "Day"}];
max=WeatherData[mygeo, "MaxTemperature", {{2014, 1, 1}, {2014, 12, 31}, "Day"}];
DateListPlot[{min,max}, Joined->True,Filling->{1->{2}}]

CountryData[ "Brazil","Shape"]

CountryData["World",{"Shape","Mollweide"}]

Parallel Programming

LaunchKernels[4]

Examples in which Simple Parallelization Work Well

lmax=8;
AbsoluteTiming[mydata=Table[Integrate[SphericalHarmonicY[l,m,θ,ϕ],{θ,0,π/2}],{l,0,lmax},{m,-lmax,lmax}];]

AbsoluteTiming[mydata2=ParallelTable[Integrate[SphericalHarmonicY[l,m,θ,ϕ],{θ,0,π/2}],{l,0,lmax},{m,-lmax,lmax}];]

mydata2-mydata

lmax=8;
t1=AbsoluteTime[];
AbsoluteTiming[mydata=Table[If[l==4&&m==0,timevar=AbsoluteTime[]-t1;Print[timevar*9/4.];];Integrate[SphericalHarmonicY[l,m,θ,ϕ],{θ,0,π/2}],{l,0,lmax},{m,-lmax,lmax}];]

Examples in which it does not

imax=1000;
jmax=imax;

Remove[data]
data=Table[Tanh[i/900.],{i,1,imax}];
t1=AbsoluteTime[];
AbsoluteTiming[
mydata=Table[If[i==400&&j==0,timevar=AbsoluteTime[]-t1;Print[{timevar,timevar*9.}]];
data[[i]]=
Cos[Sum[data[[j]]
,{j,1,jmax}]],{i,1,imax}];]

Remove[data]
data=Table[Tanh[i/900.],{i,1,imax}];
SetSharedVariable[data]
AbsoluteTiming[
mydata2=ParallelTable[
data[[i]]=Cos[Sum[data[[j]]
,{j,1,jmax}]],{i,1,imax}];]

mydata2-mydata