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

8 views (last 30 days)
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 Comments
Matt J
Matt J on 29 Jan 2020
Your TolFun setting of 0.001 looks suspiciously lax. It will encourage the solver to stop iterating very early. I doubt it's the only cause of your problems, but I would go to TolFun=1e-10 or so.
Alex Tekin
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)."

Sign in to comment.

Accepted Answer

Alex Sha
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
  18 Comments
Alex Tekin
Alex Tekin on 3 Feb 2020
Edited: Alex Tekin on 3 Feb 2020
Hi Alex Sha,
Thanks a lot!
I just used your 1st example in Baron w/o reformulation, and even though I give large range for bounds and start far away from the original solution, it gives me a right answer in no time. I am surprised you Baron requires reformulation for this example. This is my code:
clc
format long
tic
options = baronset('DeltaTerm',0,'MaxTime',500,'filekp',1);
%Objective
fun = @(x) 0;
%Nonlinear Constraints
nlcon = @(x) [60/(x(1)-0.85)-2664/x(1)];
cl = [0];
cu = [0];
%Bounds
lb = [0];
ub = [1000];
% Starting Guess
x0 = [1000];
%Solve
[x,fval,exitflag,info] = baron(fun,[],[],[],lb,ub,nlcon,cl,cu,[],x0,options)
toc
Alex Sha
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

Sign in to comment.

More Answers (1)

John D'Errico
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) - x(1)*x(6)^n*(x(2)*(x(5) + x(5)^2 + x(1)*n*x(6)^(n-1)*(r-1)*(c-1) + x(1)*n*x(6)^(n-1)*x(5)*(r-1)*(c-1)+1) + x(1)^2*x(3)*n*x(6)^n*x(6)^(n-1)*u*(n-1)*(r-1)*(c-1))*(x(5)^2+1)*(n-1)*(f-1))*(n-1)*(f-1) + x(1)*x(4)*x(6)^n*x(5)*(x(5) + x(1)*n*x(6)^(n-1)*(r-1)*(c-1))*(x(5)^2+1)*(n-1)*(f-1)) + x(1)*x(4)*x(6)^n*(n-1)*(f-1)*(2*x(5)^2 + 2*x(5)^4 + x(5)^6 + x(1)^2*n^2*x(6)^(2*n-2)*x(5)^2*(x(5)^2 + 2)*(r-1)^2*(c-1)^2 + 2*x(1)*n*x(6)^(n-1)*x(5)*(x(5)^2+1)^2*(r-1)*(c-1)+1))*(1/(exp(-x(6)*x(7))+1)^(1/x(8))-1) - x(1)^2*x(4)*n^2*x(6)^(2*n-2)*(x(1)*x(6)^n*(n-1)*(f-1) - x(1)^2*n*x(6)^n*x(6)^(n-1)*(n-1)*(f-1)*(r-1)*(c-1))*(r-1)^2*(c-1)^2 + x(1)^3*n^2*x(6)^n*x(6)^(2*n-2)*(x(5)^2+1)*(x(4)*x(5)*(x(5) + x(1)*n*x(6)^(n-1)*(r-1)*(c-1)) + x(1)^2*n^2*x(6)^(2*n-2)*(x(4) - x(1)*x(2)*x(6)^n*(n-1)*(f-1))*(r-1)^2*(c-1)^2)*(n-1)*(f-1)*(r-1)^2*(c-1)^2*(1/(exp(-x(6)*x(7))+1)^(1/x(8))-1)))/(x(1)^7*x(3)*n^4*x(6)^(3*n)*(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)) - (x(4)*x(6)^(4-4*n)*(x(1)*x(6)^n*(n-1) - x(1)*x(6)^n*f*(n-1) + x(1)^2*n*x(6)^n*x(6)^(n-1)*(n-1)*(f-1)*(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.
  3 Comments
John D'Errico
John D'Errico on 26 Jan 2020
I answered the same question a day or so ago, but the question, and my lengthy answer and followup comments got deleted with it.
Is there a way to find all solutions of a nonlinear multi-variate probem?
No, in general this is not possible. In fact, it is provably true that you cannot do so for many actually simple looking provblems. Nothing stops you from using multi-start methods with different random starting points to hope to gain a solution. But at best you can only hope to find perhaps many of the solutions. To know that you have found them all would require bounds on the derivatives of your function within regions, which could allow you to exclude a possible solution in that region. (I recall seeing work on global solvers some years ago, try to achieve exactly this sort of thing. Those ideas won't go anywhere on such a nasty problem. 8 dimensional spaces are big, and the derivatives of your expressions will be really nasty.)
Worse, the very fact that fsolve fails when given a start point away from the solution tends to imply that your function has nasty derivatives. Exponentials, and lots of exponents in general make that just expected.
Can I suggest a different method?
No. Too often I see people create massive complicated systems like this, then expecting their powerful computer to make short work of it. Not gonna happen most of the time. Computers are not yet that powerful. At least some of the time, you cannot even work with the expressions involved using double precision arithmetic. The problem is though, using higher precision is usually a route to failure too, since high precision computation is so slow to work with.
At best, you could consider using vpasolve, using different start points. So a numerical solver in the symbolic toolbox. Such an approach will be highly problematic, and even if you make any progress at all, it will be incredibly slow. Again, high precision symbolic computations are terribly slow.
My suggestion, if anything would work is to step back, to reformulate your problem. I presume this problem comes from some real world system, and is an approximation thereof. You need to reduce the complexity of your approximation, eliminating terms that are of little significance. This is a difficult thing to do, and requires sometimes great skill and true understanding of the problem. But done well, it can make a once impossible problem solvable.
Or, you can keep trying to hack this problem to death, hopefully before it does the same to you.
Alex Tekin
Alex Tekin on 26 Jan 2020
Thank you for your reply, which is informative and useful.
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.

Sign in to comment.

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!