Error regarding not enough input arguments

Keep getting error saying not enough input arguments and can't figure out what i did wrong.
function sinusoid(sinusoid_type,attenuation_factor,frequency,phase)
if nargin==3
phase=0;
end
t=0:0.0025:2*pi;
if sinusoid_type==1
mt=exp(-attenuation_factor*t).*sin((2*pi*frequency*t)+phase);
else
mt=exp(-attenuation_factor*t).*cos((2*pi*frequency*t)+phase);
end
plot(t,mt);
hold on;
plot(t,exp(-attenuation_factor*t),'--r');
hold on;
plot(t,-exp(-attenuation_factor*t).'--r');
hold off;
end

1 Commento

Also get an error saying sinusoid_type is undefined

Risposte (1)

How did you call your ‘sinusoid’ function?
You must provide ‘sinmusoid_type’ and the other arguments. Your function will not pick them up from your workspace.
The most frequent reason that ‘not enough input arguments’ occurs if you have provided what appear to be the necessary number of them, is that you have a variable or script that has the same name as one of the functions that’s throwing that error. This is called ‘overshadowing’ of MATLAB functions, and is to be absolutely avoided.
I don’t know the function that threw that error, since you didn’t provide that information. If it was exp for example, run this line from a script or your Command Window:
which exp -all
If the first line is:
exp is a variable.
you’ve found your problem. The solution is to rename the variable to one that doesn’t conflict with any built-in MATLAB functions. Do that for whatever function is throwing the error.

2 Commenti

Thank You, Everything I have done is shown above, but e is intended to be used as the mathmatical value/variable of e. The problem with sinusoid_type seems to persist no matter what i do saying it is undefined. I am new to MATLAB, so unsure of what all my problem solving options are.
My pleasure.
You need to call your ‘sinusoid’ function as:
sinusoid_type = ...;
attenuation_factor = ...;
frequency = ...;
phase = ...;
sinusoid(sinusoid_type,attenuation_factor,frequency,phase)
The variables don¹t have to be the same names as the variables in your function, but you have to define each of them specifically as something, or you will get the errors you see.
I didn’t see ‘e’ anywhere in your code. The exp function is the correct way to invoke it.
Also, you’ll probably want ‘nargin < 4’ rather than ‘nargin == 3’ here:
if nargin < 4
phase=0;
end

Questa domanda è chiusa.

Tag

Richiesto:

il 1 Dic 2018

Chiuso:

il 20 Ago 2021

Community Treasure Hunt

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

Start Hunting!

Translated by