Fsolve with different combinations of parameters

10 views (last 30 days)
Hi,
I have a system of 11 nonlinear equations and 11 unknown variables that I would like to solve for. The nonlinear equations also depend on a set of parameters. Using the fsolve function, I keep getting the message:
Solver stopped prematurely.
fsolve stopped because it exceeded the function evaluation limit,
options.MaxFunctionEvaluations = 8.000000e+03.
I therefore want to nest the fsolve function inside a set of loops over different combinations of parameter values to see if I can get a solution with a different set of parameters. What is the generic code for doing so?
Thank you very much!

Accepted Answer

Torsten
Torsten on 23 May 2023
Edited: Torsten on 23 May 2023
Varying the initial values for the unknown variables should be the first step. Try "MultiStart".
Varying parameters is usually done via nested for-loops where each loop runs over the values of one of the parameters.
  6 Comments
Selim Elbadri
Selim Elbadri on 24 May 2023
Edited: Selim Elbadri on 24 May 2023
Hi,
When I run the code, I get the following error code:
Unable to perform assignment because the left and right sides have a different number of elements.
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in solution_loops (line 14)
z(ip1,ip2) = fsolve(@(x)FUNS(x,parameter),zg);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
My code is as follows:
% Solve for equilibrium
clear all
c_bar = [0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.40];
beta = [0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4];
zg = [0.1;0.1;0.3;0.3;0.2;0.2;1.0;0.5;2.0;10.0;5.0];
options=optimset('TolFun' , 1e-6,'Display','iter', 'TolX' , 1e-8, 'MaxFunEvals' , 30, 'MaxIter' , 20);
for ip1 = 1:numel(c_bar)
for ip2 = 1:numel(beta)
parameter = [c_bar(ip1);beta(ip2)];
z(ip1,ip2) = fsolve(@(x)FUNS(x,parameter),zg);
end
end
function fval=FUNS(z, parameter)
c_bar= parameter(1);
beta = parameter(2);
cm = z(1);
.
.
.
fval(1) = cm - (((ps*c_bar) + ((beta*Nm)/(1-beta)) + beta*Lps*Ns*ps^(1/beta) + beta*Lpm*Nm)/(1 + ps^(1-sigma)));
.
.
.
Torsten
Torsten on 24 May 2023
Edited: Torsten on 24 May 2023
Since z is a solution vector with more than one element, you will have to use
z(:,ip1,ip2) = fsolve(@(x)FUNS(x,parameter),zg);
instead of
z(ip1,ip2) = fsolve(@(x)FUNS(x,parameter),zg);
If you still get the error message
Failure in initial objective function evaluation. FSOLVE cannot continue.
try to evaluate your function with the initial values zg before calling "fsolve":
FUNS(zg,parameter)

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 23 May 2023
Increase the maximum evaluations instead.
opts = optimoptions(@fsolve, 'MaxFunctionEvaluations', 1e5, 'MaxIterations', 1e5);
[bestx, fval, exitflag] = fsolve(HANDLE, x0, opts)

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!