필터 지우기
필터 지우기

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

조회 수: 1 (최근 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 CenterFile Exchange에서 Visual Exploration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by