MATLAB: Error when using ode45 command,”returns a vector of length 11, but the length of initial conditions vector is 1.”

errorfunctionMATLABode45vector

function dw_dt = equation_(t,w)
a = 1;
b = 1.35;
v= [.6:.01:.70].';
dw_dt = a*w.^(v)-b*w;
%actual script
time_period = [0 20];% time period between 0 and 20 weeks
w0 = 0.5;% intial weight of the fish
[t,w] = ode45('equation_',time_period,w0);
I'm trying to run my function using ode45. I should be able to run the function for every value of yv but for some reason i can't. when ever i try to run the function for once value of V is works perfectly fine but when i try to run it for every value of v it does not work well.

Best Answer

Hello,
The way the ode45 command works (as I'm sure you know) is that given a state vector, in your case w, you provide a function that evaluates the derivative of w and returns dw.
A common example is with position and velocity:
state_vec = [position; velocity];
where as your 'equation_' would return:
dstate_vec = [velocity; acceleration];
it seems like you understand this; however, you are giving it an initial condition of .5, so:
w = .5;
then you are returning a dw that is a vector (because v is a vector and is apart of the computation).
So ode45 is trying to update a constant .5 with a vector dw, this is where you are encountering your error. You can fix this by either changing your dw to a scalar result, or making your initial condition an equally long vector corresponding to the length of the returned dw.
Hope this helps!
Related Question