Exercises lecture 3

ClearAll["Global`*"];SetDirectory[NotebookDirectory[]]

Exercise 1: find graphically (or numerically) the minimum order of the series expansion around 0 that approximates the Sin up to better than 0.1 absolute error  over the interval (0,5)

Clear[f,n]
f[n_]:=Normal[Series[Sin[x],{x,0,n}]]
Plot[{Table[Abs[(f[n]/.x->y)-Sin[y]],{n,1,13,2}],0.1},{y,0,5},PlotRange->{0,.5}]
Plot[{Table[(f[n]/.x->y),{n,1,13,2}],Sin[y]},{y,0,5},PlotRange->{-3,3}]

Exercise 2: Solve the growth equation in FRW , in a LCDM model, from N = log a = -5 to N = 0
delta'' + (2+E’/E)*delta' - 3/2*Omega_m*delta = 0
where 
Omega_m=Omega_m0 *(1+z)^3/E^2
E^2=Omega_m0 *(1+z)^3+1-Omega_m0
E’/E=-3/2*Omega_m
with delta[-5]=1 and delta’[-5]=1
Note that 1+z=1/a=Exp[-N]

Compare then with the approximate solution
f=d log delta /d N = Omega_m ^0.545

om0=0.3;
e2[n_]=om0*Exp[-3*n]+1-om0;
omegam[n_]=om0*Exp[-3*n]/e2[n];
epe[n_]=-3/2*omegam[n];
equation=delta''[n]+(2+epe[n])*delta'[n]-3/2*omegam[n]*delta[n]==0;
sol=NDSolve[{equation,delta[-5]==1,delta'[-5]==1},delta,{n,-5,0}];

LogPlot[{(delta'[n]/delta[n])/.sol,omegam[n]^0.54},{n,-5,0},Frame->True,PlotRange->All]

Exercise 3: Solve numerically a differential equation for the motion of a spring with friction and plot the solution; you might use the constant below, or define them as you prefer

(*Define the constants*)
m=1;         (*Mass*)
k=2;         (*Spring constant*)
c=0.5;       (*Friction coefficient*)
x0=1;        (*Initial displacement*)
v0=0;        (*Initial velocity*)
tmax=10;     (*Maximum time*)

(*Define the constants*)
m=1;         (*Mass*)
k=2;         (*Spring constant*)
c=0.5;       (*Friction coefficient*)
x0=1;        (*Initial displacement*)
v0=0;        (*Initial velocity*)
tmax=10;     (*Maximum time*)


(*Define the differential equation*)
equation=m x''[t]+c x'[t]+k x[t]==0;

(*Solve the differential equation*)
sol=NDSolve[{equation,x[0]==x0,x'[0]==v0},x,{t,0,tmax}];

(*Plot the solution*)
Plot[x[t]/. sol,{t,0,tmax},PlotRange->All,AxesLabel->{"Time","Displacement"},PlotLabel->"Spring with Friction"]