[Topic] three-part method and Newton Iteration Method

Source: Internet
Author: User
Tags ranges

The following two iteration methods are summarized: three-way iteration and Newton iteration.

1. Three-way

As the most common method of divide and conquer, the bipartite method is applicable to monotonic functions and is used to approximate the value of a certain point. However, when a function is a convex function, the bipartite method cannot be applied. In this case, the three-way method can be used. The following convex functions:

Like binary definition, left and right

Mid1 = (left + right)/2mid2 = (mid2 + right)/2 If mid1 is near the extreme point, Left = mid1 if mid2 is near the extreme point, Right = mid2

The trigger principle is to remove 2/3 for each search.

The program template is as follows:


Double calc (type A) {/* calculate */} void solve (void) {double left, right; double mid, midmid; double mid_value, midmid_value; left = min; Right = max; while (left + EPS <right) {mid = (left + right)/2; midmid = (Mid + right)/2; mid_area = calc (MID); midmid_area = calc (midmid); // assume that the maximum extreme value is obtained. if (mid_area> = midmid_area) Right = midmid; else left = mid ;}}

2. Newton Iteration

The Newton Iteration Method, also known as the Newton-raphsonmethod, is a numerical solution for finding nonlinear equations through iterative calculation in real and complex fields. The basic idea of the method is to use a root guess value.X0 for the initial approximate value, use the FunctionF(X) InXThe first two items of the Taylor series formula at 0 are used as functions.F(X. Because the expression is a linear function, the equation is replaced by a linear expression.F(X)
= 0F(X) Obtain the approximate solutionX1. Forthcoming EquationF(X) = 0 inXReturns an approximate solution from a local line at 0.X1. repeat this process and convert the equationF(X) = 0 inXThe first part is calculated by local linearity.X2. Obtain the approximate solution.X2 ,.......

The biggest advantage of the Newton Iteration Method is its fast convergence speed and second-order convergence. Take the famous square root algorithm as an example to illustrate the significance of second-order convergence speed.

Example Analysis

Poj 3301 Texas trip

Theme

Given the point set in the plane, find the smallest Square area that can cover these points.

Question Analysis

The problem is that the smallest square is required. If the side of this square is parallel to the coordinate axis, that is, the square is not rotated at a certain angle, we only need to consider the top, bottom, and leftmost, the rightmost point is enough. When a square is rotated over a certain angle D, we only need to consider the Distance Difference Between the vertices on the edge. (therefore, we can use the enumeration rotation angle method to solve this problem, but pay attention to the step size selection to ensure accuracy ).

Assuming that the rotation angle is d, the maximum value of the linear distance between each two points about the rotation angle D can be enumerated to ensure that all points are covered. The distance in the two directions is:

  dis1 = fabs(cos(d) * (y[i] - y[j]) - sin(d) * (x[i] - x[j]))  dis2 = fabs(sin(d) * (y[i] - y[j]) + cos(d) * (x[i] - x[j]))

The above formula is Well deduced. Just draw the coordinate system and analyze it.

Of course, the dis1 and dis2 sizes are not necessarily the same. To ensure that the graph covers the square of all vertices, we select the longer side for the larger side.

Precision problem: To solve this problem, we can iterate m times. Each iteration divides the last obtained optimal value into N parts, and re-rotates to obtain the optimal value. Take n = 1000, M = 10 to get the exact value.

Source code

// Three-Point Rotation Angle in the range of 0 to 90 degrees // rotation formula: x' = x * Cos (PHI)-y * sin (PHI) y' = x * sin (PHI) + y * Cos (PHI) # include <iostream> # include <cstdio> # include <cmath> using namespace STD; struct point {Double X, Y;} p [31]; int N; const double Pi = ACOs (-1.0); const double EPS = 1e-4; double area (double A) {return a * A;} double max (double, double B) {return A> B? A: B;} double CAL (Double Z) {int I; Double X, Y; double max_x =-10000.0, max_y =-10000.0, min_x = 10000.0, min_y = 10000.0; for (I = 0; I <n; I ++) {x = P [I]. x * Cos (Z)-P [I]. y * sin (z); y = P [I]. x * sin (z) + P [I]. y * Cos (z); If (max_x <X) max_x = x; If (max_y <Y) max_y = y; If (min_x> X) min_x = X; if (min_y> Y) min_y = y;} return max (area (max_x-min_x), area (max_y-min_y);} double Div () {double left = 0, right = PI/2.0; double ans1, ans2; double M, mm; do {M = (left + right)/2.0; Mm = (right + M)/2.0; ans1 = CAL (m); ans2 = CAL (mm); If (ans1 <ans2) Right = mm; else left = m;} while (FABS (ans1-ans2)> EPS ); return ans1;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N ); int I; for (I = 0; I <n; I ++) scanf ("% lf", & P [I]. x, & P [I]. y); printf ("%. 2lf \ n ", Div ();} return 0 ;}
Question recommendation

The specific implementation of the three-way method is analyzed based on several OJ questions.

BUAA 1033 easy problem
Http://acm.buaa.edu.cn/oj/problem_show.php? C = 0 and P = 1033

Finding a point on a line segment is the least distance from a given point P. Obviously, the convex function is solved by three points.
The calc function is used to calculate the distance between a certain point and a certain point.

Zoj 3203 light bulb
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3203.

The longest length of the Shadow L.
According to the figure, it is easy to find that when the light, the person's head and the corner are in a straight line (assuming that the person is standing at a point), the length at this time is the maximum length of the shadow on the ground. When a person goes to the right, the shadow begins to be projected onto the wall. When a person sticks to the wall, the length of the shadow is the height of the person. So when a person moves from point A to the wall, the function increments first and then decreases, which is a convex function, so we can use the three-way method to solve the problem.

Only the calc function is provided below, and Other templates can be directly set.
Double calc (Double X)
{
Return (H * D-H * X)/(d-x) + X;
}

Heru 5081 turn the corner 08 Harbin Regional Network Competition
Http://acm.hrbeu.edu.cn/index.php? Act = problem & amp; id = 1280

The turning of a car is determined by X, Y, L, and D. First, when X or Y is less than D, it must not.
Secondly, we find that with the increase of angle θ, the maximum height H increases first and then decreases, that is, the convex function. We can use the three-way method to solve the problem.

The calc function here requires a complicated formula:
S = L * Cos (θ) + W * sin (θ)-X;
H = S * Tan (θ) + W * Cos (θ );
S is the horizontal distance from the rightmost point of the car to the corner, H is the highest distance from the inner inflection point, and θ ranges from 0 to 90.

Poj 3301 Texas trip
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 3301

Given n (n <= 30) points, find the square with the smallest area filled with these points.

There are two solutions, one is the approximation method, that is, the M-point angle each time, find the most consistent angle, and then continue M-points, so that the times, you can find a more accurate solution. (M is about 10, and times is about 30)

The second solution is the three-way method. First, the rotation angle must be between 0 and 180 degrees, and the rotation angle must be greater than 180 degrees. After the coordinate axis is rotated, the coordinates are changed:
X' = x * cosa-y * Sina;
Y' = y * cosa + x * Sina;

HDU 3400 line belt
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3400
The typical three-way method is to first take the first line segment by three minutes, find a point, and then use the second line segment by the third. The idea of the three points is basically enough.

For solving some practical problems, when the formula is difficult to derive, the two-or three-way method can accurately solve some critical values, and the efficiency is satisfactory.

The calc function here requires a complicated formula:
S = L * Cos (θ) + W * sin (θ)-X;
H = S * Tan (θ) + W * Cos (θ );
S is the horizontal distance from the rightmost point of the car to the corner, H is the highest distance from the inner inflection point, and θ ranges from 0 to 90.
Poj 3301 Texas trip

Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 3301

Given n (n <= 30) points, find the square with the smallest area filled with these points.

There are two solutions, one is the approximation method, that is, the M-point angle each time, find the most consistent angle, and then continue M-points, so that the times, you can find a more accurate solution. (M is about 10, and times is about 30)

The second solution is the three-way method. First, the rotation angle must be between 0 and 180 degrees, and the rotation angle must be greater than 180 degrees. After the coordinate axis is rotated, the coordinates are changed:
X' = x * cosa-y * Sina;
Y' = y * cosa + x * Sina;

HDU 3400 line belt

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3400

The typical three-way method is to first take the first line segment by three minutes, find a point, and then use the second line segment by the third. The idea of the three points is basically enough.
For solving some practical problems, when the formula is difficult to derive, the two-or three-way method can accurately solve some critical values, and the efficiency is satisfactory.

Three-way BUAA 1024 BUAA 1033 easy problem zoj 3203 light bulbhdoj 3400 line belthdoj 2438 turn the corner Newton Iteration poj 1905 expanding rodspoj 3122 piepoj 2728 desert kingpoj 2868 computer djpoj 3727 Newton's method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.