For example you have to find the root of Equation
2 * SIN(x) = x
Transform this equation to f(X) = 2 * SIN(X) - X = 0.
One root could be find between -2 and -1 because
f(1) * f(2) = (2 *Sin(-1) + 1) * (2 * Sin(-2) + 2) = -1.23889187945779E-001 < zero.
So Xleft = -2.0 and Xright = -1.0....
When we find the boundaries we will iterate by finding the middle of the interval:
So, the first iteration of the X will be (-1.0 - 2.0) / 2 = -1.5.
Then we find where f(X) * f(Xright), or f(X) * f(Xleft) is less then 0.
If f(X) * f(Xright) > 0 we change Xright on (Xleft + Xright) / 2,
If f(X) * f(Xleft) > 0 we change Xleft on (Xleft + Xright) / 2.
We will finish the process when abs (Xleft - Xright) <= eps
with control_tbl (Equation, Xleft, Xright, eps) as (select '2 * sin(X) = X ==> 2 * sin(X) - X = 0', double(-2), double(-1), double(1.e-12) from sysibm.sysdummy1 ) , eq_solution (Equation, Xleft, Xright, eps, X) as (select ct.*, (Xleft + Xright) / 2 from control_tbl ct union all select Equation, case when (md.E_Function * lf.E_Function ) > 0 then (Xleft + Xright) / 2 else Xleft end, case when (md.E_Function * rt.E_Function ) > 0 then (Xleft + Xright) / 2 else Xright end, eps, (Xleft + Xright) / 2 from eq_solution eq, table(select 2 * sin (X) - X as E_Function from (select Xleft as X from sysibm.sysdummy1 ) ii) lf, table(select 2 * sin (X) - X as E_Function from (select Xright as X from sysibm.sysdummy1 ) ii) rt, table(select 2 * sin (X) - X as E_Function from (select (Xleft + Xright) / 2 as X from sysibm.sysdummy1 ) ii) md where abs(Xleft - Xright) > eps ) select Equation, abs(Xleft - Xright) "Real Accurancy", X "Equaty Solution" from eq_solution where abs(Xleft - Xright) <= eps
Result:
X = -1.89549426703434
P.S.
We can round result in same number of digits in fraction which has eps. ==> X = -1.895494267034
Lenny Khiger, ADSPA&VP