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

2 Ansichten (letzte 30 Tage)
Itqan Ismail
Itqan Ismail am 19 Jun. 2021
Kommentiert: Itqan Ismail am 20 Jun. 2021
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

Akzeptierte Antwort

Rik
Rik am 19 Jun. 2021
If you use array inputs to your anonymous function you can use the plot function.
  3 Kommentare
Rik
Rik am 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)
Itqan Ismail
Itqan Ismail am 20 Jun. 2021
thank youuu so much

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by