How can I solve ODE without using nested functions?

I have some differential equations that I need to solve using MATLAB ODE solvers. While the differential equations themselves are quite simple, they depend on many "constants". These constants are not universal and must be provided by the caller.

An example of an ODE of this type:

dx/dt = -j * (k + x) ./ (l + x)

Where j, k and l are constants, and x is a variable.

The way I have solved this so far is to use a function that takes in all initial values ​​and all constant values ​​(about 10) as arguments, and then calls an internal "step" function that takes the shape vector that MATLAB expects for him ODE solvers. So that...

function [outputVector] = someFunction(x, y, j, k, l, m, n, o)
    function [output] = someFunctionStep(t, inputVector)
        x = inputVector(1);
        y = inputVector(2);
        dx = -j .* (k + x) ./ (l + x);
        dy = -m .* (n + y) ./ (o + y);
        output = [dx;dy]
    end
    outputVector = ode15s(@someFunctionStep, [0, endTime], [x,y]);
end

, , . , , : a) b) . , ?

+3
2

"" ODE, ( ). :

function odeFcn = makeODE(j,k,l,m,n,o)
  odeFcn = @(t,y) [-j*(k+y(1))/(l+y(1)); -m*(n+y(2))/(o+y(2))];
end

, . :

outputVector = ode15s(makeODE(a,b,c,d,e,f), [0,endTime], [x,y]);
+3

, , , , - FunctionStep. ode15s?

someFunctionStep, varargin input. . ode15s.

- Loren

+4

Source: https://habr.com/ru/post/1720178/


All Articles