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

2 visualizzazioni (ultimi 30 giorni)
Itqan Ismail il 19 Giu 2021
Commentato: Itqan Ismail il 20 Giu 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Rik il 19 Giu 2021
If you use array inputs to your anonymous function you can use the plot function.
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Rik il 19 Giu 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 il 20 Giu 2021
thank youuu so much

Accedi per commentare.

### Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by