express ODE without set a m file function

조회 수: 2 (최근 30일)
nirwana
nirwana 2023년 5월 25일
답변: Walter Roberson 2023년 5월 25일
Can someone explain this to me? actually this is part od example in the numerical book
I found example in numerical book about ODE that can formulated by
dx/dt=v
dv/dt=g-cd/m*v*abs(v)
and used a M-fle function to express those system by
function dydt=freefall(t,y,cd,m)
%y(1)=x and y(2)=v
grav=9.81;
dydt=[y(2);grav-cd/m*y(2)*abs(y(2))];
then calculate ODE using ode45
opts=odeset('events',@endevent);
y0=[-200 -20];
[t,y,te,ye]=ode45(@freefall,[0 inf],y0,opts,0.25,68.1);
te,ye
plot(t,-y(:,1),'-',t,y(:,2),'--','LineWidth',2)
legend('Height (m)','Velocity (m/s)')
xlabel('time (s)');
ylabel('x (m) and v (m/s)')
i try to calculate ODE withouy making function m-file to perform ODE as shown below
func=@(dxdt) [dxdt;9.81-0.25/68.1*dxdt*abs(dxdt)];
[t,y,te,ye]=ode45(func,[0 inf],y0,opts);
but it doesn't work. Can anyone explain to me why or it should make a separete function to solde ode using ode45 in matlab?

채택된 답변

Rik
Rik 2023년 5월 25일
If you want to replicate the results, you need to replicate the function exactly:
func=@(t,y,cd,m) [y(2);9.81-cd/m*y(2)*abs(y(2))];

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 5월 25일
func=@(dxdt) [dxdt(2);9.81-0.25/68.1*dxdt(2)*abs(dxdt(2))]
However you will have problems when 0 is crossed.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by