Question
2 - Matlab Implementation code for the comparison between the ode45 Matlab method and the exact solution.
Also write the numerical result as detailed explanation.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
%final timetf=2;
disp(['final time = ' num2str(tf)]);
disp('table ODE45')
Abstols=5*10.^[-3:-3:-12];
nsteps=[];
MaxError=[];
for k=1:length(Abstols)
Abstol=Abstols(k);
[nsteps(k),MaxError45(k)]=P(tf,Abstol);
end
disp('ODE45: Tol MaxErr Nsteps ')
for k=1:length(Abstols)
fprintf('%g %g %d \n',Abstols(k),MaxError45(k),nsteps(k));
end
%this is just to generate a plot of timestep used by ode45
to_plot=1;
P(tf,Abstol(end),to_plot);
disp('table ERK')
MaxError=[];
AbsoluteError=[];
Ns=10*2.^[3:7];
for k=1:length(Ns)
n=Ns(k);
[h(k),MaxError(k),AbsoluteError(k)]=erk(n,tf);
end
disp('ERK: MaxErr Nsteps h ')
for k=1:length(Ns)
n=Ns(k);
fprintf('%g %d %g \n',MaxError(k), n,h(k));
end
figure;
loglog(nsteps,MaxError45,Ns,MaxError,'Linewidth',2);
xlabel('N Steps');
ylabel('Max Error');
legend('ode45','erk','Location','Best');
title_str=['Comparison ODE45 and ERK, Final Time = ' num2str(tf)];
title(title_str);
%final time 8
tf=8;
disp(['final time = ' num2str(tf)]);
disp('table ODE45')...