MATLAB: Non linear equation with single root

MATLABnonlinear

I am trying to solve this
fun1=@(x)(x-0.25).^2+((1628.1/6.08.*(720-720*x))-0.79).^2
using fzero but my answer is NaN
is there anyway to get a real value answer for this equation
?

Best Answer

fzero does not find a root, cannot find it, because it has no zero crossing. That is a requirement for fzero. Effectively, there is not single root, but what looks like it might be a double root.
fplot(fun1,[0.999,1.001])
But is it?
syms x
y = (x-0.25).^2+((1628.1/6.08.*(720-720*x))-0.79).^2;
vpa(solve(y),5)
ans =
1.0 - 3.89e-6i
1.0 + 3.89e-6i
So no real root at all. In fact, this is just a quadratic polynomial with two complex roots.
vpa(expand(y),5)
ans =
3.7172e+10*x^2 - 7.4344e+10*x + 3.7172e+10
In general, for a function that actually had a real double root at some point, fzero is not a good choice of solver. For example, consider this function where a real root is known to exist.
fun2 = @(x) (x - 1.25).^2;
>> fzero(fun2,2)
Exiting fzero: aborting search for an interval containing a sign change
because NaN or Inf function value encountered during search.
(Function value at -1.7162e+154 is Inf.)
Check function or try again with a different starting value.
ans =
NaN
>> fzero(fun2,[0,2])
Error using fzero (line 290)
The function values at the interval endpoints must differ in sign.
As you see, fzero cannot survive here, even on this simple problem, because the function never crosses zero.
Yet fsolve works with no problem, finding a root that is within its tolerances.
[xsol,fval] = fsolve(fun2,1.5)
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
xsol =
1.2578
fval =
6.1035e-05
It did not do a great job, as problems with double roots are more difficult to solve exactly in floating point arithmetic.