fsolve stopped because the problem appears regular
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I am trying to solve a simple problem, I have to finde the root of:
f(x)=b3*x^3 + b1*x + b0
But I'm somehow stuck with the output:
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.
My script says:
b3 = -1;
b1 = 2;
b0 = -2;
f =@(x) (x^3)*b3 + x*b1 + b0;
x0=1;
los = fsolve(f, x0);
Do I have to change some output-parameters/options or what? Because changing the starting point doesn't help.
Would appreciate some help!
6 comentarios
Mario_mr_nice
el 13 de Mzo. de 2018
5.5778e-13, so very near zero.
But in the last comment it was my mistake, it said "fsolve completed...", so the problem is solved. Thx for the help.
Respuestas (1)
John D'Errico
el 13 de Mzo. de 2018
Why in the name of god and little green apples would you use fsolve to solve for the roots of a 1-d polynomial problem? Oh. Yes. That is right. This is homework, and they told you to use fsolve.
Fsolve will fail to find the roots given that start point. Yes, changing the start point WILL help!!!
First, write f properly! Learn to use the .* operator for multiplication and the .^ operator to exponentiate, each as necessary. Since b0,b1,b3 are all scalars, .* is not needed here. But .^ will be useful.
f = @(x) (x.^3)*b3 + x*b1 + b0;
ezplot(f,[-2,2])
grid on

See that the start point is just to the right of that hump. But where is the root?
So there is only one real root.
p = [b3,0,b1,b0];
roots(p)
ans =
-1.76929235423863 + 0i
0.884646177119316 + 0.589742805022205i
0.884646177119316 - 0.589742805022205i
Roughly at x = -1.77.
But where are the flat spots?
roots(polyder(p))
ans =
0.816496580927726
-0.816496580927726
When you start the solver at 1 though fsolve looks around, realizes the derivative tellit to decrease x. But if it tries to decrease x, it gets as far as 0.8164... and realizes that if it goes any further, decreasing x actually pushes the function AWAY from zero.
So fsolve gives up at this point. It tells you that it has found a point where it cannot proceed, yet it knows this is not a solution.
So instead, what happens when we start out at a different location?
[los,fval] = fsolve(f, -1);
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
What results did we get?
los
los =
-1.76929235423871
fval
fval =
5.57776047571679e-13
So it did indeed find the root. And fsolve knows that it converged happily, telling you that fact.
As I said, fsolve works here, IF you use it properly. But why you would choose to use fsolve when roots is far more intelligent choice, only your instructor knows. But my guess is, you were told you needed to use fsolve to learn how and why it may fail.
1 comentario
Mario_mr_nice
el 13 de Mzo. de 2018
Thanks a lot for this answer!
Yeah I guess my teacher wanted us to see that fsolve fails with the wrong x0. Also you gave a good explanation why. Thanks for that.
Ver también
Categorías
Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!