How can I plot (y vs t) and (z vs t) in one graph?

3 次查看(过去 30 天)
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

采纳的回答

Rik
Rik 2021-6-19
If you use array inputs to your anonymous function you can use the plot function.
  3 个评论
Rik
Rik 2021-6-19
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by