0

I'm having a bit of difficulties when I try to solve an ODE system of two equations in MATLAB.

The code I am using is the following:

x0=-1;              %Initial condition for variable x
y0=-10;             %Initial condition for variable y
dx=@(t,x,y) y+2.*t; %First ODE
dy=@(t,y) y;        %Second ODE
f={dx;dy};          %Array that contains the first and second ODE's

[t,g]=ode15s(f,[0 1],[x0 y0]);  %Call for ode15s solver

When I execute this code, i get the following error:

Undefined function 'exist' for input arguments of type 'cell'.

I don't want to create a function of the style

function f=myodes(t,x,y)
etc etc
end

Because this code is intended to be nested in a function code, which is then going to be inserted in a MATLAB Function block in Simulink, which needs to use, as inputs, the outputs of other blocks in the Simulink file.

I cannot do it directly on Simulink because that code is actually a practice for a larger set of equations that I need to solve, where the independent variable is not time but distance.

Any help would be appreciated.

Thanks!

elporsche
  • 43
  • 1
  • 5
  • 1
    And what makes you think you cannot use a function of the form you propose? Take a look at the docs on [local functions](http://www.mathworks.nl/help/matlab/matlab_prog/local-functions.html). – Rody Oldenhuis Jan 20 '14 at 15:02
  • Note that Simulink solves the differential equations that the system represents at each time step, so there's no need to (and you shouldn't) call `ode15s` and try to solve the differential equations within the MATLAB Function block. – am304 Jan 20 '14 at 15:56
  • @Rody Oldenhuis: I took a look at the Nested functions section of the MATLAB help but I believed that this could be done without the need of Nested functions and with less code lines. And your answer proves my point. – elporsche Jan 20 '14 at 23:08
  • @am304: I cannot let Simulink sole my differential equations because my independent variable is not time (as is the ind. variable taken by Simulink); in my code, time controls the inputs to the block but not the operations executed inside it. I'm just curious if this is going to work in the Simulink environment the way I intend to. Thanks for the input. – elporsche Jan 20 '14 at 23:08
  • @elporsche But `ode15s` solve differential equations with respect to time? – am304 Jan 21 '14 at 09:33

1 Answers1

1

Make the substitution

z ≣ [x; y]

such that

dz ≣ [dx; dy]

You'd implement this as

x0 = -1;    %// Initial condition for variable x
y0 = -10;   %// Initial condition for variable y

%// The ODEs. Note that z ≣ [x; y]
f = @(t,z) [
    z(2)        %// First ODE
    z(2)+2*t];  %// Second ODE

%// Call for ode15s solver
[t,g] = ode15s(f, [0 1], [x0 y0])

Only thing left to do is properly demux your outputs.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96