Problem in using fsolve

Hi
I am trying to solve the following set of equations but the solver is prematurely stopped and If I alter the MaxIter and MaxEvals then also its showing stopped prematurelly and then I tried to alter the tolerance then its shows no solution found. Can anyone guide how can i obtain the solution for the following set of equations.
function F = compare(X)
rho=1000.0;
F=[
1.0*7.018e-4*4.2850022315+-1.0*1.25e-3*X(2)+-1.0*7.36e-5*X(3)+-1.0*4.47e-5*(4);
-1.0*1.25e-3*X(1)+1.0*1.66e-5*X(4)+1.0*4.47e-5*X(5)+1.0*1.07e-4*X(6);
+1.0*7.36e-5*X(2)-1.0*1.07e-4*X(6)+1.0*1.07e-4*X(7);
1.0*4.47e-5*X(3)+-1.0*4.47e-5*X(5)+-1.0*1.07e-4*X(7);
1.0*0.4*rho*4.2850022315979^2+1.0*807362.4375+-1.0*0.4*rho*X(2)^2 + -1.0*X(9);
1.0*0.4*rho*4.2850022315979^2 + 1.0*807362.4375+-1.0*0.4*rho*X(3)^2+-1.0*X(10);
1.0*0.4*rho*4.2850022315979^2 + 1.0*807362.4375+-1.0*0.4*rho*X(4)^2+-1.0*X(11);
-1.0*0.4*rho*X(1)^2 + -1.0*X(8)+ 1.0*0.4*rho*X(4)^2 + 1.0*X(11);
-1.0*0.4*rho*X(1)^2 + -1.0*X(8) + 1.0*0.4*rho*X(5)^2 + 1.0*X(12);
-1.0*0.4*rho*X(1)^2 + -1*X(8) + 1.0*0.4*rho*X(6)^2 + 1.0*X(13);
-1.0*0.4*rho*X(6)^2 + -1.0*X(13) + 1.0*0.4*rho*X(2)^2 + 1.0*X(9);
-1.0*0.4*rho*X(6)^2 + -1.0*X(13) + 1.0*0.4*rho*X(7)^2 + 1.0*X(14);
1.0*0.4*rho*X(3)^2 + 1.0*X(10) + -1.0*0.4*rho*X(5)^2 + -1.0*X(12);
1.0*0.4*rho*X(3)^2 + 1.0*X(10) + -1.0*0.4*rho*X(7)^2 + -1.0*X(14)
];
end
options = optimset('Display','iter','TolX',1e-7,'MaxFunEvals',18000,'MaxIter',2200);
[Sol,fval,exitflag]=fsolve(@compare,X,options);
following error comes
No solution found.
fsolve stopped because the last step was ineffective. However, the vector of function
values is not near zero, as measured by the default value of the function tolerance.
<stopping criteria details>
where X's are the variables
Please Guide!!!
Regards

2 Comments

Sameer - it may be helpful to format your above code so that it is easier to read (highlight the code and press the {}Code button).
Also you may want to include additional information - what is V, what is P? What was your initial condition? Did you include any options? What is the code you used to evaluate this system of equations? What is the solution obtained by fsolve?
Unfortunately, I don't have access to the Optimization Toolbox so can't test out the above, but if you provide more information, someone else might be able to.
Thanks for the tip....
Regards

Sign in to comment.

 Accepted Answer

Star Strider
Star Strider on 10 Jun 2014
Edited: Star Strider on 10 Jun 2014
You have to write your function so that all of your unknowns are in the same parameter vector.
So calling your parameter vector ‘B’:
B = [V(1) ... V(7) P(1) ... P(7)];
then replace V(1) with B(1), P(1) with B(8), and so forth.
If you intend ‘F’ to be an anonymous function, you have to write it as:
F = @(B,rho) [1.0*10.0*25+-1.0*17.0*B(2)+ ... + -1.0*0.4*rho*B(7)^2 + -1.0*B(14)];
(I abbreviated the function here.) Use the ‘Search’ and ‘Replace’ utilities in the MATLAB Editor to do this more easily.
Your call to fsolve is then:
[x,fval] = fsolve(@(B) F(B,rho),x0)
Your F function will get ‘rho’ from your workspace.
EDIT — Try using different starting values for ‘X’ that are reasonably close to what you expect them to be. Otherwise experiment with them. If it doesn’t converge on a solution after several attempts, it may not have a solution. If you believe it should, go back to your original equations and be sure you wrote ‘F’ correctly.

