Search code examples
matlabmatlab-figureodedifferential-equationssubplot

Odes15 sub plotting, differential equations


So I have three differential equations relating to diabetes, I have to plot the 2 out of the three, them being G and I in a subplot. For some reason when I attempt to run it the command window prints out: "Not enough input arguments" This is the criteria for it:

function dx = problem1(t,x)     
    P1 = 0.028735 ;
    P2 = 0.028344 ;
    P3 = 5.035 * 10^(-5) ;
    Vi = 12 ;
    n = 5/54 ;
    D_t = 3*exp(-0.05*t) ;
    U_t = 3 ;
    Gb = 4.5;
    Xb = 15;
    Ib = 15;

    G = x(1);
    X = x(2);
    I = x(3);

    dx = zeros(3,1);
    dx(1) = -P1*(G-Gb) - (X-Xb)*G + D_t ;
    dx(2) = -P2*(X-Xb) + P3*(I-Ib) ;
    dx(3) = -n*I + U_t/Vi ;

    [T,X] = ode15s(@problem1,[0 60*24],[4.5 15 15]) ;

subplot(3,1,1);
plot(T,X(:,1)); % Plot G
subplot(3,1,2); % Second subplot
plot(T,X(:,2)); % Plot I

Solution

  • The error is thrown when you run the function and MATLAB attempts to evaluate D_t = 3*exp(-0.05*t);. Since no value of t was given, MATLAB throws an error saying that the up-to-that-point unused t variable must be specified.

    The main problem with the code is in the function's design. Namely, ode15s needs a function that accepts a t and an x and returns dx; however, as it is currently laid out, the call to ode15s is embedded within problem1 which itself requires a t and x. It is a chicken-or-egg problem.

    All of the input is correct aside from this design problem and can easily be corrected using a separate function for the ODE's definition:

    function problem1
    
        [T,X] = ode15s(@ODE,[0 60*24],[4.5 15 15]) ;  
        subplot(3,1,1);
        plot(T,X(:,1)); % Plot G
        subplot(3,1,2); % Second subplot
        plot(T,X(:,2)); % Plot I
    
    end
    
    function dx = ODE(t,x)
        P1 = 0.028735 ;
        P2 = 0.028344 ;
        P3 = 5.035 * 10^(-5) ;
        Vi = 12 ;
        n = 5/54 ;
        D_t = 3*exp(-0.05*t) ;
        U_t = 3 ;
        Gb = 4.5;
        Xb = 15;
        Ib = 15;
    
        G = x(1);
        X = x(2);
        I = x(3);
    
        dx = zeros(3,1);
        dx(1) = -P1*(G-Gb) - (X-Xb)*G + D_t ;
        dx(2) = -P2*(X-Xb) + P3*(I-Ib) ;
        dx(3) = -n*I + U_t/Vi ;
    end
    

    Notes:

    • The first line function problem1 is short-hand for function [] = problem1(). I prefer the latter form myself, but I'm in the minority.
    • The function handle passed to ode15s @ODE is short-hand for @(t,x) ODE(t,x). I prefer the latter form myself, but it is no less or more valid as long as you are not parametrizing functions.
    • You can also use a nested function and give the problem1 function access to the model constants, but I opted for a separate function here.