Solving ODE45 equations in matlab without a function

I am struggling to figure out what is wrong with my code please help.
%%Numerically solving for Trajectory of the Ball in a Vacuum
clc
v0=25;
% Average football is punted at 25 m/s
theta=35;
% Chosen value for the angle of a punt
g = 9.8;
t=2.5;
t2 = 1.25;
CF = [.3 .385];
y0 = 0;
xdist = v0*cosd(35)*t;
yheight = (-.5*(g*(t2^2)))+v0*sind(theta)*t2;
% A ball punted with these variables would travel 51.2 meters horizontally
% and the max height would be 10.27 meters
x = zeros(1,2);
y0 = zeros(1,2);
t = [0 2.5];
[t,x] = ode45(@(t,x) (v0*cos(theta)*exp(-CF*t)), t, y0)
--------------------------------------
and I am receiving the errors:
Error using odearguments (line 90)
@(T,X)(V0*COS(THETA)*EXP(-CF*T)) must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

 Accepted Answer

It is stated that "The function dydt = odefun(t,y), for a scalar t and a column vector y, must return a column vector dydt" (See ode45 odefun)
[t,x] = ode45(@(t,x) (v0*cos(theta)*exp(-CF*t))', t, y0)
(added ' at the end of the function definition) would not cause an error, but could you check if you get the expected solution?

3 Comments

I would suggest .' instead of ' as you do not want the complex conjugate transpose.
You are right. Thanks for pointing that out.
[t,x] = ode45(@(t,x) (v0*cos(theta)*exp(-CF*t)).', t, y0)
instead.
Actually nevermind this equation is for acceleration so this does appear to be correct. Thank you so much.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!