12 Comments

The the calling is fine but its giving the error....please see at the error its giving....
Awaiting your response
Regards
Please see the EDIT in my previous comment:
  • EDIT — Try using different starting values for ‘X’ that are reasonably close to what you expect them to be. Otherwise experiment with them. If it doesn’t converge on a solution after several attempts, it may not have a solution. If you believe it should, go back to your original equations and be sure you wrote ‘F’ correctly.
Nothing seems to have changed. (Except that every time I edit your Question to format your code so it is readable, you un-format it!)
Hi...thanks for replying and I am sorry for all the trouble.
The equations are correct and there exists the solution for them. Inspite of several attempts its not converging to a solution, sometime error is prematurely stopped and sometimes no solution found.Is there any way using which I can solve the following set of equations.
Regards
Star Strider
Star Strider on 10 Jun 2014
Edited: Star Strider on 10 Jun 2014
No trouble, and no need to apologise! If there is no problem with your code, everything runs as it should, and your function simply won’t converge, all we can do is to suggest possible alternative approaches.
Such as:
If you have the Global Optimization Toolbox, I suggest the patternsearch function.
If you don’t have that toolbox, writing your own genetic algorithm would not be difficult, especially with your problem. The only problem with genetic algorithms is that they take more time to converge. However, they usually succeed.
If you don’t want to write your own genetic algorithm, then keep randomly starting with various values and magnitudes of ‘X0’. Sometimes multiplying ‘X0’ by 100 or 1000 will get you closer to an answer. Eventually, you will find one that works.
If you know it has a solution, and published parameters for the same or similar functions don’t converge for you, the either something is wrong with your code, or the parameters in the paper are wrong. The fact that a paper passes the review process doesn’t mean that the results it reports are reproducible.
Hi thanks for replying, unfortunately I dont have Global Optimization Toolbox, and writing a genetic algorithim seems kinda tricky or I would say its hard for me as I am a newbiee in Matlab. But after lot of attempts its showing Equation Solved,fsolve stalled.
So what does it mean by Equation Solved,fsolve stalled....does it indicating the answers obtained are unreliable?
Please Guide
Regards
The ‘Equation Solved’ sounds good!
Is ‘fsolve stalled’ that the entire error message?
You requested it return ‘exitflag’, so that should give you some indication of its success.
yes it was,but as the input conditions that i have made fixed(not the guess) must be the input from the user so as it is changed it is not getting solved again. I tried with lsqnonin with that as well initially it showed minimum possible with the exitflag3 and then after number of trials its giving same as fsolve. Can you suggest any other way apart from fsolve by which such system can be solved. Its very important for my project without it I cannot proceed further. ANy guidance is highly appriciated.
Regards
Sameer
Sameer on 11 Jun 2014
Edited: Sameer on 11 Jun 2014
Hi...what do you mean by Genetic algorithim, is there any example I can refer to..... and how will it help.....please quide
Regads
See the Genetic Algorithms Toolbox from the University of Sheffield (UK). I haven’t used it, but I did look through it and am impressed to the extent that I recommend it.
Thanks a lot.
Regards
My pleasure!
The GA should work for you.
Sameer
Sameer on 13 Jun 2014
Edited: Sameer on 13 Jun 2014
Didn't used that....Manupulated my equations and simplified them a bit and then took guess of 10's and it worked. Though there is a slight error but its fine as its involves the use of terms obtained from linear regression. So I am satisfied thus didn't feel like to use GA but thanks a lot for all the support.
Regards

Sign in to comment.

More Answers (2)

