Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to plot numerical solution of differential equations?

TulipppTulippp Posts: 2Member
My problem is how to plot the exact and the approximated solutions?
This is the Matlab M-file for solving differential equations with Euler's Method.

a=0;
b=1;
y0=-1;
N=10;
fprintf('
')
disp(' Euler Method ')
disp('__________________________________')
disp('ti f(ti,yi) yi exact error')
disp('__________________________________')
fprintf('
')
h=(b-a)/N;
y=y0;
fprintf('%4.2f ----------- %12.6f %12.6f %4.2f
',a,y,y,0)
for i=1:N
t=a+(i-1)*h;
m=f(t,y);
y=y+h*m;
t=t+h;
g=exp(-t)+2*t-2; (exact solution)
if (g~='N')
err=abs(g-y);
fprintf('%4.2f %12.6f %12.6f %12.6f %8.2e
',t,m,y,g,err)
else
fprintf('%4.2f %12.6f %12.6f
',t,m,y)
end
Sign In or Register to comment.