MATLAB: Use of “fsolve” when in the definition of function there is “sum”

fsolvesum

Hello everybody,
I have this code:
X0A=[L/2; (ei+es)/2; nt/2];
[x,fval,exitflag]=fsolve(@equilibriumA, X0A)
and my function is:
function F=equilibriumA(X)
F(1)= a*X(1)/X(2)+b*X(1)/X(2)*(X(2)-c)-d*X(2)/X(1)*sum(BR(1:floor(X(3)),1)-X(1))-N;
F(2)= (a*X(1)/X(2))*(L/2-(X(1)*(1-2/3*c/X(2))))+(b*X(1)/X(2)*(X(2)-c))*(L/2-(X(1)/2*(1-c/X(2))))+(d*X(2)/X(1)*sum(BR(1:floor(X(3)),1)-X(1)))*(((BR(1:floor(X(3)),1)-X(1))'*BR(1:floor(X(3)),1)/sum(BR(1:floor(X(3)),1)-X(1)))-L/2)-M;
F(3)=floor(X(3))-floor((L-X(1))/p)+1;
where:
X(1), X(2), X(3) are the unknowns and
X(1) belongs to real numbers and belongs to the interval [0, L]
X(2) belongs to real numbers and belongs to the interval [ei,es]
X(3) belongs to natural numbers belongs to the interval [0, nt]
a, b, c, d, L, N, M, p, are knowns and are the real numbers
BR is an array known nt x 2
I get the following error:
"Optimizer appears to be converging to a point which is not a root.
Norm of relative change in X is less than max(options.TolX^2,eps) but
sum-of-squares of function values is greater than or equal to sqrt(options.TolFun)
Try again with a new starting guess"
I think the problem is the use of sum in the F definition.
Any advice for my problem?
Furthermore, I have another problem:
I have a second similar case of system to resolve, the code is:
X0B=[L/2; ei/2; nt/2];
[x,fval,exitflag]=fsolve(@equilibriumB, X0B)
and the function is:
function F=equilibriumB(X)
F(1)= b*X(1)*X(2)/2 ...
-d*X(2)/X(1)*sum(BARRE(1:floor(X(3)),1)-X(1))...
-N;
F(2)= b*X(1)*X(2)/2*(L/2-X(1)/3) ...
+(d*X(2)/X(1)*sum(BR(1:floor(X(3)),1)-X(1)))*(((BR(1:floor(X(3)),1)-X(1))'*BR(1:floor(X(3)),1)/sum(BR(1:floor(X(3)),1)-X(1)))-L/2)...
-M;
F(3)=floor(X(3))-floor((L-X(1))/p)+1;
in this case:
X(1) belongs to the interval [0, L]
X(2) belongs to the interval [0,ei]
X(3) belongs to the interval [0, nt]
I get the following error:
"??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> equilibriumB at 30
F(1)=b*X(1)*X(2)/2 ...
Error in ==> fsolve at 254
fuser = feval(funfcn{3},x,varargin{:});
Error in ==> VR_DT200 at 98
[x,fval,exitflag]=fsolve(@equilibriumB,X0B);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue."
Why?
Thank you!

Best Answer

  • Your functions are well outside the scope of what FSOLVE can solve. The function must be continuous and differentiable. Expressions like floor(X(3)) and using the X(i) to index discrete data ruin it.