Clear Filters
Clear Filters

Not Enough Input Arguments in ODE Solver

5 views (last 30 days)
Neil Solan
Neil Solan on 26 Feb 2018
Edited: Adam on 27 Feb 2018
I'm getting the error that there are not enough input arguments in my ODE function. However, every variable in the equation is listed as an input. I thought that was the solution but clearly it's not. Here's my function file:
function dtheta = nonlin_function(t,t_1,theta,m,M,L,s,c,k,c_t,k_t,F_0)
if t>=(ceil(t/t_1)-1)*t_1 && t<=(ceil(t/t_1)-1)*t_1+t_1
F = (F_0/t_1)*(t-(ceil(t/t_1)-1)*t_1);
elseif t>=(ceil(t/t_1)-1)*t_1
F = F_0;
end
dtheta = zeros(2,1);
dtheta(1) = theta(2);
dtheta(2) = ( 1/((m/L)*((((L-s)^3)/3)+((s^3)/3))-M*s^2)*(F-((c*(L-s)^2*(cos(theta(1))*cos(theta(1)))+c_t)*theta(2))-(k*(L-s)^2*sin(theta(1))*cos(theta(1)))-(k_t*theta(1))));
And here is the actual .m file:
clear
clc
close all
%Constants:
g = 9.81; %[m/s^2]
M = 0.2; %[kg]
m = 0.8; %[kg]
k = 1; %[N/m]
c = 0.01; %[N*s/m]
L = 0.15; %[m]
s = 0.05; %[m]
c_t = 10^-3; %[N*m*s/rad]
k_t = 0.5; %[N*m/rad]
F_0 = 5; %[N]
t_1 = 0.25; %[s]
theta_0 = pi/18; %[rad]
theta_dot_0 = -0.3; %[rad/s]
%Effective Values:
J_eff = ((m/L)*((((L-s)^3)/3)+((s^3)/3))-M*s^2);
K_eff = (k*(L-s)^2+k_t);
C_eff = (c*(L-s)^2+c_t);
omega_n = sqrt(K_eff/J_eff);
c_tc = 2*J_eff*omega_n;
zeta = C_eff/c_tc;
%ODE Solutions:
t_i = 0; %Initial Time [s]
t_f = 3; %Final Time [s]
t_force = 0:0.001:3;
%Non-Linear Equation Solution:
for i=length(t_force)
if t_force(i)>=(ceil(t_force(i)/t_1)-1)*t_1 && t_force(i)<=(ceil(t_force(i)/t_1)-1)*t_1+t_1
F1(i)=(F_0/t_1)*(t_force(i)-(ceil(t_force(i)/t_1)-1)*t_1);
elseif t_force(i)>=(ceil(t_force(i)/t_1)-1)*t_1
F1(i)=F_0;
end
end
[T1,X1] = ode45(@(t,theta)nonlin_function(t,t_1,theta,m,M,L,c,k,c_t,k_t,F_0),[t_i t_f],[theta_0 theta_dot_0]);
%Linear Equation Solution:
for i=length(t_force)
if t_force(i)>=(ceil(t_force(i)/t_1)-1)*t_1 && t_force(i)<=(ceil(t_force(i)/t_1)-1)*t_1+t_1
F2(i)=(F_0/t_1)*(t_force(i)-(ceil(t_force(i)/t_1)-1)*t_1);
elseif t_force(i)>=(ceil(t_force(i)/t_1)-1)*t_1
F2(i)=F_0;
end
end
[T2,X2] = ode45(@(t,theta)nonlin_function(t,t_1,theta,m,M,L,c,k,c_t,k_t,F_0),[t_i t_f],[theta_0 theta_dot_0]);
%State Space Solution:
[A] = [0 1; -omega_n^2 -2*zeta*omega_n];
[B] = [0;1/J_eff];
[C] = [1 0];
[D] = zeros;
Theta_0 = [theta_0 theta_dot_0];
system = ss(A,B,C,D);
[V_m,D_m] = eigensort(A);
k=0;
for j=1:size(V_m,2)/2
k=k+1;
Mode_Shape(:,k)=V_m(length(A)/2+1:length(A),j)/norm(V_m(length(A)/2+1:length(A),j));
end
omega_n_ss = abs(D_m);
zeta_ss = -real(D_m)./omega_n_ss;
t = 0:0.01:3;
[Y_total,t_total,X_total]=lsim(system,F2,t,Theta_0);
I'm assuming I'll also have the same problem for the linear function part of this so here is that file as well just in case it's needed:
function dtheta = lin_function(t,t_1,theta,m,M,L,s,c,k,c_t,k_t,F_0)
if t>=(ceil(t/t_1)-1)*t_1 && t<=(ceil(t/t_1)-1)*t_1+t_1
F = (F_0/t_1)*(t-(ceil(t/t_1)-1)*t_1);
elseif t>=(ceil(t/t_1)-1)*t_1
F = F_0;
end
dtheta = zeros(2,1);
dtheta(1) = theta(2);
dtheta(2) = (1/((m/L)*((((L-s)^3)/3)+((s^3)/3))-M*s^2)*(F-((c*(L-s)^2)+c_t)*theta(2))-((k*(L-s)^2+k_t)*theta(1)));
How do I fix this error? Thank you in advance.

Answers (1)

Adam
Adam on 26 Feb 2018
You only call the function with 11 input arguments, the function definition takes 12, which in itself is far too many in general, but that is a different discussion!
You get the 'Not enough input arguments' error when the function runs and tries to use an argument that was not passed in. In this case that would be the final F_0 argument. That is one that you pass in, but you must be missing another one. I'll leave you to discover which one is missing.
  2 Comments
Neil Solan
Neil Solan on 26 Feb 2018
So if a lot of those arguments are constants, should I remove them from the inputs and just list them below as what their values are? In which case, how many input arguments do I need? You said it takes 12, but what if I have less?
Adam
Adam on 27 Feb 2018
Edited: Adam on 27 Feb 2018
If they are used in the function then they must be defined and if they aren't defined within the function you must pass them in so if you defined a function that takes 12 arguments you have to pass in 12 if they all get used. You can have optional arguments, but only if they are handled and usually the ones at the end.
If they are constants throughout all usages can't you just define them in the function?
Alternatively you could add them all to a struct that you pass in to at least make it neater.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!