3 views (last 30 days)

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:

Star Strider
on 23 Jul 2020

The problem is that ‘y’ is a (3x1) vector, and ‘mo’ is evaluating each value for all values of ‘y’.

Perhaps you intend:

function D = mo(t,y,a,b)

dy = a*y(1);

dv= (a*t+1)*y(2);

dw = (a/b+1)*y(3);

D = [dy; dv; dw];

end

or as an anonymous function:

mo = @(t,y,a,b) [a*y(1); (a*t+1)*y(2); (a/b+1)*y(3)];

.

Star Strider
on 23 Jul 2020

‘How can I avoid matlab to assume y is a (3x1) vector?’

You cannot. That is how the MATLAB ODE solvers work.

‘The ode I am trying to solve has the solution with dy being the time derivative and dv dw being the sensitivities for a and b’

I may be missing something here.

Using:

syms a b t y(t)

Eq = y == b*exp(a*t);

dEq = diff(Eq)

dEqda = diff(Eq,a)

dEqdb = diff(Eq,b)

I get:

The last two are the sensitivity functions of ‘a’ and ‘b’ (at least as I understand sensitivity functions).

What is your original differential equation?

.

Bjorn Gustavsson
on 23 Jul 2020

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

Opportunities for recent engineering grads.

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

Start Hunting!
## 0 Comments

Sign in to comment.