How can I plot (y vs t) and (z vs t) in one graph?
2 views (last 30 days)
Show older comments
z=4;y=2;
h=0.1;
t0=0;
tn=0.4;
y=2;
fprintf('x y z\n');
for t=t0:h:tn
dy=@(y,t) -2*y+4*exp(-t);
dz=@(y,z) -(y*z^2)/3;
ynew=y+dy(y,t)*h;
znew=z+dz(y,z)*h;
fprintf('%2.4f %2.4f %2.4f \n',t,ynew,znew);
z=znew;y=ynew;
end
0 Comments
Accepted Answer
Rik
on 19 Jun 2021
If you use array inputs to your anonymous function you can use the plot function.
3 Comments
Rik
on 19 Jun 2021
Due to the missing formatting I didn't notice the iterative part. Something like this will store the result in a vector you can use to plot.
z=4;y=2;
h=0.1;
t0=0;
tn=0.4;
y=2;
dy=@(y,t) -2*y+4*exp(-t);
dz=@(y,z) -(y*z^2)/3;
t=t0:h:tn;
ynew=zeros(size(t));
znew=zeros(size(t));
for n=1:numel(t)
ynew(n)=y+dy(y,t(n))*h;
znew(n)=z+dz(y,z)*h;
z=znew(n);y=ynew(n);
end
plot(t,znew)
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots 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!