how to plot euler forward method
3 views (last 30 days)
Show older comments
could you please help me make a disply (plot) containing the graphs of four solutions (b),(a),(u) and (w), when the constant is changing in four cases.
clear all
c=0;
b(1)=1;
v(1)=-2;
h=0.1;
%explicit euler
for n=1:10
b(n+1)=b(n)+(h*v(n))
v(n+1)=v(n)-(((c*h)/3)*v(n))-16*b(n)*h;
n=1:11;
end
clear all
c=30;
a(1)=1;
v(1)=-2;
h=0.1;
%explicit euler
for n=1:10
a(n+1)=a(n)+(h*v(n))
v(n+1)=v(n)-(((c*h)/3)*v(n))-16*a(n)*h;
n=1:11;
end
clear all
c=24;
u(1)=1;
v(1)=-2;
h=0.1;
%explicit euler
for n=1:10
u(n+1)=u(n)+(h*v(n))
v(n+1)=v(n)-(((c*h)/3)*v(n))-16*u(n)*h;
n=1:11;
end
clear all
c=6;
w(1)=1;
v(1)=-2;
h=0.1;
%explicit euler
for n=1:10
w(n+1)=w(n)+(h*v(n))
v(n+1)=v(n)-(((c*h)/3)*v(n))-16*w(n)*h;
n=1:11;
end
0 Comments
Answers (1)
James Tursa
on 22 Oct 2019
Don't change the iteration variable withing the loop. Remove these lines from your code:
n=1:11;
2 Comments
James Tursa
on 22 Oct 2019
Edited: James Tursa
on 22 Oct 2019
You should get rid of the "clear all" lines also, since these wipe out your solutions. I don't get any errors when I run your code. What errors are you getting? Please post them.
James Tursa
on 22 Oct 2019
Edited: James Tursa
on 22 Oct 2019
Yes. Now you can simply plot a, b, u, w. E.g.,
x = (0:numel(a)-1)*h;
plot(x,b,x,a,x,u,x,w);
grid on
legend('undamped','overdamped','critically damped','underdamped');
See Also
Categories
Find more on Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!