Problem passing function to lsqnonlin with costant parameters

Hello, i have this function to pass to lsqnonlin :
[val,Jac] = myfunction(x,par1,par2,par3)
my function computes already the Jacobian that im planning to pass to slqnonlin and is essential.
x is the optimization variable and par1,par2... are constant parameters required to evaluate the function and are computed separately.
The problem then is how i declare x as the only optimization variable?
I can't use function handle otherwise i would lose my multi ouput;
I can't declare par1,par2,... as globals cause myfunction has parallelization in it;
I could pass the code to evaluate pars into myfunction but it would add not necessary heavy computation into each iteration of lsqnonlin which i'd like to avoid;
Any other ideas? Thank you

 採用された回答

Matt J
Matt J 2019 年 1 月 15 日
編集済み: Matt J 2019 年 1 月 15 日

2 投票

par1=______;
par2=______;
par3=______;
lsqnonlin(@(x) myfunction(x,par1,par2,par3), __________);

6 件のコメント

Eugenio Grabovic
Eugenio Grabovic 2019 年 1 月 15 日
編集済み: Eugenio Grabovic 2019 年 1 月 15 日
This would be perfect if i had only one output from myfunction but in this way it doesn't recognize the Jacobian (as second output). For example i tried to just compute:
out = @(x) myfunction(x,par1,par2,par3)
out(xgiven)
and it simply gave me the value omitting the jacobian.
Am i wrong ?
Eugenio Grabovic
Eugenio Grabovic 2019 年 1 月 15 日
編集済み: Eugenio Grabovic 2019 年 1 月 15 日
Edit: i found another way thanks to your link given Passing Extra Parameters. by using nested functions. This way the parameters act as global inside the function and somehow allow the parallelization. Thanks
Matt J
Matt J 2019 年 1 月 18 日
There shouldn‘t have been issues with a 2nd output argument either way. I’d have to see your complete code to say why there was.
Eze Far
Eze Far 2023 年 7 月 4 日
Hi, will this "lsqnonlin(@(x) myfunction(x,par1,par2,par3), __________);" also make use of the second output of "myfunction", i.e. Jac?
Matt J
Matt J 2023 年 7 月 4 日
@Eze Far Yes, it will.
Eze Far
Eze Far 2023 年 7 月 5 日
@Matt J thanks!

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

その他の回答 (0 件)

質問済み:

2019 年 1 月 15 日

コメント済み:

2023 年 7 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by