Harmonic Oscillator

Solve differential equation

Differential Equation:  x'' + 2b*x' + c*x = 0

System of differential equations with parameters b and c
Set b = 0 for no damping

Clear[x,t,b,c]
b = 0.;
c=1.;
equationx=x'[t]==xdot[t]
equationxdot=xdot'[t]==-2*b* xdot[t]-c*x[t]

Initial conditions for the trajectory

Clear[xstart,xdotstart]
{xstart,xdotstart} = {1.,0.};
initialx=x[0]==xstart
initialxdot=xdot[0]==xdotstart

Solve the system of differential equations numerically
from time 0 to EndTime

EndTime = 30;xsolution=NDSolve[{equationx,equationxdot,initialx,initialxdot},{xdot[t],x[t]},{t,0,EndTime}];

Time evolution of position

Use the solution of the system of differential equations
to obtain the position x(t) as a function of t

Clear[xgraph]
xgraph[t_]=x[t]/.xsolution[[1]]

Display the graph of x(t)  and animate it

Plot[xgraph[t],{t,0,EndTime},AxesLabel->{"t","x"},PlotStyle->{Red,Thickness[0.005`]}]

Animate[Plot[xgraph[t],{t,0,tt},AxesLabel->{"t","x"},PlotRange-> {{0,EndTime},{-1,1}},PlotStyle->{Red,Thickness[0.005`]}],
{tt,0,EndTime}]

Phase space trajectory

Use the solution of the system of differential equations
to obtain the phase space trajectory (x,x') as a function of t

Clear[trajectory]
trajectory[t_]={x[t]/.xsolution[[1]],xdot[t]/.xsolution[[1]]}

Plot the trajectory and animate it

xrange = {-4,4};
xdotrange = {-4,4};
ParametricPlot[trajectory[t],{t,0,EndTime},AxesLabel->{"x","x'"},PlotRange->{xrange,xdotrange},PlotStyle->{{Red,Thickness[0.005`]}}]

Animate[ParametricPlot[trajectory[t],{t,0,tt},AxesLabel->{"x","x'"},PlotRange->{xrange,xdotrange},PlotStyle->{Red,Thickness[0.005`]},Epilog->{Blue,PointSize[0.03`],Point[trajectory[tt]]}],{tt,0,EndTime}]

Phase space portrait

List of initial values

Clear[startlist,length]
startlist = {{0.5,0.},{1.,0.},{2.,0.},{4.,0.}}
length=Length[startlist];

Make a list of phase space trajectories for each inital condition

portrait={}
Do[
initialx=x[0]==startlist[[n,1]];
initialxdot=xdot[0]==startlist[[n,2]];xsolution=NDSolve[{equationx,equationxdot,initialx,initialxdot},{xdot[t],x[t]},{t,0,EndTime}];
trajectory[t_]={x[t]/.xsolution[[1]],xdot[t]/.xsolution[[1]]};
trajectoryplot=ParametricPlot[trajectory[t],{t,0,EndTime},PlotStyle->{Red,Thickness[0.005]}];
orbit=Show[trajectoryplot,PlotRange->{xrange,xdotrange},AxesLabel->{"x","x'"}];
portrait=Append[portrait,orbit],
{n,1,length}]

Draw the phase space portrait

Show[portrait]

Damped Harmonic Oscillator