Search code examples
matlabfunctionodefunction-handle

Errors when using functions with multiple inputs


I am trying to evaluate a duffing oscillator in MATLAB with multiple inputs, and I am getting an odd error I was hoping someone might be able to help me with.

Here is the code:

% file duffing.m

function dy=duffing(t,y,a,b,d,g,w) 
    dy=[y(2); -a*y(1)^3-b*y(1)-d*y(2)+g*cos(w*t)];
end

And the file that calls the duffing function:

t=0:0.01:100;

%duffing oscillator 

y0=[2,0];
a=1;
b=-1;
d=0.2;
w=1;
g=0.1;

% duffing oscillator
[t,y]=ode45('duffing',t,y0,a,b,d,g,w); 

When I run it, I get the following error:

Error using odearguments (line 92) DUFFING returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned by DUFFING and the initial conditions vector must have the same number of elements.

But when I define the function duffing as

function dy=duffing(t,y) 

    a=1;
    b=-1;
    d=0.2;
    w=1;
    dy=[y(2); -a*y(1)^3-b*y(1)-d*y(2)+g*cos(w*t)];

end

and pass in

[t y]=ode45('duffing',t,y0);

with the same y0 as above, it runs fine and I can plot it.

What am I doing wrong?


Solution

  • ode45 takes as input a function of two variables only. If you want to pass more inputs, you have to create a little anonymous function, like this:

    a=1;
    b=-1;
    d=0.2;
    w=1;
    g=0.1;
    
    % duffing oscillator
    [t,y]=ode45(@(t,y) duffing(t,y,a,b,d,g,w),t,y0);