problem to create three graph in one Matlab file (explicit Euler, implicit Euler, Heun)

4 views (last 30 days)
function taska
h = 0.1;
x = 0:h:pi;
y1 = [0];
%euler exlicit method
for i = 1:length(x)-1
y1(i+1)=y1(i)+h*f1(x(i),y1(i));
end
%heun's method
for i = 1:length(x)-1
x(i+1)=x(i)+h;
ynew=y1(i)+h*(f1(x(i),y1(i)));
y2(i+1)=y1(i)+(h/2)*(f1(x(i+1),y1(i))+h*(f1(x(i+1),ynew)));
end
%euler implicit method
for i = 1:length(x)-1
x(i+1)=x(i)+h;
ynew=y1(i)+h*(f1(x(i),y1(i)));
y3(i+1)=y1(i)+h*f1(x(i+1),ynew);
end
plot(x,y1,'g',x,y2,'r',x,y3,':')
end
function dy = f1(x,y1)
y0 = -1;
dx=0.01;
d = 50;
c1=(y0-(d^2/(d^2+1)));
dy=c1*exp(-dx)+d*(sin(x)/(d^2+1))+d^2*(cos(x)/d^2+1);
end

Accepted Answer

Ameer Hamza
Ameer Hamza on 9 Nov 2020
Your code plot 3 graphs. But they are so close that it is difficult to see them separately. You can try the following line to plot the graphs()
plot(x,y1,'g-.+',x,y2,'b',x,y3,'m-')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!