express ODE without set a m file function

1 次查看(过去 30 天)
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