フィルターのクリア

function handle with input parameters

1 回表示 (過去 30 日間)
min lee
min lee 2024 年 5 月 1 日
コメント済み: Star Strider 2024 年 5 月 1 日
I need to invoke the 'eigs' function to calculate the lowest eigenvalue of a hermitian matrix, which I do not construct explicitly. I thus use a function handle to realize the action of the matrix on a vector. My code is like this
[evector, evalue]=eigs(@fun_Htimesx, dim, 1,'smallestreal');
In the function 'fun_Htimesx', I have
function y = fun_Htimesx(x)
global H transx_many transy_many qx qy Lx Ly
That is, the hermitian matirx 'H' and many other parameters are set as global variables so that I do not need to pass them to the function handle.
Now, my question is, how can avoid global variables? How to pass parameters to a function handle?

採用された回答

Star Strider
Star Strider 2024 年 5 月 1 日
I am not certain that I understand what you want to do.
One approach:
function y = fun_Htimesx(x, H , transx_many, transy_many, qx, qy, Lx, Ly)
If you only want to pass ‘x’ to it (for example in an optimisation function call), the function handle becomes:
@(x)fun_Htimesx(x, H , transx_many, transy_many, qx, qy, Lx, Ly)
providing that all the other argumnents are already present in the calling script (or function script) workspace. Otherwise, just pass ‘x’ to it if that is the only argument that changes. The others will be passed automatically, providing they are preseent in the calling script (or function script) workspace.
.
  2 件のコメント
min lee
min lee 2024 年 5 月 1 日
編集済み: min lee 2024 年 5 月 1 日
@(x)fun_Htimesx(x, H , transx_many, transy_many, qx, qy, Lx, Ly)
I got an error message 'too many input arguments'.
All other arguments like 'H' are parameters defining the hermitian matrix, which do exist in my script workspace. That is why I set them as global.
Star Strider
Star Strider 2024 年 5 月 1 日
If you want to use all of them to calculate ‘y’ use this approach:
y = fun_Htimesx(x, H , transx_many, transy_many, qx, qy, Lx, Ly);
.

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by