Search code examples
variablesdifferential-equations

How to change constant value w.r.t to time in Matlab?


I am solving a 1st order differential equation in matlab.

dp/dt=q/c 

where c value is constant 55 but q value is changing with respect to time i.e from 0 to 1 second q=500 and 1 to 2 sec q=0.

I have made this program in Matlab:

when plotting a graph between t and y it is giving straight line (linear,directly proportional).

function dq =myode45function(t,y)

    Q=500

    C=55;

    dq=Q/C;

end

How can i tell matlab that q value is changing w.r.t time.


Solution

  • You don't. At least not if you want to use ode45 since the step size regulator requires that the ODE function be very smooth, and a jump in the ODE function is the opposite of that.

    Using a fixed step size method you may use conditionals or step functions to get the effect:

    function dq =myode45function(t,y)
        Q=500
        if t>1 and t<2 then Q=0 end
        C=55;
        dq=Q/C;
    end
    

    However, for variable step size integrate the pieces between the jumps separately using anonymous functions to specialize a generic ODE function with parameters.

    Or use events to force the integrator to pass exactly through the jump point.