I am trying to write this function for ode. But it always gives me an error as not enough input arguments. Here is my code associated:

I am trying to write this function for ode. But it always gives me an error as not enough input arguments. Here is my code associated:
function dxdt = odefcn(x,a)
dxdt = zeros(3,1);
dxdt(1) = x(2);
dxdt(2) = x(3);
dxdt(3) =-a*x(3)+x(2)-x(1);
end

 Accepted Answer

Do not use
ode45( odefcn, .....)
Use
ode45( @odefcn, .....)

5 Comments

Actually i am writing this as a function for my ODE. But this function doesn't run. It always give the same error as not enough input arguments.
Here's my main code:
a=2
tspan = [0 5];
[t,x] = ode45(@(t,x) odefcn(t,x,a), tspan, 0);
figure;
plot(t,x);
grid on;
With that code you should be getting an error about too many input arguments, not about not enough input arguments.
You should be using
[t,x] = ode45(@(t,x) odefcn(x,a), tspan, 0);
I did tried the similar code line which you have written here. Still, my error is same. "Error using odefcn (line 3) Not enough input arguments." I dont know what's wrong with my line 3.
a=2
tspan = [0 5];
X0 = ones(1,3);
[t,x] = ode45(@(t,x) odefcn(x,a), tspan, X0);
figure;
plot(t,x);
grid on;
with
function dxdt = odefcn(x,a)
dxdt = zeros(3,1);
dxdt(1) = x(2);
dxdt(2) = x(3);
dxdt(3) =-a*x(3)+x(2)-x(1);
end

Sign in to comment.

More Answers (0)

Tags

Asked:

on 2 Sep 2017

Commented:

on 2 Sep 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!