Failure in initial objective function evaluation. FSOLVE cannot continue.

4 vues (au cours des 30 derniers jours)
Chang Li
Chang Li le 14 Fév 2022
Commenté : Chang Li le 14 Fév 2022
I have some problems with fsolve:
My function:
function F = simple(x)
global c1 c2
F(1) = x(1)-c1;
F(2) = x(2)-c2;
end
Here is my code that calls this function in my main file:
global c1 c2
c1 = 150
c2 = 130
options = optimoptions('fsolve','Display','iter');
[x,fval,exitflag,output] = fsolve(@simple,[0 0],options)
Matlab's error message is as follows. If this is caused by some other part of my code. What can be the cause? How do I locate and correct that part? Thank you very much.
Error message:
Unable to perform assignment because the indices on the left side are not
compatible with the size of the right side.
Error in simple (line 4)
F(1) = x(1)-c1;
Error in fsolve (line 264)
fuser = feval(funfcn{3},x,varargin{:});
Error in nlogit (line 478)
[x,fval,exitflag,output] = fsolve(@simple,[0 0],options)
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
  2 commentaires
Ludovico Cucciniello
Ludovico Cucciniello le 14 Fév 2022
The problem might be the way you're defining the variables c1 and c2. Anyway, I tried this and seems to work.
c = [150, 300];
x0 = [0,0];
options = optimoptions('fsolve','Display','iter');
[x,fval,exitflag,output] = fsolve(@simple,x0,options,c);
function F = simple(x,c)
F(1) = x(1)-c(1);
F(2) = x(2)-c(2);
end
Chang Li
Chang Li le 14 Fév 2022
I figured out that I forget to declare global variables somewhere. Now it works. Thank you very much.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 14 Fév 2022
Works for me.
format long g
global c1 c2
c1 = 150
c1 =
150
c2 = 130
c2 =
130
options = optimoptions('fsolve','Display','iter');
[x,fval,exitflag,output] = fsolve(@simple,[0 0],options)
Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 3 39400 150 1 1 6 39004 1 149 1 2 9 38022.8 2.5 147 2.5 3 12 35624.4 6.25 143 6.25 4 15 29970.3 15.625 131 15.6 5 18 17971.2 39.0625 101 39.1 6 21 1325 97.6563 27.5 97.7 7 24 1.23776e-14 36.4006 9.4e-08 244 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x = 1×2
150.000000094 129.999999940489
fval = 1×2
1.0e+00 * 9.40000859372958e-08 -5.95112510382023e-08
exitflag =
1
output = struct with fields:
iterations: 7 funcCount: 24 algorithm: 'trust-region-dogleg' firstorderopt: 9.4000085878389e-08 message: '↵Equation solved.↵↵fsolve completed because the vector of function values is near zero↵as measured by the value of the function tolerance, and↵the problem appears regular as measured by the gradient.↵↵<stopping criteria details>↵↵Equation solved. The sum of squared function values, r = 1.237761e-14, is less than↵sqrt(options.FunctionTolerance) = 1.000000e-03. The relative norm of the gradient of r,↵9.400009e-08, is less than options.OptimalityTolerance = 1.000000e-06.↵↵'
function F = simple(x)
global c1 c2
F(1) = x(1)-c1;
F(2) = x(2)-c2;
end

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by