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

7 ビュー (過去 30 日間)
Charles Zhou
Charles Zhou 2017 年 1 月 27 日
回答済み: Sally Al Khamees 2017 年 2 月 1 日
%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.

回答 (1 件)

Sally Al Khamees
Sally Al Khamees 2017 年 2 月 1 日
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

カテゴリ

Help Center および File ExchangeVisual Exploration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by