Fsolve can't solve this simple nonlinear equation
26 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Romio
am 19 Mär. 2023
Bearbeitet: John D'Errico
am 19 Mär. 2023
I am trying to solve the following equation using fsolve (possibly for different values of c). Despite it is a simple equation, fsolve doesn't find a solution that is accurate. Any idea on how to overcome this
clear,clc,close all
c = 3;
eq = @(z) c - (z^3/3 - z);
tol = 1e-14;
options = optimoptions('fsolve','display','none','FunctionTolerance',tol,'OptimalityTolerance',tol,'MaxFunctionEvaluations',1e7,'MaxIterations',1e7);
[z0,fval,exitflag] = fsolve(eq,0,options)
0 Kommentare
Akzeptierte Antwort
John D'Errico
am 19 Mär. 2023
Bearbeitet: John D'Errico
am 19 Mär. 2023
c = 3;
eq = @(z) c - (z.^3/3 - z);
[xsol,fval,exitflag] = fsolve(eq,0)
fplot(eq,'b')
hold on
plot(xsol,fval,'rs')
grid on
yline(0,'g')
xline(0,'g')
Clearly you need to understand how an optimixation tool works. An optimization tool like fsolve does not use symbolic operations to manipulate the objective, finding exact values for the roots. Solve can do that, but fsolve is not solve. fsolve is an optimizer. It works as if the objective is a black box. It sends in values for x, then looks at what comes out. So a perfect black box. fsolve CANNOT see inside the black box.
The example I like to use is that of a blind person set down on the surface of the earth. The task is to find the lowest point on the surface of the earth. I'll be nice, and give the subject of this experiment a cane, so they can know which direction is down hill. As well, I'll give them an altimeter, one that works underwater. (That would be one slick altimeter.) And yes, I'll even give this subject some scuba gear, since it would probably be useful too.
But all the person can do is to search in the vicinity of where they are set down, and move to a new place that is better than where they are at now.
Now, suppose that I was in an unfriendly mood, and set them down in the vicinity of the Dead Sea? They will get lost in any local depression, unable to climb out. Do you think they will find the low spot in the bottom on a trench in the Pacific Ocean from that start point? Of course not!
Now, what did you do? You set fsolve down at a point in a local depression, starting at x==0. DO you think it can somehow do better than the blind person around the Dead Sea? OF COURSE NOT!
fsolve ends up at eactly the point it found. It returns an exitflag that tells you it thinks it has failed to find a solution. READ THE MESSAGE IT RETURNS.
The solution is to use better starting values. Just as if you want the blind person to have a chance of finding the Marianas trench, it would be best to set them down in the Pacific ocean, and possibly near where the solution is when the surface can be so difficult. Complicated functions sometimes require good starting values.
[xsol,fval,exitflag] = fsolve(eq,3)
And this time, as long as I start fsolve in a reasonable place, it has no problem at all.
The concept of a basin of attraction is an important one. Given any set of possible solutions to an optimization problem, (some of which are not solutions at all, but merely stable points where no further progress can be made), each solution will have a basin of attraction. That is the set of points, such that when you start out the solver at those points, it will terminate at or near a specific solution. Different solvers may have different basins of attraction, depending on the algorithmic details in the implementation.
It is my guess that you were assigned this homework problem to learn the concept of a basin of attraction. A fun assignemnt is to have the student map out the basins of attraction for some specific function/solver combination. Luckily, I'm not your teacher, else that would have been your assignment on this problem. An interesting question is exactly how many basins of attraction does this specific problem have? Must a basin of attraction be a contiguous set?
If you truly need to find the solution ALWAYS, then you need to use a tool designed to solve for all solutions to a polynomial prolem. Use the right tool to solve any problem. But that often means you need a solid understanding of the tools you will use. And you need to fully understand the problem you are trying to solve, as this allows you to best choose the correct tool. For example:
syms Z
zsol = solve(eq(Z),'maxdegree',3)
vpa(zsol)
I'm sorry, but the failure of fsolve here was really your failure to use the tool properly, to understand how it works, and to understand optimization in general.
(Disclaimer: No blind people were harmed in this thought experiment. gurgle, gurgle, gurgle.)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Surrogate Optimization finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!