Can someone explain why I am getting the error "Not enough input arguments (line 4)"?

104 vues (au cours des 30 derniers jours)
function [f] = f(x,y)
%PROBLEM3a
f = 1/(3*x-2*y+1); %line 4
y(1) = 0;
x(1) = 0;
step_sizeh = .1;
n_steps = 1./(step_sizeh);
for i = 1:n_steps;
y(i+1) = y(i) + step_sizeh*f(x(i),y(i));
x(i+1) = x(i)+step_sizeh;
end
end
  3 commentaires
Torsten
Torsten le 25 Nov 2023
Modifié(e) : Torsten le 25 Nov 2023
Did you supply numerical values for a, b, M, N, w_state and x_n before you tried to call the function "iirFilt" ? If not, the function does "not have enough input arguments" to perform any kind of computation.
Walter Roberson
Walter Roberson le 25 Nov 2023
Modifié(e) : Walter Roberson le 25 Nov 2023
When you have a variable named as a parameter in the function definition (such as x_n in this case), then MATLAB will never search any other environment to try to find a value for the variable. If you execute iirFilt without passing anything in, then when the code needs to use a variable named as a parameter but no value has been passed in, then MATLAB will generate an error message. MATLAB will never go looking in the base workspace or in the calling function for x_n if x_n is named as a parameter in the function definition. This is true even in the case of nested functions with shared variables: if you do not pass something in then it is an error to try to use the variable.
Likewise, if you have a name as a parameter in a function definition, then MATLAB will never search for the name as a function. Any named parameter in a function definition is "locked in" to being a variable (not a function) in the body of the function. You can see in the below example that even though you clear the definition of sum as being a variable, it got locked in as being a variable anyhow.
If you click on the green Run button in the editor, then that is equivalent to executing the function without passing anything in to the function: MATLAB will not look for a variable named x_n anywhere to pass the value in: the Run button is the same as executing iirFilt() with no parameters.
test_it
Not enough input arguments.

Error in solution>test_it (line 4)
sum(1:3)
function test_it(sum)
clear sum
sum(1:3)
end

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 26 Juil 2012
The first possiblity that comes to mind is your using ‘f’ for both your functon name and the variable inside the function. I suggest for a start:
function [z] = f(x,y)
Then since you also seem to call ‘f(x,y)’ recursively in this line:
y(i+1) = y(i) + step_sizeh*f(x(i),y(i));
I suggest you use an anonymous function here:
%PROBLEM3a
fcn = @(x,y) 1/(3*x-2*y+1); %line 4
z = fcn(x,y);
and change the ‘y(i+1)’ line to:
y(i+1) = y(i) + step_sizeh*fcn(x(i),y(i));
I also suggest you not use ‘i’ or ‘j’ as loop index variables because MATLAB uses these for its imaginary operators.
  2 commentaires
