Numerical method for integral explanation + template Code _-----Problem Solving template of the number theory-----

Source: Internet
Author: User
What is a numerical integral

The numerical integral can be used to calculate the approximate value of the definite integral. For many functions, we can use the elementary function to express its integral, and for this function, we can get the definite integral by simply asking for indefinite integral and substituting the value.

But there are many other functions that are difficult to find and functions that cannot be represented by elementary functions. When we want to ask for their definite integral, we need to use numerical integral to solve.

In the ACM some topics need to use numerical integration to solve, listed below some of the methods for numerical integration, from simple to difficult, and the most important for Acmer is the composite Simpson, its precision is high, and adjustable precision, is a tool to do integral geometry.

I learned from this: NetEase Open class MIT numerical integration

The opportunity to learn is to want a to drop this question: Zoj 3898 My method of solving a Riemann and

Riemann sum is used to divide the interval equal length into n segments, then the rectangle is used to approximate the function, the length of each paragraph is Δx.

You can select the function value to the left of each paragraph as the height of the rectangle, or you can select the function value to the right of each paragraph as the rectangle's height.

If you set the N+1 function value from left to right x0,x1⋯xn, you can get the following formula:
Left Hand riemann=right Hand riemann=δxf (x0) +δxf (x1) +⋯+δxf (xn−1) =δx∑i=0n−1f (xi) δxf (x1) +δxf (x2) +⋯+δxf (xn) =δx∑i=1nf ( XI) This approximation is rough, but it can better convey the concept of numerical integration.

Method Two • Trapezoidal method

Riemann and although simple, but the precision is worrying, can not well simulate approximation function, next to introduce the second method.

We can see that with the rectangle approximation there are many vacancies, the use of trapezoidal to approximate, can greatly improve the accuracy, looks very much like.

Using the preceding XI, we can get the following formula from the trapezoid formula:
Trapezoid=δx (f (x0) +f (x1)) 2+δx (f (x1) +f (x2)) 2+⋯+δx (f (xn−1) +f (xn)) 2=δx (f (x0) 2+f (x1) +⋯+f (xn−1) +f (xn) 2) =left Hand Riemann+right Hand Riemann2
It can be seen that the trapezoid method is the average of the left and right Riemann.

The first and last part of the formula is divided by 2, and the other 1 can be combined. Law Three · Simpson formula

The first two are relatively simple, but the accuracy is poor, the third method is to use a parabolic approximation.

In fact, I do not know how to calculate, is from the public class to school.

Use the Simpson formula to first require N to be even.

The whole range is divided into N2 segments, the bottom of each segment is 2δx, the height is difficult, but there are formulas: Simpson height=f (XL) +4f (XM) +f (XR) 6

So there's the formula:
Simpson=δx3 (f (x0) +4f (x1) +2f (x2) +⋯+2f (xn−2) +4f (xn−1) +f (xn))

Method four • Compound Simpson

The accuracy of the Simpson formula is actually quite good, but there is still a gap relative to the required precision in ACM.

In order to improve the accuracy, we need to reuse the Simpson formula many times.

We first define the integrand as f (x), define Simpson (l,r) = (r−l) f (L) +4f (L+R2) +f (r) 6

This is also the conventional Simpson formula, and then defines the function Rsimpson as compound Simpson.

When we asked Rsimpson (L,r), shilling m=l+r2

When Simpson (l,r) ≈simpson (l,m) +simpson (m,r) We think that the precision is enough to return one of them.

And when it's not enough, we'll step back and ask Rsimpson (l,m) +rsimpson (m,r)

The accuracy is higher, and the accuracy can be adjusted by defining the range of ≈.

The summary formula is as follows:
Rsimpson (l,r) ={simpson (l,r) Approximatersimpson (l,m) +rsimpson (m,r) Else

The realization of compound Simpson

Inline double getappr (double FL, double FM, double FR, double L, double R) {return
    (FL+4*FM+FR) * (r-l)/6.0;
}

double Simpson (double L, double, double fl, double fr) {
    Double m = (l+r)/2, LM = (l+m)/2, RM = (r+m)/2;
    Double fm = f (m), flm = f (lm), frm = f (rm);
    Double VLR = GETAPPR (FL, FM, FR, L, R);
    Double VLM = GETAPPR (fl, FLM, FM, L, m);
    Double VRM = GETAPPR (FM, frm, FR, M, R);
    return Fabs (VLR-VLM-VRM) < EPS? Vlr:simpson (L, M, FL, FM) +simpson (M, R, FM, FR);

Implementation of Composite Simpson 2 (natureal code)

Inline double getappr (double l,double r) {return
    (f (L) + 4.0*f ((l+r)/2) + F (r)) * (R-L)/6.0;
}

Double Simpson (double l,double r) {
    Double sum = GETAPPR (l,r);
    Double mid = (l+r)/2;
    Double suml = GETAPPR (l,mid);
    Double SUMR = GETAPPR (mid,r);
    Return (Fabs (SUM-SUML-SUMR) < EPS)? Sum:simpson (L, Mid) + Simpson (Mid, R);
}


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.