'Not enough input arguments' while trying to create a user-defined function

1 次查看(过去 30 天)
Hello guys,
I was trying to create my own function but apparently when I try to use it it returns the error:
Error using f (line 37) Not enough input arguments.
The following is my MATLAB code:
function [system] = f(t,p0,p1,m2,p2,m3,p3,m4,p4)
%Pipe Constants%
d = input('Enter pipe diameter ');
LLL = input('Enter pipe length ');
n = input('Enter number of lumps ');
rho = input('Enter fluid density ');
c = input('Enter fluid sound speed ');
mu = input('Enter fluid viscosity ');
%Calculate system's lump length, bulk modulus, area, compliance, resistance,inertance%
l = LLL/n;
B = c^2*rho;
a = pi*(d/2)^2;
CC = (rho*a*l)/B
RR = (32*mu*l)/d^2;
LL = l/a;
%Boundary Condition Inputs% %For open BC, choose a high compliance and the
%opposite for closed BC%
BC0 = input('Enter inlet compliance ');
BC1 = input('Enter outlet compliance ');
%Matrix Definitions%
C0 = 1/BC0;
C1 = 1/BC1;
R = RR/LL;
L = 1/LL;
C = 1/CC;
%System Matrix%
[system] = [0,0,0,0,0,0,0,0;0,0,-C,0,0,0,0,0;0,L,-R,L,0,0,0,0;0,0,C,0,-C,0,0,0;0,0,0,L,-R,-L,0,0;0,0,0,0,C,0,-C,0;0,0,0,0,0,L,-R,L;0,0,0,0,0,0,C1,0]*[p0;p1;m2;p2;m3;p3;m4;p4]...
+[-C0,0;0,C1;0,0;0,0;0,0;0,0;0,0;0,0]*[sin(t);sin(t)]
Thank you very much for your help!!!
Gus

回答(1 个)

Walter Roberson
Walter Roberson 2013-6-10
You are probably trying to start it by pressing the Run key (F5) or clicking on "Run" in the menus. If you do either of those, it will start the function but without passing anything in, which would lead to that error.
You need to start that function from the command line (or from another function or script) and supply values for all 9 parameters. For example
f(14, 3, 1/2, 55, pi, exp(sqrt(-1)), 65535, -sqrt(3), 2)
  2 个评论
Gustavo
Gustavo 2013-6-10
Hi Walter,
Thanks for your response. Well, after defining this function I am trying to input it to the ode45 function with the following code:
clear all
time = input('Time range in the following format [t0 t] ');
ics = input('Initial conditions for p0,p1,m2,p2,m3,p3,m4,p4 ');
[t,solutions] = ode45(@f,time,ics);
and it still does not work. Any ideas?
Walter Roberson
Walter Roberson 2013-6-10
Are those 8 values to be considered constants for any one ode run, or are they values being refined along the way for which "ics" is the initial conditions?
You have declared your function to take 9 arguments, but ode45 will only be passing in 2 arguments: http://www.mathworks.com/help/matlab/ref/ode45.html
[T,Y] = solver(odefun,tspan,y0) with tspan = [t0 tf] integrates the system of differential equations y′ = f(t,y) from time t0 to tf with initial conditions y0. The first input argument, odefun, is a function handle. The function, f = odefun(t,y), for a scalar t and a column vector y, must return a column vector f corresponding to f(t,y).
Thus if your p* and m* are values being refined (or couplings or the like) then they must be bundled together into a single vector "y"... exactly like your "ics" vector but your function f() needs to be recoded to expect a single parameter y rather than 8 individual variables. Your first line of code could then be
p0 = y(1); p1 = y(2); m2 = y(3); .... p4 = y(8);
If, on the other hand, you are treating some of them as constants for the run, then see http://www.mathworks.com/help/matlab/math/parameterizing-functions.html

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Fluid Dynamics 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by