Search code examples
matlabodematlab-deploymentdifferential-equations

Passing arguments in ode45


I want to pass a simple argument in my ode45 function. My function is as follows:

function dxdt = state( t,x,vgth,vgval)
p=1;
k=10^0.7;
window1=1-((2*x)-1).^(2*p);
dxdt=k*(vgval-vgth+1.2)*window1;  
end

The main script is:

clear all
step=0.01;
t = 0:step:10;
f=2*0.157;
vg = 5*sin(2*f*t);
x0=0.01;
vgth=1.9;
[t,x] = ode45(@(t,x) state1 (t,x,vgth,vg(t)), t, x0);
plot(t,x)

Here When I exclude the parameter vg(t) in the argument passing, the whole works just fine. But It doesn't work with vg(t) in the argument.


Solution

  • Assembling the comments to an answer:

    function dxdt = state( t,x,vgth,vgval)
        p=1;
        k=10^0.7;
        window1=1-((2*x)-1).^(2*p);
        dxdt=k*(vgval-vgth+1.2)*window1;  
    end
    
    function vgval = vg(t)
        f=2*0.157;
        vgval = 5*sin(2*f*t);  
    end 
    
    step=0.01;
    t = 0:step:10;
    x0=0.01;
    vgth=1.9;
    [t,x] = ode45(@(t,x) state(t,x,vgth,vg(t)), t, x0);
    

    This should hopefully work as intended.