Physics with Mathematica

Fifth Class

Thanks to everyone for getting me your chosen homework problem.

Today: Numerical Solutions of Differential Equations

The same as analytic solutions, but everything must be numbers, and you have to specify the domain.

Remove["Global`*"]

Let’s solve (d^2)y/(dx^2)=(y^2)-2x subject to y(0)=0 and y’(0)=1.

eqs={y''[x]==y[x]^2-2x,y[0]==0,y'[0]==1};
sol=DSolve[eqs,y[x],x]

xmax=10;
sol=NDSolve[eqs,y[x],{x,0,xmax}][[1]]

yPlot=y[x]/.sol[[1]];
Plot[yPlot,{x,0,xmax}]

Physics Example: Period and Motion of a 1D Pendulum (1)

Remove["Global`*"]

First let’s use the energy to calculate and plot the pendulum period as a function of initial angle θ0.

$Assumptions={ω>0,l>0,θ0>0,θ0<Pi};

eq=(1/2)m l^2θDot^2+m g l(1-Cos[θ])==m g l(1-Cos[θ0]);
sol=Solve[eq,θDot]

dθdt=Simplify[θDot/.sol[[2]]/.g->ω^2l]

t1=AbsoluteTime[];
period=4Integrate[1/dθdt,{θ,0,θ0}]
t2=AbsoluteTime[];

t2-t1

periodSimple=2 Pi/ω;
Plot[period/periodSimple,{θ0,0,0.999Pi},PlotRange->{0,5},GridLines->Automatic]

Physics Example: Period and Motion of a 1D Pendulum (2)

Now solve the differential equation to find the motion

eqs={θ''[t]+ω^2Sin[θ[t]]==0/.ω->2Pi,θ[0]==θ0,θ'[0]==0};

tMax=10;

θ0val=0.5;
sol=NDSolve[eqs/.θ0->θ0val,θ[t],{t,0,tMax}];
Plot[{θ[t]/.sol,θ0val Cos[2Pi t]},{t,0,tMax}]

θ0val=2.5;
sol=NDSolve[eqs/.θ0->θ0val,θ[t],{t,0,tMax}];
Plot[θ[t]/.sol,{t,0,tMax}]

θ0val=3.1;
sol=NDSolve[eqs/.θ0->θ0val,θ[t],{t,0,tMax}];
Plot[θ[t]/.sol,{t,0,tMax}]