How to pass extra parameters to lsqnonlin function?

25 views (last 30 days)
I am a bit new to Matlab.
I am trying to solve a non-linear system of 31 equations using lsqnonlin function. I made two scripts, one for the function called "Fault_free_HP_3" :
function F = Fault_free_HP_initial_input_3(Y,Lb,Hb)
It contains all the non-linear equations, 10 extra constant parameters and it ends with:
F =[F1; F2; .....; F31];
and the other one called "Fault_free_HP_initial_input_3" contains all the initial guesses of and upper and lower limits for each variable and used the fhandle tool to call the function as below:
Y0 = [.....]; %as initial guesses
Lb = [.....]; %as lower limits
Hb = [.....]; %as upper limits
options = optimset('MaxFunEvals',3000);
fhandle = @Fault_free_HP_3;
[Y,resnorm,residual,exitflag] = lsqnonlin(fhandle,Y0,Lb,Hb,options);
The 10 extra parameters I have are constants and I had to specify them inside the function and everything work well.
But those 10 extra constant parameters should be input by the user and then treated as constants in the code. I was thinking of making a user friendly interface like GUI for example for the user to input them.
I tried to pass those parameters (for example "m_dot_w" as extra parameter) to lsqnonlin function by doing the following:
in the function script:
function F = Fault_free_HP_initial_input_3(Y,Lb,Hb,m_dot_w)
I just deleted the constants parameters values from the function script and kept everything else the same, and it ends with:
F =[F1; F2; .....; F31];
For example if I just deleted one parameter "m_dot_w" from the function script and kept it as input in the second script that calls the function as the following:
m_dot_w = 0.271;
Y0 = [.....]; Lb = [.....]; Hb = [.....];
options = optimset('MaxFunEvals',3000);
fhandle = @Fault_free_HP_3;
[Y,resnorm,residual,exitflag] = lsqnonlin(fhandle,Y0,Lb,Hb,options,m_dot_w);
When I run the code, the function does not recognize the values of "m_dot_w" and it gives me the following error message:
Error using Fault_free_HP_3 (line 86)
Not enough input arguments.
Error in lsqnonlin (line 194)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in Fault_free_HP_initial_input_3 (line 222)
[Y,resnorm,residual,exitflag] = lsqnonlin(fhandle,Y0,Lb,Hb,options,m_dot_w);
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot
continue.
Line 86 in Fault_free_HP_3 (which is the function script name) were the error is happening is the first line in that function which uses "m_dot_w". It seems that the parameter m_dot_w is not passed to the function properly, and the function does not recognize it!
Am I doing it right? I there a possible way that I can pass those extra parameters to lsqnonlin function so that the user can change these constants when he runs the code using input command or GUI without the need to open the code and write it down in the function script ?
Please give me a simple code example using lsqnonlin function if possible. I have been struggling with this for a long time now instead of focusing on developing my code!
I would really appreciate your help!

Accepted Answer

Sean de Wolski
Sean de Wolski on 4 Jun 2015
Easiest way is with an anonymous function:
a = 1
b = 2
fh = @(x)your_function(x,a,b)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!