Search code examples
matlabmathdifferential-equationsodeequation-solving

How to express and solve this differential equation system in Matlab?


The equations looks like this:

dxi(t)/dt = -c * xi(t) * yi(t)

dyi(t)/dt = a * Σ{i=1 to n}(xi(t) * yi(t)) + xi(t) * yi(t - 1) + b

where a, b and c are whatever constant values you want, for example a=1, b=2, c=3.

Σ{i=1 to n}(xi(t) * yi(t)) means summation from i=1 to n, for example n=3: x1(t)*y1(t) + x2(t)*y2(t) + x3(t)*y3(t)

So, how can I express & solve this using matlab?


Solution

  • You need to build what is called a delayed differential equation. I was about to explain how to do, but then I found this wonderful tutorial to do just that. Example 1 is basically what you need.

    The only extra caveat is that you should incorporate dx/dt and dy/dt into the same set of differential equation

    Let me know if you need more help

    Edit: keep it in one file

    function  dYdt = ddefun(t,Y,Z)
        % assume Y = [x;y]
        x = Y(1:n); % 2n is the size of Y. this step is unnecessary ...
        y = Y(n+1:2*n); % but helps visualize what is happening
    
        ytau = Z(:,1);
    
        dYdt(1:n) = -c*x.*y;
        dYdt(n+1:2*n) = a*dot(x,y) + x.*ytau + b
    
    end