lsqnonlin for an unique parameter in a multiple variables function
2 views (last 30 days)
Show older comments
Hello,
I have a function with multiples variables : "p, T, e, Q, H, t" that I want to minimize, but ONLY in regard to the "p" parameter (wich can be a vector).
myfun is initially defined as :
function [res] = myfun(p, T, e, Q, H, t)
...
end
I have tried several input for the lsqnonlin function but, even if there is no error messages, the solution is false :
__________________
1st case__________
[x] = lsqnonlin(myfun, ptest, [], [], options, T, e, Q, H, t);
__________________
2nd case__________
myfun_real = @(params)myfun(params, T, e, Q, H, t);
[x] = lsqnonlin(myfun_real, ptest, [], [], options);
___________________
3rd case___________
The only way I have found is to create a new fonction with ONLY "p" as entry and where I have to declare the parameters "T, e, Q, H, t" as "GLOBAL", and it is NOT a good solution for the aim of my program (reproductibily etc...)
function res = myfun_wor(params);
global T, e, Q, H, t
....
end
[x] = lsqnonlin(myfun_wor, param, [], [], options);
__________________
Could you please tell me how to call the lsqnonlin for my function WITHOUT having to use global assignation for the other parameters ?
Thank you !
2 Comments
Caleb Magruder
on 29 Jan 2021
Hi Marie,
My understanding is that you are trying to minimize with respect to the parameter p while holding the other variables fixed/constant. I also understand that sometimes lsqnonlin returns an incorrect answer without an error message.
Your second case is a recommended workflow. An equivalent way to write the second case is as follows:
[x] = lsqnonlin(@(params)myfun(params, T, e, Q, H, t), ptest, [], [], options);
There are a few reasons why lsqnonlin can return the wrong answer, most frequently because the algorithm did not successfuly converge. Would you be able to share the terminal output after enabling the iterative display in lsqnonlin? You can do this by running the following command before calling lsqnonlin:
options.Display = 'iter-detailed';
This should cause a large amount of text to appear in the terminal which can diagnose whether the algorithm is converging or not.
Answers (0)
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!