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 Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by