Filter löschen
Filter löschen

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

5 Ansichten (letzte 30 Tage)
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 Kommentare
Arne Kähler
Arne Kähler am 18 Feb. 2021
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)

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by