Marc
Marc on 15 Jun 2014
Edited: Marc on 15 Jun 2014
Taking Star Strider's approach, I compared "patternsearch" with "fsolve". For patternsearch, I used the following objective function:
fObj = sum(F.^2)
Called patternsearch and solve with the following initial guess...
xGuess = [1.0; 0.5; 0.25; 0.1; 1.2; 2.3; 3; 0.5;
2.0; 1.5; 1.2; 0.46; 0.9; 0.8; 2];
%xGuess = [10; 10; 10; 10; 10; 10; 10; 10;
% 10; 10; 10; 10; 10; 10; 10];
F = craxy14(xGuess)
options1 = optimset('Display','iter','TolX',1e-7,'MaxFunEvals',18000,'MaxIter',2200);
[Sol,fval,exitflag]=fsolve(@craxy14,xGuess,options1)
fvalComp = sum(fval.^2)
options = psoptimset('TolX', 1e-12, 'TolFun', 1e-10);
[Sol, fval, exitflag] = patternsearch(@craxy14_PS, xGuess,[],[],[],[],[],[],[],options)
And got the following output...
F =
0.0021850145660667
-0.0009486
0.0001117
-0.000363465
814604.93514992
814680.43514992
814701.73514992
-395.3
175.96
1716.4
-2014.9
1483.9
-549.96
-3574.3
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead. > In fsolve at 309 In tryCrazy at 13
First-Order Norm of
Iteration Func-count Residual optimality Lambda step
0 16 1.99105e+12 3.27e+08 0.01
1 41 1.48607e+12 6.51e+09 1e+07 36.218
2 58 1.04076e+12 2.68e+09 1e+08 30.8991
3 74 7.3865e+11 5.56e+09 1e+07 26.3164
4 91 1.36874e+11 8.44e+09 1e+08 42.257
5 107 3.31559e+09 9.86e+08 1e+07 13.2943
6 123 703959 2.58e+07 1e+06 2.72485
7 139 2.20032 1.47e+04 100000 0.0333549
8 155 0.00565597 3.26 10000 7.18966e-05
9 171 0.00565585 8.38e-05 1000 1.69513e-08
No solution found.
fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the default value of the function tolerance.
Sol =
45.1305446907037
45.1305011994196
45.1305149725761
45.1305231544755
45.1305458382382
45.1305337039192
45.1305364903727
0.509518068564456
2.07974558905608
1.58247394623463
1.2870712557843
0.468086982642547
0.906189676429257
0.805586368498305
2
fval =
-0.0569063178351894
-0.0488177116737268
0.0033216051864278
-0.00482896878416497
5.23959542420016e-09
5.85573345368573e-09
4.49008474845414e-09
3.42615713577743e-09
-8.54356030366432e-11
5.20720910834882e-10
2.21098517272367e-09
-2.78552736432403e-10
2.18091139592147e-09
1.90836724200949e-09
exitflag =
-2
fvalComp =
0.00565584998315177
Optimization terminated: mesh size less than options.TolMesh.
Sol =
1
32
16.25
16.1
1.2
2.3
3
814306.935150146
405106.935150146
709081.935150146
711022.935149383
814130.935150108
812590.935150528
811106.935150909
2
fval =
0.00147856720654457
exitflag =
1
Not sure which one is "more" or "less" right.
Two of solutions:
1:
x1 0.0302626794484622
x2 2.44987629792448
x3 -3.17840769482201
x4 -0.0245615818144686
x5 -0.291871074861581
x6 0.479277650607576
x7 -1.20587090572179
x8 814706.568818013
x9 812306.177599867
x10 810666.024960118
x11 814706.693841399
x12 814672.859660183
x13 814615.052323371
x14 814125.285293413
2:
x1 0.168219021935448
x2 2.20358006851998
x3 1.00461250566195
x4 0.191813576695766
x5 0.564198756330943
x6 1.69971951063707
x7 0.18398593079529
x8 814695.616094183
x9 812764.629102568
x10 814303.236635307
x11 814692.218170638
x12 814579.607055261
x13 813551.316583983
x14 814693.394820827

Asked:

on 10 Jun 2014

Answered:

on 11 Dec 2019

Community Treasure Hunt

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

Start Hunting!