[Mathematical algorithms] integral algorithms (1)

Source: Internet
Author: User

When I was in elementary school, I learned the ball's volume formula V = (4/3) π R weight. At that time, I thought it was amazing, is it possible for someone who has obtained this formula to melt an iron into molten metal and put it in a rectangular container? It wasn't until I went to school that I realized that I had used points for calculation. Of course, the calculus may hate more than love for many students, including me, however, it is undeniable that points play an irreplaceable role in almost all science and technology disciplines. As a result, bloggers write an algorithm to calculate points. Because there are too many algorithms, in order to avoid excessive length, I decided to write the integral algorithm in four parts to make readers feel tired.


The only convenient and unified algorithm. This article uses this basic formula as an example.

1. Random projection method (Monte Carlo algorithm)
This method has been mentioned once in the article for calculating the circumference Rate Link: http://blog.csdn.net/nash_/article/details/8199357In the range of A to B and the rectangle composed of functions, N points are randomly cast, and the number of green shadow points is M, for this image, it is easy to know that the integral value (green shadow) is (M/N) * rectangular area.
Code List:
public class JiFen {public static void main(String[] args){int N = 1000000;int count = 0;for(int i = 0; i < N; i++){double x = Math.random();double y = Math.random();if(f(x) >= y)count++;}System.out.println((double)count/N);}private static double f(double x) {return x*x;}}

Output: 0. 3333022. Another Monte Carlo Method
The first method is special depending on the situation. If it is in the integral form, for example:
In general cases, if the first algorithm is used, it is necessary to determine whether the random point is cast above or below the X axis. for the selection of the rectangle, it is also divided into F (a), F (B) whether it is the same number and the size of the absolute values of f (a) and F (B) are troublesome. Therefore, another projection method is generated: n random points x1 ~ are generated in the range A to B ~ XN, the integral value is(F (X1)
+ F (X2) +... + f (Xn) * (B-a)/m
. Code List:

Public class jifen_toudian2 {public static void main (string [] ARGs) {double A = 0; Double B = 1; double area = getarea (a, B); system. out. println (area);} public static double getarea (double A, double B) {int Sign = 1; // If (A> B) {double T = A; A = B; B = T; Sign =-1 ;}int n = 10000; double sum = 0; For (INT I = 0; I <n; I ++) {Double X = a + (B-a) * Math. random (); // generate a random sum + = f (x);} return (sum * (B-a) point in the range of (a, B) /n) * sign;} Private Static double F (Double X) {return x * X ;}}

Output: 0. 339663254655035053. Definition Integral Method
Recall how points are obtained at first, which is to divide an area into N small rectangles, calculate the area sum, and finally calculate the limit. We use this method to implement it, but our n is limited after all. To make the result more accurate, change the rectangular area to the trapezoid area (of course, the rectangle is also acceptable), such: divide (a, B) into N equal points, and the integral value is equalS1 + S2 +... + SN, where Si = (f (xi) + f (x (I + 1) * (B-a)/n/2(Rectangular area formula)
With the previous foundation, you can easily write programs.
Code List:

public class JiFen3 {/** * @param args */public static void main(String[] args) {double a;double b;double y;a = 0;b = 1;y = getArea(a, b);System.out.println(y);}static double f(double x){return x*x;}static double getArea(double a, double b){int sign = 1;if(a > b){double t = a;a = b;b = t;sign = -1;}double h;double x;double sum = 0;int n = 10000;h = Math.abs(a - b)/n;x = a;for(int i = 0; i < n; i++){sum += f(x) + f(x + h);x = x + h;}return sum * h / 2 * sign;}}

Output: 0.3333333349999429

4. Step-changing trapezoid Integral Method
It is very difficult to determine the error of the integral by using the definition-based integral method. We cannot find a correct N value that meets our requirements. Therefore, we need to improve the definition method, the improved method is to change the original fixed step size to a variable step size, and use the bipartite method, for example, Figure 4-1, Figure 4-2, and Figure 4-3... When should we get it? Assigned to | The latter area and the former area and | <specified Error. In this way, we have achieved the goal of accuracy.
Code List:
Public class jifen_bianchang {static Double E = 0.00001; // error public static void main (string [] ARGs) {double A = 0; // lower limit of points Double B = 1; // credit limit double area = getarea (a, B); system. out. println (area);} public static double getarea (double A, double B) {int Sign = 1; // positive and negative signs if (A> B) {double T =; A = B; B = T; Sign =-1;} double S1 = 0; // The first area and double S2 = 0; // The next area and double H = B-A; S2 = getonearea (a, B, H); For (INT I = 2; math. ABS (S1-S2)> E; I * = 2) {double T = H/I; // each trapezoid height double sum = 0; Double X =; for (Int J = 0; j <I; j ++) {// obtain the trapezoid and sum + = getonearea (x, x + T, T ); X = x + T;} S1 = S2; S2 = sum;} return sign * S2;} public static double getonearea (double A, double B, double H) {return (f (a) + F (B) * H/2;} public static double F (Double X) {return x * x ;}}
Output: 0.33333587646484375 integral algorithm (2) (to be continued) integral algorithm (3) (to be continued) integral algorithm (4) (to be continued)

========================================================== ========================================================== ============================

Author: Nash _ Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/nash_/article/details/8560759

========================================================== ========================================================== ==============================

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.