Search code examples
matlabfunctionhandle

Passing parameters in a MATLAB function handle


I have to solve my differential equation with ode45:

[t, phi]=ode45(@diff_phi, time_period, initial);

where diff_phi is my function:

function dphi_dt = diff(t,phi,gamma0,gamma1,L0,L1)
phi0=phi(1);
phi1=phi(2);

dphi0_dt=-2*L0*phi0-gamma0*(phi0^2+1);
dphi1_dt=-2*L1*phi1-gamma1*(phi1^2+1);

dphi_dt=[dphi0_dt; dphi1_dt];
end

This way is not working:

[t, phi]=ode45(@(t,phi) diff_phi(gamma0,gamma1,L0,L1), time_period, initial);

because I got this error message:

Attempted to access phi(2); index out of bounds because numel(phi)=1.

Error in diff_phi (line 18)
phi1=phi(2);

What should I do?


Solution

  • diff_phi still expects 6 parameters, so in your second method, try:

    [t, phi]=ode45(@(t,phi) diff_phi(t,phi,gamma0,gamma1,L0,L1), time_period, initial);