Use of "fsolve" when in the definition of function there is "sum"

1 vue (au cours des 30 derniers jours)
Romina
Romina le 2 Nov 2013
Modifié(e) : Matt J le 3 Nov 2013
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!

Réponse acceptée

Matt J
Matt J le 2 Nov 2013
Modifié(e) : Matt J le 2 Nov 2013
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.
  2 commentaires
Romina
Romina le 3 Nov 2013
Thanks for the help!
I modified the code:
global NH
NH=0;
nS=nt;
while NH~=nS
NH=nS;
x0A=[L/2;(ei+es)/2];
options=optimset('MaxFunEvals',1000, 'MaxIter', 1000);
[X,fval,exitflag]=fsolve(@equilibriumA,x0A,options);
nS=floor((L-X(1))/p)+1;
end
and my function is:
function F=equilibriumA(X)
global NH
som=sum(BR(1:NH,1));
som2=sum(BR(1:NH,1).^2);
F(1)=a*X(1)/X(2)...
+b*X(1)/X(2)*(X(2)-c)...
-(d*X(2)*(som/X(1)-NH))...
-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)*(som/X(1)-NH))*(((som2-X(1)*som)/(som-X(1)*NH))-L/2)...
-M;
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]
a, b, c, d, ei, es, L, N, M, p, are knowns and belong to real numbers
nt is known and belongs to natural numbers
BR is an array known nt x 2
But now, I get the following error:
Optimization terminated: no further progress can be made.
Trust-region radius less than 2*eps.
Problem may be ill-conditioned or Jacobian may be inaccurate.
Try using exact Jacobian or check Jacobian for errors.
Maximum number of function evaluations reached:
increase options.MaxFunEvals.
Maximum number of function evaluations reached:
increase options.MaxFunEvals.
Maximum number of function evaluations reached:
increase options.MaxFunEvals.
Warning: Matrix is singular, close to singular or badly scaled.
Results may be inaccurate. RCOND = NaN.
> In optim\private\dogleg at 72
In optim\private\trustnleqn at 183
In fsolve at 378
In VR_DT200 at 89
Any advice?
Thank you!
Matt J
Matt J le 3 Nov 2013
Modifié(e) : Matt J le 3 Nov 2013
It's saying your problem is ill-conditioned. You could try following its advice about providing an exact Jacobian instead of using a finite difference approximation to the derivatives.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by