lsqnonlin for an unique parameter in a multiple variables function

2 views (last 30 days)
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
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.
N/A
N/A on 29 Jan 2021
Hello Caleb,
Thanks for your answer.
When I compute with your proposed implementaton:
[x] = lsqnonlin(@(params)myfun(params, T, e, Q, H, t), ptest, [], [], options);
The terminal output is : (and the obatained result is an incorrect answer)
First-Order Norm of
Iteration Func-count Residual optimality Lambda step
0 3 2.69984e+09 2.36e+14 0.01
1 6 2.52542e+09 2.92e+14 0.001 12.2689
2 9 2.49845e+09 4.03e+13 0.0001 0.653413
3 12 2.49756e+09 8.2e+12 1e-05 0.44025
4 15 2.49752e+09 1.96e+12 1e-06 0.135337
Optimization stopped because the relative norm of the current step, 8.099757e-13,
is less than options.StepTolerance = 1.000000e-12.
However, when I compute as follows :
[x] = lsqnonlin(@(params)myfun2(params), ptest, [], [], options);
The terminal output is : (and Here I have the correct answer )
First-Order Norm of
Iteration Func-count Residual optimality Lambda step
0 3 145379 5.67e+11 0.01
1 6 230.836 4.19e+10 0.001 10.1825
2 9 0.0232868 4.66e+08 0.0001 0.284303
3 12 4.93448e-09 1.76e+05 1e-05 0.0018378
4 15 7.73217e-15 197 1e-06 1.53144e-06
5 18 1.58678e-15 1.71 1e-07 1.65433e-09
Optimization stopped because the relative norm of the current step, 1.959784e-13,
is less than options.StepTolerance = 1.000000e-12.
myfun and myfun2 are exactly the same, except that in myfun2 I had to declare the global variable
function J = myfun2(param)
global T e Q H t
...
end
function J = myfun(param, T, e, Q, H, t)
...
end

Sign in to comment.

Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!