Search code examples
matlabodedifferential-equations

How can I get around this error using deval with dde23 in MATLAB?


I am trying to plot a graph between k(t) and k(t-1) of the solution of my delay differential equation. I am pasting the code:

% Defining the variables:
X = 0.2;
M_C = 1.523;
a = 15;

sol = dde23(@(k,t,KL) new_func(k,t,KL,X,M_C,a),1,0.5, [1,10] );

t = linspace(1,10);
p_t = deval(sol,t);
p_lagt = deval(sol,t-1);

% Stability boundary for M/C: 
M_C_Stab = 1.523;

function dk = new_func(k,t,KL,X,M_C,a)
    dk = X*((1-M_C*(1-KL^a))/(M_C*(1-KL^a)))*k;
end

This is the error I am getting:

>> new_defined_function
Error using deval (line 132)
Attempting to evaluate the solution outside the interval [1.000000e+00, 1.000000e+01] where it is defined.


Error in new_defined_function (line 12)
p_lagt = deval(sol,t-1);

Example 2 in this tutorial is what I am referring to but even that code is giving an error. Maybe because its old as ddeval which has been used here has already become deval. I am fairly new to MATLAB and have no idea how to resolve this error. If anyone could help, I'll be more than grateful.


Solution

  • The error is caused by

    p_lagt = deval(sol,t-1)
    

    t-1 is equivalent to linspace(0,9), and so trying to evaluate the solution at time 0 causes an error as the solution was calculated on the time interval [1,10].

    You could fix the error by changing the p_lagt so that it is evaluated on an interval contained in [1,10], for example

    p_lagt = deval(sol,max(t-1,1))
    

    Note in the example you linked to, the solution is calculated on the time interval [0,100], while t = linspace(2,100). To mirror this in your code try

    sol = dde23(@(k,t,KL) new_func(k,t,KL,X,M_C,a),1,0.5, [0,10] ); % note change in time interval
    
    t = linspace(1,10);
    p_t = deval(sol,t);
    p_lagt = deval(sol,t-1);