Plotted velocity and position using rk4, want acceleration on the plot also

1 visualizzazione (ultimi 30 giorni)
%functions
fX=@(t,X,V) V;
fV=@(t,X,V) a*V+b+(c/X);
%initial conditions
t(1)=0;
V(1)=0;
X(1)=1;
%Step size
h=0.001;
tfinal=10;
N=ceil(tfinal/h);
%update loop
for i=1:N
t(i+1)=t(i)+h;
k1X=fX(t(i) ,X(i) ,V(i) );
k1V=fV(t(i) ,X(i) ,V(i) );
k2X=fX(t(i)+h/2,X(i)+h/2*k1X,V(i)+h/2*k1V);
k2V=fV(t(i)+h/2,X(i)+h/2*k1X,V(i)+h/2*k1V);
k3X=fX(t(i)+h/2,X(i)+h/2*k2X,V(i)+h/2*k2V);
k3V=fV(t(i)+h/2,X(i)+h/2*k2X,V(i)+h/2*k2V);
k4X=fX(t(i)+h ,X(i)+h *k3X,V(i)+h *k3V);
k4V=fV(t(i)+h ,X(i)+h *k3X,V(i)+h *k3V);
X(i+1)=X(i)+h/6*(k1X+2*k2X+2*k3X+k4X);
V(i+1)=V(i)+h/6*(k1V+2*k2V+2*k3V+k4V);
end
When I plot V and X versus time, everything works just fine, its just that I can't figure out an easy way to get fV, the acceleration to also show up.
plot(t,fV)
That gives an error message: invalid second data argument.

Risposte (1)

Sally Al Khamees
Sally Al Khamees il 1 Feb 2017
If you examine fV in the Workspace, or print it in the command window, you will see that fV is a function handle.
fV = function_handle with value:
@(t,X,V)a*V+b+(c/X)
To plot acceleration, you need to store acceleration values in a vector similar to what you did for X, and V.
Then you can plot fV against t. If you would like to plot the three variables on the same plot, you can use plot3

Categorie

Scopri di più su Graphics Performance 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