ode solver returns too many results
显示 更早的评论
I am trying to solve a system of ode's with ode15s but it is not working properly. It is returning a vector of length 9 for my system of 3 ode's instead of 3 as I desire. My system looks like this
function D = mo(t,y,a,b)
dy = a*y;
dv= (a*t+1)*y;
dw = (a/b)*y;
D = [dy; dv; dw];
And this is how I am trying to solve it
x0(1) = 0.2;
x0(2)= 4;
ts = [0,1,2,3,4,5];
options = odeset('RelTol',1e-8,'AbsTol',1e-8);
[~,y] = ode15s(@(t,y) mo(t,y,x0(1),x0(2)),ts,[x0(2) 0 1],options);
I have no clue why I get this error
Error using odearguments (line 95)
@(T,Y)MO(T,Y,X0(1),X0(2)) returns a vector of length 9, but the length of initial conditions vector is 3. The vector returned by
@(T,Y)MO(T,Y,X0(1),X0(2)) and the initial conditions vector must have the same number of elements.
Edit1: I had a typo in dw
Edit2: Just for clarification the result I want to get can be retrieved with wolfram alpha:

采纳的回答
更多回答(1 个)
Bjorn Gustavsson
2020-7-23
When you get an error in matlab instantly set the debugger on:
dbstop if error
then rerun the command that caused the error, this will give you an interactive command-line prompt at the line where the error happened in the function, in the stack of function-calls. This makes it possible to inspect the different variables in the function-workspace, and move up in the call-stack so that you can inspect the input arguments and such.
In this case you problem is that when ode15s calls the function mo it starts off with your initial conditions, converted into a column array - that is a 3 x 1 array. In the function you first calculates dy which will be a 3 x 1 array, then dv which will be a 3 x 1 array and then dw which also will be a 3 x 1 array, then these 3 arrays are concatenated to produce a 9 x 1 array.
HTH
类别
在 帮助中心 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



