SetDirectory[NotebookDirectory[]]

Pallettes -> Other -> Basic Math Input

Write/Read data

Export["out.dat",{{5.7,4.3},{-1.2,7.8}},"CSV"]

out=Import["out.dat","CSV"]

Vectors

vec1={1,3/4,4.5,4π}

vec1

N[vec1]

vec2=Range[20]

vec3=Range[0,20,2]

vec1+vec2

vec4={2.,-5,0,14};

10vec1

vec1-vec4

vec1 vec4

vec1.vec4

vec1[[1]]

vec1[[2;;4]]

awww=Select[vec1,#>2&]

#^2+3&[m]

#^2+3&@m

#>2&@1

#>2&/@{1,1.1,2,4,6}

Flatten[Position[vec1,#]&/@Select[vec1,#>2&]]

Arrays, Lists, Tables

Clear[out]

Dimensions[vec1]

tab=Table[n!,{n,1,20}]

Plot[Cos[x],{x,1,5}]

ListLogPlot[tab]

tab3d=Table[{(i^2),(j^2),(k^2)},{i,1,20},{j,1,20},{k,1,20}]

awwww=tab3d[[;;,3,;;]];

tab3d[[1,4,8]]

tab3d[[1,4;;12,-8]]

ImageData[][[1,1;;5]]

Plotting in 2 d and 3 d

Plot[Sin[x],{x,0,6Pi}]

Plot[{Sin[x],Sin[2x],Sin[3x]},{x,0,2Pi},PlotLegends->"Expressions"]

Complex signal plot

sig[t_]:=Piecewise[{{0.5Sin[2π 45 t],0.1<t<0.3},{Sin[2π 250 t],0.4<t<0.5},{Sin[2π 75 t],0.5<t<0.6},{Sin[2π 30 t]+Sin[2π 110t],0.7<t<0.9}}]+Piecewise[{{2(E^(-((((t-0.2))^2)/(2 (0.001^2)))))-0.5Sin[2π 45 t],0.2-0.005<t<0.2+0.005}}]

Plot[sig[t],{t,0,1},PlotRange->All,Frame->True,FrameLabel->{"time, s","x(t)"},PlotStyle->Gray,LabelStyle->{FontFamily->"Helvetica",12,GrayLevel[0.3]}]

Plot3D[Sin[x+y^2],{x,-3,3},{y,-2,2}]

Plot3D[ ((x^2)+(y^2)) Exp[1-(x^2)-(y^2)],{x, -3, 3},{y, -3, 3},PlotStyle->Opacity[0.5],Mesh->None,PlotPoints->50]

c=ParametricPlot3D[{t,0,0},{t,-6 Pi,6 Pi},PlotRange->All,Boxed->False,Axes->False];
b=ParametricPlot3D[{t,0,0.1 t^2},{t,-6 Pi,6 Pi},PlotRange->All,Boxed->False,Axes->False];
a=ParametricPlot3D[{r Cos[θ],r Sin[θ],0.1 r^2},{θ,0,6 Pi},{r,0,17},PlotRange->All,Boxed->False,Axes->False,Mesh->None,PlotStyle->Opacity[0.1]];
Show[a,b,c]

Dynamical manipulation

Manipulate[Plot[Sin[x(1+a x)],{x,0,6}],{a,0,2,0.5}]

Animate[Plot[Sin[x(1+a x)],{x,0,6}],{a,0,2}]

Manipulate[Factor[2^n+1],{n,10,100,1}]

Histograms

Histogram[{1,2,5,8,5,7,9,10,50,3,6,24}]

Histogram[RandomVariate[NormalDistribution[0,1],200],PlotTheme->"Scientific"]

data1=RandomVariate[NormalDistribution[0,1],500];
data2=RandomVariate[NormalDistribution[3,1/2],500];

Histogram[{data1,data2}]

SmoothHistogram[{data1,data2}]

Random numbers

RandomReal[]

RandomReal[{-2,1},10]

RandomInteger[{-10,20},{10,2}]

ClearAll[a,b,c];

RandomChoice[{a,"Y",c},20]

Range[30]

RandomSample[Range[30],20]

RandomChoice - repetitions, RandomSample - no elements ever occur more than once

data=RandomVariate[NormalDistribution[1,3],10^4];

Show[
Histogram[data,20,"ProbabilityDensity"],
Plot[PDF[NormalDistribution[1,3],x],{x,-9,9},PlotStyle->Thick]]

Homogeneous Poisson process is a prosess in which the probability of event happening during the time interval dt remains the same for any interval.

Simulate a homogeneous (or inhomogeneous) Poisson processes (imagine a counting experiment with certain rate which changes as a function of time for inhomogeneous Poisson process). How to simulate the times when events are happening (e.g., a decay which produces photon which is registeres by the instrument)?

SeedRandom[34];

RandomFunction[PoissonProcess[1.3],{0,15},5]

data=RandomFunction[PoissonProcess[1.3],{0,15},5]["TimeList"];

IPP[p_]:=InhomogeneousPoissonProcess[((1-p)/(2π))+p Sin[2π 2.3t],t]

data=RandomFunction[IPP[1],{0,100},300]["TimeList"];

Dimensions[data]

Length/@data

Be sure to  explore and understand the structure of the object called “TemporalData”   and read about it in Mathematica help. What kind of output does the command below produce?

What does this command do?

Histogram[Length/@data]

Generate randomly distributed points

ng=50;

g1={RandomVariate[NormalDistribution[-3.,.1],ng],RandomVariate[NormalDistribution[1.,1.],ng]}\[Transpose];
g2={RandomVariate[NormalDistribution[3.,.1],ng],RandomVariate[NormalDistribution[1.,1.],ng]}\[Transpose];
gtemp={RandomVariate[NormalDistribution[0,0.75],2ng],RandomVariate[NormalDistribution[0,0.75],2ng]}\[Transpose];
g3=SortBy[gtemp,Total[((#)^2)]&][[;;ng]];
g4=SortBy[gtemp,Total[((#)^2)]&][[ng+1;;]];
g51=Subdivide[-2.75,2.75,ng];
g52=(((g51/2))^2)-4;
g5={g51,g52+RandomVariate[NormalDistribution[0,0.5],Length[g52]]}\[Transpose];
data1={g1,g2,g3,g4,g5};

ListPlot[data1,PlotRange->All,AspectRatio->Automatic,PlotStyle->PointSize[Medium]]

What do the the commands below do?  Why is the Transpose operation needed?  Transpose[m] can be input as mᵀ.

Dimensions[data1]

data1

data=Flatten[data1,1];

Dimensions[data]

data

Dimensions[data\[Transpose]]

data\[Transpose]

MinMax[data\[Transpose]]

mm=MinMax/@(data\[Transpose])

Functions

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

f[5.]

N[f[5]]

f[5.]

((#)^2)+3Sin[((#)^2)]&

Map[f,{a,b,c,d,e}]

f/@{a,b,c,d,e}

N[((#)^2)+3Sin[((#)^2)]&/@{5.,7,6.}]

Equations

Solve[a (x^2)+b x+c==0,x]

Clear[x,y]

Solve[f[x]==x,x]

NSolve[f[t]==t,t,Reals]

Solve[a x+y==7&&b x-y==1,{x,y}]

sol=NSolve[{x^2+y^3==1,2x+3y==4},{x,y}]

sol[[2,1,2]]

ReplaceAll ( /. )

x+y/.sol[[2]]

Systems of linear equations

A=Table[RandomReal[{0,i}]+(j^2),{i,4},{j,4}];

MatrixForm[A]

b=Table[1,4]

Inverse[A]

Inverse[A].b

Differential equations

DSolve[{x''[t]==a,x'[0]==v0,x[0]==x0},x[t],t]

DSolve[y'[x]+y[x]==a Sin[x],y[x],x]

Clear[t]

Manipulate[ParametricPlot[Evaluate[{x[t],y[t]}/.NDSolve[{y'[t]==-4x[t]-Cos[2t],x'[t]==y[t],x[0]==p0[[1]],y[0]==p0[[2]]},{x,y},{t,20}]],{t,0,20},PlotRange->All],{{p0,{1,1}},Locator},SaveDefinitions->True]

Integration

Clear[n]

Integrate[x^n,x]

Integrate[Exp[-x^2.],{x,0,Infinity}]

(∫_0^∞)(E^(-(x^2)))\[DifferentialD]x

Integrate[Exp[-x^2-y^2],{x,-Infinity,Infinity}, {y,-Infinity,Infinity}]

∫(E^(-x ((Sin[x])^2)))\[DifferentialD]x

N[(∫_0^∞)(E^(-x ((Sin[x])^2)))\[DifferentialD]x]

Interpolation

tab=Table[Sin[x],{x,0,2π,0.1}]

f=Interpolation[tab]

Show[Plot[f[x],{x,1,63}],ListPlot[tab]]

tab=Table[{x,Sin[(x^2)]},{x,0,4π,0.1}];

f=Interpolation[tab]

f[0.3333]

Sin[(0.3333^2)]

Show[Plot[{f[x],Sin[(x^2)]},{x,0,12.5}],ListPlot[tab]]

Fitting

data={{0,1},{1,0},{3,2},{5,4}};

line = Fit[data, {1,x,(x^7)},x]

data=Table[{x,Sin[2π x]+Sin[3π (x^2)]+0.2RandomReal[]},{x,0,4,0.1}];

ListPlot[data]

errors=RandomReal[{0,0.2},Length[data]]

Needs["ErrorBarPlots`"]

f1=ErrorListPlot[{data,ErrorBar/@errors}\[Transpose]]

Clear[b]

nlm=NonlinearModelFit[data,a Sin[2π ω x]+b Sin[3π (x^2)],{a,b,ω},x,Weights->1/errors^2]

nlm[{"BestFit","ParameterTable"}]

Show[f1,Plot[nlm[x],{x,0,4}]]

nlm["ParameterConfidenceIntervals"]

nlm["ANOVATable"]

Conditionals

If[7>8,x,y]

{6,-7,3,2,-1,-2}/.x_/;x<0->w

Loops

For[i=0,i<4,i++,Print[i]]

t=x;Do[t=1/(1+t),{10}];t

Procedures

Module[] allows one to have a local variable (by all default variables in Mathematica are global)

f[v_]:=1+(v)^2;

f[v_]:=Module[{t},t=(1+v)^2;t=Expand[t]]

ff[v_]:=Module[{t},t=(1+v)^2;t=Expand[t]]

ff[(((x+1))^2)]

Greatest common divider for 2 numbers (Euler’s method)

gcd[m0_,n0_]:=
Module[{m=m0,n=n0},
While[n≠0,{m,n}={n,Mod[m,n]}];
m
]