MATLAB: Not enough input arguments ode45

error ode45not enough input arguments

Hi everyone,
I have a problem with an ode45 function. Hier is my code:
function dx=num_lsg1(t,x)
dx=zeros(2,1);
dx(1)=k*x(2)-x(1)*(1-x(2));
dx(2) = (x(1)*(1-x(2))-kmm*x(2))/e;
%def parameters
k=0.625;
e=0.001;
kmm=1;
s0=1;
c0=0;
t=linspace(0,0.01,100);
x0=[s0 c0];
% Main program
[t,x]=ode45(@(t,x) num_lsg1(t,x), t, x0);
plot(x(:,1),'-o', x(:,2),'-x');
end
Hier is the arror message:
>> num_lsg1
Not enough input arguments.
Error in num_lsg1 (line 5)
dx(1)=k*x(2)-x(1)*(1-x(2));
Could someone help me to find the problem?
Thank you!

Best Answer

% Main program

s0=1;
c0=0;
t=linspace(0,0.01,100);
x0=[s0 c0];
% Main program
[t,x]=ode45(@(t,x) num_lsg1(t,x), t, x0); % function call
plot(t,x(:,1),'-o')
figure % plot it in separate figure because obviously the scales are different
plot(t, x(:,2),'-x');
function dx=num_lsg1(t,x) % function definition
%def parameters
k=0.625; % have t be defined before usage (that's why you had error)
e=0.001;
kmm=1;
dx=zeros(2,1);
dx(1)=k*x(2)-x(1)*(1-x(2));
dx(2) = (x(1)*(1-x(2))-kmm*x(2))/e;
end
Screen Shot 2018-12-14 at 12.38.17 PM.png
Screen Shot 2018-12-14 at 12.38.27 PM.png