56 views (last 30 days)
Nawal on 31 Jul 2013
Commented: Walter Roberson on 21 Jan 2019
I'm writing a program and I can't finish it because I'm keep getting these error below....
Error using odearguments (line 93)
MY_FUNCTION returns a vector of length 1, but the length of initial conditions vector is 2.
The vector returned by MY_FUNCTION and the initial conditions vector must have the same number of elements.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in p3 (line 10)
[ts,xs]= ode45(@my_function,[0,10],[5,0])
My script file :
function ys = my_function (t,y)
W= 50; % weight in lb.
g= 32.2 ; % acceleration due to gravity ( ft/s^2)
Mk=0.2; % no units
L= 12 ;% length in ft.
ys = (( 3 ) *( ( (1-(Mk^2))* g* sin(y(1))) - ( 2*Mk*g*cos(y(1))) + (Mk*((y(2))^2)))) / ( L*(2-(Mk^2)));
And the ode45 part is :
% problem # 3
W= 50; % weight in lb.
g= 32.2 ; % acceleration due to gravity ( ft/s^2)
Mk=0.2; % no units
L= 12 ;% length in ft.
[ts,xs]= ode45(@my_function,[0,10],[5,0])

Richard Brown on 31 Jul 2013
The error message says it all.
You're only computing one derivative in my_function. You need to define ys(1) and ys(2), because you refer to y(1) and y(2), and provide two initial conditions. You need to have the same number of derivatives as y values.
Jan on 1 Aug 2013
+1. Actually all you have answered is a rephrased version of the error message. But you hit the point exactly. The meta-message is a recommendation to read the messages carefully. And especially in the combination with Matlab, this is a very good hint.

Francisco Barber on 21 Jan 2019
Hello,
I tried defining y(1) and y(2) for a similar program and it continued failing.
Someone found a solution?
Walter Roberson on 21 Jan 2019
"You need to have the same number of derivatives as y values."