matlab Not enough input arguments for Anonymous function - matlab

Why is that error popping up?
I would like to know why the input argument is lacking
Did I write the expression for x wrong?
The expression for x is
I tried modifying the expression for x and also the range of fplot, but I couldn't.
my_function=#(x)my_function((-x.^2)-(5.*x)-3+(exp.^x))
my_function =
function_handle with value:
#(x)my_function((-x.^2)-(5.*x)-3+(exp.^x))
>> fplot(my_function,[-5:5])
Error using fplot
Invalid parameter '-5 -4 -...'.
>> fplot(my_function,[-5 5])
Warning: Error updating FunctionLine.
The following error was reported evaluating the
function in FunctionLine update: Not enough input
arguments.
>> fplot(my_function,[-5,5])
Warning: Error updating FunctionLine.
The following error was reported evaluating the
function in FunctionLine update: Not enough input
arguments.
>> fplot(my_function,[-5,5,1])
Error using fplot
Invalid parameter '-5 5 1'.
>> fplot(my_function,(-5,5))
fplot(my_function,(-5,5))
↑
Invalid expression. When calling a function or indexing
a variable, use parentheses. Otherwise, check for
mismatched delimiters.
>> fplot(my_function,[-5,5])
Warning: Error updating FunctionLine.
The following error was reported evaluating the
function in FunctionLine update: Not enough input
arguments.
The issues to be addressed are:
Use the fplot function to plot the function over the range of x from -5 to +5.

exp is not a value, it is a function, and you are providing no inputs (hence the error message). exp.^x should be replaced by exp(x)

Related

About symbolic MATLAB

What is wrong with the following code?
clear all
syms x y a ;
u=2*x*y;
v=a^2+x^2-y^2;
diff=diff(u,'x',1)+diff(v,'y',1);
if diff==0
disp('Liquid motion is possible.')
disp('Stream function exists.')
else
disp('Liquid motion is not possible.')
disp('Stream function does not exist.')
end
diff2=diff(v,'x',1)-diff(u,'y',1);
if diff2==0
disp('Velocity potential exists.')
else
disp('Velocity potential does not exist.')
end
This comes in the command window when I run the above.
Liquid motion is possible.
Stream function exists.
Error using sym/subsindex (line 672)
Invalid indexing or function definition. When defining a function, ensure that the body of the function is a SYM
object. When indexing, the input must be numeric, logical or ':'.
Error in sym>privformat (line 1502)
x = subsindex(x)+1;
Error in sym/subsref (line 694)
[inds{k},refs{k}] = privformat(inds{k});
Error in q7 (line 17)
diff2=diff(v,'x',1)-diff(u,'y',1);
But if I rewrite(redefine) the symbolic variables after the first if construct, it runs fine. Also if I cancel the first if construct, it runs.
I would avoid to overwrite a reserved name, so instead of
diff=diff(u,'x',1)+diff(v,'y',1);
I would suggest
derFcn = diff(u,'x',1)+diff(v,'y',1);
This triggers the second error;
diff2=diff(v,'x',1)-diff(u,'y',1);
at this point diff is your diff value (which, incidentally is 0) so it is equivalent to write
0(v,'x',1)
which, of course, will not compile and it is not what you meant.
So, please, make the substitution (and update your if statements accordingly).

Why is fgoalattain built-in function not working in MATLAB?

