In numerical calculation, it is very important to obtain the approximation of a root, because when using other methods such as Newton iteration, we often need to find an initial point, the initial vertex should be selected near the root. In addition, only when the approximate root is known can the overall conversion be local. I have just learned the numerical calculation method and have a limited understanding. Let's discuss it together.
Function file approot_my.m
Function r = approot_my (f, x, epsilon)
Y = f (x );
Yrange = max (y)-min (y );
Epsilon2 = yrange * Epsilon; % the description of Y is very small, that is, the vertex tangent to the X axis.
N = length (X );
M = 0;
X (n + 1) = x (n );
Y (n + 1) = y (N );
K = 2;
While k <= N
If (Y (k-1) * Y (k) <= 0)
M = m + 1;
R (m) = (x (k-1) + x (k)/2;
End
S = (Y (k)-y (k-1) * (Y (k + 1)-y (k); % Y Curve Inflection Point
If (ABS (Y (k) <epsilon2) & (S <= 0)
M = m + 1;
R (m) = x (k );
End
K = k + 1;
End
Test function F. M
Function Y = f (x)
Y = sin (COS (X. ^ 3 ));
Run
> X =-. 000;
> Approot_my (@ f, x, 0.001)
Ans =
-1.9878-1.6766-1.1625 1.1625 1.6766
The following figure shows the test function.
> X =-. 000;
> Y = sin (COS (X. ^ 3 ));
> Plot (X, Y, X, 0)