Olivia
Olivia

Reputation: 37

Too Many Input Arguments Matlab ode45

I am trying to write a simple Matlab code to model a projectile. Whenever I try to run the code I get an error saying there are too many input arguments. I am running the code with

model1(44.7, 45)

function[] = model1(vel, angle)
close all; 
tspan = [0 3];
x0 = [0; 0.915; vel*cos(angle); vel*sin(angle)];
[x] = ode45(@ball, tspan, x0);

function xdot = ball(x)
g = 9.81; 
xdot = [x(3); x(4); 0; -g];

end 
end

Error using model1/ball
Too many input arguments.

Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

Error in ode45 (line 115)
    odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options,
    varargin);

Error in model1 (line 9)
[x] = ode45(@ball, tspan, x0);

I'd appreciate any advice!

Upvotes: 0

Views: 2384

Answers (1)

Zoltan Csati
Zoltan Csati

Reputation: 699

The error was (what I also committed many times in the past) that you have to pass the independent variable (time, in this case) too.

function [t, x] = model1(vel, angle)
tspan = [0 3];
x0 = [0; 0.915; vel*cos(angle); vel*sin(angle)];
[t, x] = ode45(@ball, tspan, x0);

end

function xdot = ball(t,x)
g = 9.81; 
xdot = [x(3); x(4); 0; -g];
end

I modified your code to return the solution and the corresponding time steps. Moreover, I removed ball to be a nested function.

Upvotes: 2

Related Questions