How can I use the number of variables as expression in a logical if Statement?

I have a function that operates with two function handles as input arguments and creates the hessian matrix of another function later. I want to calculate the eigenvalues of that hessian matrix at certain points, so that I converted the matrix into a function handle using 'matlabFunction'.
The problem I got now is that I don't know, whether the hessian matrix is a function in x and y, in only one or in none variables, so that the input hessian(x,y) is not defined if there are less then two variables. My idea is to seperate the cases in an if-statement so that I can have only one input, but I don't know what kind of expression would fit for the condition and how to seperate the cases when there is one variable in the matrix, like P=[1;0] hessian=[1 0; 0 y].

9 Comments

That works for the cases nargin()= 0 and 2, thank you sir.
But I can't use that for case 1, can I? It only gives me the number of variables not the name, so that I can't decide whether to use the y or x coordinate.
inputname() call tell you the name of the variable the user passed as a parmeter, if the user used a variable at all and not an expression. However, if you are planning to look at whether the user used variable name x or y then that is not good programming practice -- suppose the user used Y or t or something else of meaning to them ?
I made sure that the variables are x and y, anyways if I try to use
if inputname(f)==x
I get
Error using inputname
Scalar integer value required, but non-numeric value seen
inputname() returns 'x' or 'y', use strcmp()
Hesse=hessian(sym(f)); %Hessian of main function f
Hessianhandle=matlabFunction(Hesse);
for k=1:n
switch nargin(Hessianhandle) %functionhandle in nargin()variables
%calculate eigenvalues of hessian at points p1,...,pn
case 0
eigenvalues=eig(Hesse);
case 1
tempvar=inputname(Hessianhandle)
if strcmp(tempvar,x)
Htemp=Hessianhandle(P(1,k));
elseif strcmp(tempvar,y)
Htemp=Hessianhandle(P(2,k));
end
eigenvalues=eig(Htemp);
case 2
Htemp=Hessianhandle(P(1,k),P(2,k));
eigenvalues=eig(Htemp);
end
Thats the current part of the code I got troubles with @Jan. The last thing I tried is using inputname and strcmp as recommended by @Fangjun Jiang, but he seems to have troubles using inputname with a matrix.
The current error:
Error using inputname
Scalar integer value required, but non-numeric value seen
Error in exp46 (line 145)

Sign in to comment.

Answers (0)

Categories

Products

Asked:

on 18 Feb 2021

Commented:

on 18 Feb 2021

Community Treasure Hunt

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

Start Hunting!