Zuleyka
Zuleyka le 26 Juil 2012
Thanks for all your advice. If you hadn't written the actual, I'm not quite sure I would have understood all the points since I'm not very familiar with Matlab.
Like what is an anonymous function, and is fcn a command? Anyway, the code works and I learned some new things. Thanks!
Star Strider
Star Strider le 26 Juil 2012
Anonymous functions ( http://www.mathworks.com/help/techdoc/math/bsgprpq-3.html ) are definitely worth learning about! They can make MATLAB much easier. (I also suggest you look up and learn about ‘inline’ functions. They also have their uses.)
The variable I called ‘fcn’ is just that — there is nothing special about the name. Here, it becomes the anonymous function I assigned it to, but it could be anything and I could have named the function almost anything except for ‘f’ because it refers to your function, or other variable names you used, or a number of MATLAB reserved words and commands.
A very easy way to find out if a name is ‘safe’ or a reserved word is to right-click on it in the Editor. A Help window will appear (or change if it is already open) to give you informaton on the function if it exists, or to say it cannot find anything about that function if it is not a MATLAB function. This is also a shortcut way to find documentation on a function if you need it in a hurry. It is an extremely useful feature.
You will quickly become quite knowledgable in MATLAB. It is usually easy to find code problems because we have all made many of those same errors. Be sure to share your knowldege of MATLAB with others who are learning.
Thank you for accepting my answer.

Connectez-vous pour commenter.

Plus de réponses (5)

Diego Tasso
Diego Tasso le 26 Juil 2012
Your getting that error because your compiling a function that does not have x or y defined. Its a normal error that occurs when the function is compiled. Just try calling the function from the command window and make sure to input variables for x and y ( i.e. in command window type "f(2,4)"). Another error statement is going to pop up when you execute line 10. That's your real problem.Also think about using "ii" instead of "i" since "i" is an already defined variable in MATLAB and may give you problems. Change the function name. I believe its confusing if not erroneous to use the same name for your function and for your output variable.
  2 commentaires
Diego Tasso
Diego Tasso le 26 Juil 2012
Modifié(e) : Diego Tasso le 26 Juil 2012
Sorry I forgot to tell you to get rid of your initial error statement move lines five and six above line four. As for what your trying to do, I am unsure of.
Zuleyka
Zuleyka le 26 Juil 2012
Modifié(e) : Zuleyka le 26 Juil 2012
Thanks for all the details! I did't know that "i" was already defined since my professor usually uses it in examples not pertaining to imaginary numbers. I also took your advice on the file name.
Oh, I'm making a loop for forwards euler, since our professor doesn't want us to use ode45.

Connectez-vous pour commenter.


Yanai
Yanai le 26 Juil 2012
You use the same name for a variable and for a function ("f") When you write f = .... matlab thinks you're trying to call the function f(x,y)

muhammad irfan
muhammad irfan le 24 Fév 2019
Modifié(e) : Stephen23 le 24 Fév 2019
EDIT: copyright code removed.
when i will run this program i face the error like this
(Error using mipbackwarddiff (line 17)
Not enough input arguments.)
kindly someone help me to solve this
  2 commentaires
Stephen23
Stephen23 le 24 Fév 2019
Modifié(e) : Stephen23 le 24 Fév 2019
@muhammad irfan: as the error message states, most likely you have not used enough input arguments. But as you have not shown us how you are calling this function, we can only guess.
When I searched for "Medical Image Processing Toolbox Omer Demirkaya, Musa Asyali, Prasanna Shaoo" most results appear to be about this book:
Star Strider
Star Strider le 24 Fév 2019
@Stephen — Thank you.

Connectez-vous pour commenter.


Toni Bodunrin
Toni Bodunrin le 7 Nov 2020
Modifié(e) : DGM le 4 Mar 2023
Getting not enough input arugmetns for this code, PLEASE HELP
function Dn=calculateFourierCoefficients(sig,period,n)
syms t;
w0 = (2*pi)/period;
N = n;
x = sig; %what was given to us
ak = zeros(1,2*N+1); %intialize a row vector of 2N+1 zeros
%calculate the period and store in T
for k = -N:N
ak(1,1+k+N) = 1/period * int(x * exp(-1i*k*w0*t), t, 0, period); % ak is fourier coefficient
end
ak(1,1+k+N)
  7 commentaires
Toni Bodunrin
Toni Bodunrin le 7 Nov 2020
Modifié(e) : Walter Roberson le 8 Nov 2020
ohhh okay i see
I changed it to this but I am still getting the not enough input arguments error. Thank you for your help and patience in theis matter
function Dn=calculateFourierCoefficients( sig,period,n)
syms t
Dn= 1/period * int(sig * exp(-1i*n*w0*t), t, -period, period)
end
Walter Roberson
Walter Roberson le 7 Nov 2020
x = @(t) cos((3*pi*t)/10) + 0.5*sin((pi*t)/10);
That is a function handle.
Dn=calculateFourierCoefficients(x ,w0, g);
You pass the function handle to the function.
Dn= 1/period * int(sig * exp(-1i*n*w0*t), t, -period, period)
You multiply the function handle by something, without invoking the function handle. That would normally be an error, but possibly it invokes the function with no arguments.

Connectez-vous pour commenter.


Min You
Min You le 23 Sep 2021
Help I am getting the error "Not enough input arguments
function [A, B, C, D, N, Q] = Modify_Arrays(m, n)
% Check whether m,n are even and greater than 4
if m<4 || n<4 || mod(m,2)~=0 || mod(n,2)~=0
error('m & n must be even numbers greater than or equal to 4!!')
end
% Array A
A = ones(m, n);
% Array B
B = A;
B([1,end], :) = 0;
B(2:end-1, [1, end]) = 0;
% Array C
C = A;
C([1,end], [1,end]) = 0;
% Array D
D = A(2:end-1, 2:end-1);
% Array N
N = A;
N(:, 1) = 1:m;
N(1,2:end) = 2:n;
% Array Q
Q = A;
r = m/2;
c = n/2;
Q(1:r, 1:c) = 0;
Q(r+1:end, c+1:end) = 0;
end
  1 commentaire
Walter Roberson
Walter Roberson le 23 Sep 2021
When you pressed the green Run button, where where you expecting MATLAB to look to find out which value it should use for m and n ?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by