Search code examples
matlabdifferential-equations

How to solve a differential equation with MATLAB


i have a problem. that is :

y"^2 + 2*y'+ 3*y = sin(x), y'(0)=0, y(0)=1

I want solve this problem with MATLAB but I can't.

Can you help me ?


Solution

  • First, you have to reduce the order. Let z = y' => z' = y"

    Your ODE then becomes

    z' = sqrt(-2*z - 3*y + sin(x)), with z(0) = 0
    y' = z, with y(0) = 1
    

    You can now write a function in MATLAB to represent this ODE: (where M = [ z y ]')

    function dMdx = odefunc(x,M)
        z = M(1);
        y = M(2);
        dMdx(1) = sqrt(-2*z - 3*y + sin(x));
        dMdx(2) = z;
    end
    

    You can then call this function as follows:

    M0 = [ 0 1 ];   % Initial values of ODE
    tfinal = 12;     % Final integration time
    [x,M] = ode45(@odefunc,[0 tfinal],M0)   % Integration using the RK-45 algorithm