how to plot euler forward method

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

Réponses (1)

James Tursa
James Tursa le 22 Oct 2019

0 votes

Don't change the iteration variable withing the loop. Remove these lines from your code:
n=1:11;

2 commentaires

James Tursa
James Tursa le 22 Oct 2019
Modifié(e) : James Tursa le 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
James Tursa le 22 Oct 2019
Modifié(e) : James Tursa le 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');

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by