How can I plot (y vs t) and (z vs t) in one graph?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Rik
il 19 Giu 2021
If you use array inputs to your anonymous function you can use the plot function.
3 Commenti
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)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Migrate GUIDE Apps 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!