I'm trying to invoke an fgoalattain function (computing a function minimum with a goal-attainment algorithm) in MATLAB. According to documentation, it should look like this:
[X,FVAL,ATTAINFACTOR] = FGOALATTAIN(#objf_1,x0,goal,weight)
where #objf_1 is a function handle for a function defined in objf_1 m-file and the rest are some arguments I set on my own. They are not important in any case, because evidently MATLAB has a problem with that function, as it throws:
>> rospar_4
Undefined function 'fgoalattain' for input arguments of type 'function_handle'.
Error in rospar_4 (line 29)
[X,FVAL,ATTAINFACTOR] = fgoalattain(#objf_1,x0,goal,weight)
However, I already know that the function works fine on another MATLAB version - R2011b (the one I'm using is R2012b), but with first argument as char instead:
[X,FVAL,ATTAINFACTOR] = fgoalattain('objf_1',x0,goal,weight)
If I try to invoke it like this in mine though, the error is almost the same:
>> rospar_4
Undefined function 'fgoalattain' for input arguments of type 'char'.
Error in rospar_4 (line 29)
[X,FVAL,ATTAINFACTOR] = fgoalattain('objf_1',x0,goal,weight)
Any idea how am I getting this?
It is not a built-in function. You need to install the Optimization toolbox.

Matlab: nest fzero in fminsearch

I am trying to minimize with respect to a variable "y" a function that contains a parameter which must be calculated as a solution of an equality that contains "y" as well (say, y=-3; in my complete problem it is an equation with no analytic closed form solution, so I really need fzero).
Because of this, I include the fzero function in the argument of fminsearch:
fminsearch( #(y) 10*fzero(#(y) y+3, 0)) ;
I get the error:
Error using fminsearch (line 85)
The input to FMINSEARCH should be either a structure with
valid fields or consist of at least two arguments.
I obviously get the same error with:
f = fzero(#(y) y+3, 0);
fminsearch(#(y) 10*f);
Apparently the problem is that I cannot "nest" a fzero inside fminsearch.
Any idea about how to turn around this problem?
If you read the error message you got and look at the documentation of fminsearch you'll see that you need to call it with two input arguments. You call it with only one.
fminsearch( #(y) 10*fzero(#(x) x+3, 0), 0 )

??? Input argument "x" is undefined

I enter:
EDU>> %using the temporary variable levels
EDU>> levels=range/quantise_range;
levels=round(levels);
quantisation_bits=log2(levels)
NB. There is no x variable anywhere
My error is:
??? Input argument "x" is undefined.
Error in ==> range at 18
y = max(x) - min(x);
EDU>> %combining above process into one statement
quantisation_bits=log2(round(range/quantise_range));
??? Input argument "x" is undefined.
Error in ==> range at 18
y = max(x) - min(x);
Would someone care to explain thie issue? I am beginner into programming and I really don't understand how to read the error hint.
Thanks.
Additionally, in what may be due to whatever the same principle misunderstanding on my part is, I am finding trouble here with this code, with error also included:
%Trying to create my own function, I’m pressing shift+enter at the end of lines for neatness:
EDU>> function what_am_i()
disp 'I am a function'
??? function what_am_i()
|
Error: Function definitions are not
permitted in this context.
EDU>>
function what_am_i()
disp' I am a function'
??? function what_am_i()
|
Error: Function definitions are not
permitted in this context.
EDU>> end
??? end
|
Error: Illegal use of reserved keyword
"end".
You may be confusing the "Command Window" with the "Editor".
In the "Command Window" you can enter some lines of code, but can't create functions. It acts more like a calculator.
If you create and save files, then you open them in the "Editor" and that's when Matlab begins behaving more like a programming language. It saves .m files that can have functions or algorithms written in them.
Matlab has lots of help available. I recommend visiting their website and searching around a bit. (or just google "intro to matlab")
In addition to #user1860611's answer regarding function definitions in the command window, the other problem you have has to do with range, which is a built-in function. It appears you are trying to use range it as a variable name, but didn't actually initialize it to a value, so it is still a function.
In the line here:
levels=range/quantise_range;
you are essentially calling the range function, but without passing it an argument.
Error in ==> range at 18
y = max(x) - min(x);
The error message is telling you that a function called range has generated an error. It doesn't matter that you don't have a variable named x, because the function has one internally.

"Not enough input arguments." when using fsolve with nested functions

I am trying to make my own function in matlab to solve for a system of two nonlinear equations, while using a nested function to share some some parameters, here is a sample code:
function y=solve(a,x0)
a;
y=fsolve(nle,x0); % this is line 3
function f=nle(x)
f(1)=x(1)-a*x(1)^2-x(1)*x(2); % this is line 6
f(2)=2*x(2)-x(2)+3*x(1)*x(2);
end
end
Here a is the parameter I want to pass from command line to the function, and x0 is the start point for the fsolve.
However, when I call the function in malab after specifying a=4 and x0=[1 1]', it gave me the following error:
Error using solve/nle (line 6)
Not enough input arguments.
Error in solve (line 3)
y=fsolve(nle,x0);
I'm quite a newbie for matlab, can anybody tell me where I am doing wrong?
Thanks in advance.
EDIT:
I tried substituting the nle with a function handle #nle, but seems something else went wrong:
Undefined function 'fsolve' for input arguments of type 'function_handle'.
Error in solve (line 3)
y=fsolve(#nle,x0);
Doesn't seem to make sense since I checked the documentation for fsolve, and it says it should indeed use a function handle there...
You miss the '#' in front of nle, i.e.
y = fsolve(#nle,x0);
should work.