Search code examples
matlabsimulinkodefunction-handleundefined-function

Trying to solve ODE system in MATLAB yields the following error: "Undefined function 'exist' for input arguments of type 'cell'"


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!


Solution

  • 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.