express ODE without set a m file function

1 view (last 30 days)
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?

Accepted Answer

Rik
Rik on 25 May 2023
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))];

More Answers (1)

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

Tags

Community Treasure Hunt

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

Start Hunting!