# 8 nonlinear equations, 8 unknowns - little luck with fsolve/vpasolve

15 views (last 30 days)
Alex Tekin on 25 Jan 2020
Commented: Alex Sha on 3 Feb 2020
Hi All. I have a system of 8 nonlinear equations that I try to solve with fsolve. If I run the code, I get the solution because I start at the actual solutions. But If I change the guess values (g1,g2,etc) slightly, I get errors, like no solutions found, or fslove stopped prematurely. etc. I tried vpasolve, -- it seems to perform even worse than fslove, returning empty sets. I tried random guess values in fsolve, but it takes too long and I cannot see any solution still. I understand no solver or method is perfect. But perhaps I thought there is an inefficiency in my code? Any help is appreciated. I tried to convert the paramaters to ratios as suggested in earlier threads, -- no luck. Is there a way to solve this problem more efficiently instead of suggesting to give up on this, or research advanced numerical techniques?
##### 2 CommentsShowHide 1 older comment
Alex Tekin on 29 Jan 2020
Dear Matt J, thank you. I have chnaged that setting but to no avail. I received an error: "Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 100000 (the selected value)."

Alex Sha on 28 Jan 2020
Hi, Alex Tekin, you may try some solvers or software package with the ability of global optimization, i.e. Baron, Lingo, 1stOpt. the belows are the results obtained from 1stOpt:
1:
x1 4.5153251734112
x2 1.15089340375541
x3 1.02455790981764
x4 0.00214722268127572
x5 0.100000000000004
x6 4.07792365160064
x7 0.0252189443492125
x8 0.330004160606675
2:
x1 2.75653453284178
x2 1.2705558348226
x3 1.54646959816705
x4 0.000816371359806575
x5 -0.894039944060641
x6 1.88605932702964
x7 0.0545283992510777
x8 0.330003462902014
3:
x1 0.817677432070711
x2 -0.734946906055547
x3 -5.97395677713386
x4 -7.07117349295853E-5
x5 -0.894039944060641
x6 0.282420925065193
x7 0.364258018370474
x8 0.329996089923976
4:
x1 5.57459460098411
x2 0.321268890484856
x3 0.205761174232818
x4 0.000833138706775475
x5 0.100000000000003
x6 5.66820754494582
x7 0.018143343701659
x8 0.330004329039833
5:
x1 0.360923161734683
x2 -0.0228328849580109
x3 -0.666063660048423
x4 -6.12135370250361E-7
x5 -0.894039944060643
x6 0.0786951613776357
x7 1.3084212647888
x8 0.329973615235634
6:
x1 2.145899354053
x2 4.9676878398387
x3 14.1405959014085
x4 0.00289857324146092
x5 0.100000000000002
x6 1.27534324528113
x7 0.0806420939672824
x8 0.330002841272532
Alex Sha on 3 Feb 2020
Hi, Tekin, you may download a software package GAMS trial version from https://www.gams.com, Baron solver is included, the code for first case (slight changed： “-2664/x” -> "-2664/(x-0.01)", avoided devision by zero) looks like:
variables obj, x;
equations defobj, con1;
defobj.. obj =e= 1;
con1.. 60/(x-0.85) =e= -2664/(x-0.01);
model m / all /;
option nlp=baron;
solve m minimizing obj using nlp;
option decimals=8;
display obj.l, x.l;
It gives result:
VARIABLE obj.L = 1.00000000
VARIABLE x.L = -1.0000E+51
the x value obtained by Baron is -1.0000E+51
Reformulation code:
variables obj, x;
equations defobj, con1;
defobj.. obj =e= 1;
con1.. 60*(x-0.01) =e= -2664*(x-0.85);
model m / all /;
option nlp=baron;
solve m minimizing obj using nlp;
option decimals=8;
display obj.l, x.l;
result
VARIABLE obj.L = 1.00000000
VARIABLE x.L = 0.83149780
the x value is 0.83149780

John D'Errico on 25 Jan 2020
Edited: John D'Errico on 25 Jan 2020
This same question gets asked on literally a daily basis. The problem is not fsolve. It lies in understanding nonlinear equations, and things like basins of attraction. Finally, part of the problem lies in understanding numerical computation in floating point arithmetic, because you have lots of exponents in there. Tiny changes in a parameter will result in huge differences, perhaps in some terms suddenly going into the complex domain.
Without looking seriously at your equations, except to note they form an incredible mess of terms, perhaps even a hundred lines or so in length. I see no trig functions or other periodic functions, so I'll predict there are problably only a few dozen or so real solutions. There will almost certainly be multiple solutions, if any solutions exist at all. (The presence of periodic functions will often turn a problem with finitely many solutions into usually one with infinitely many solutions.)
For example, here is ONE of the 8 equations posed to be solved:
F(1) = - (x(6)^(4-4*n)*((x(1)^2*n^2*x(6)^(2*n-2)*(r-1)^2*(c-1)^2*(x(1)*x(6)^n*(x(1)*x(4)*n*x(6)^(n-1)*(r-1)*(c-1)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)*(r-1)*(c-1))*(x(5)^2 + x(1)*n*x(6)^(n-1)*x(5)*(r-1)*(c-1)+1)^2)/(x(1)^7*x(3)*n^4*x(6)^(3*n)*(exp(-x(6)*x(7))+1)^(1/x(8))*(x(5)^2+1)*(n-1)^3*(f-1)^3*(r-1)^4*(c-1)^4*(1/(exp(-x(6)*x(7))+1)^(1/x(8))-1)) - a;
I should probably have split it into multiple lines to make it possible to read without scrolling too far to the right.
ANY numerical solver is sensitive to initial guesses. Change the start point, get a completely different result. This is the concept of a basin of attraction, thus the set of start points that will result in any given solution. A basin of attaction can sometimes be a very strangely shaped set, especially so in 8 dimensions.
As I said, your problem is not in fsolve. It lies in assuming that you can just throw any complete mess of computations at a computer and expect a reasonable result, without also understanding the methods that must be used to solve the problem.
Alex Tekin on 26 Jan 2020
Unfortunately, making a system less complicated is not an option for me, so either I have to keep on trying to solve it (or get as many of those solutions as possible), or I just have to totally give up.
By the way, I tried vpasolve for the above problem, and it performs even worse than fsolve. Even when I insert the initial guesses that are extremely close to the solutions, vpasolve returns an empty answer while fsolve does not. I have checked manually, for the first three equations vpasolve encounters no problem. Second I introduce the fourth one, it returns empty set, even though there is cleary a solution.
I do understand that no numerical technique is meant to be “perfect”, and computers are not meant to be all powerful. However, the truth is, fsolve and vpasolve in MATLAB will not solve fairly complicated problems, and occasionally even if one starts quite close to the solutions. This is certainly obvious for more experienced users, but not for those who are fairly new to programming and numerical techniques.
I am not technically equipped to deeply research numerical approximation techniques, so I would like to request further help, or some reference to the codes that can be used in MATLAB (in place of fsolve vpasolve) that can more successfully tackle the above problem. I know there has been some good research on generic algorithms etc but I could not find a straightforward example, or user-supplied code to apply them to this particular problem. Thanks.

R2016b

### Community Treasure Hunt

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

Start Hunting!