1. 程式人生 > >HDU 1724 ——————自適應辛普森法

HDU 1724 ——————自適應辛普森法

Ellipse

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2916 Accepted Submission(s): 1334

Problem Description

Math is important!! Many students failed in 2+2’s mathematical test, so let’s AC this problem to mourn for our lost youth…
Look this sample picture:

在這裡插入圖片描述

A ellipses in the plane and center in point O. the L,R lines will be vertical through the X-axis. The problem is calculating the blue intersection area. But calculating the intersection area is dull, so I have turn to you, a talent of programmer. Your task is tell me the result of calculations.(defined PI=3.14159265 , The area of an ellipse A=PIa

b )

Input

Input may contain multiple test cases. The first line is a positive integer N, denoting the number of test cases below. One case One line. The line will consist of a pair of integers a and b, denoting the ellipse equation在這裡插入圖片描述 , A pair of integers l and r, mean the L is (l, 0) and R is (r, 0). (-a <= l <= r <= a).

Output

For each case, output one line containing a float, the area of the intersection, accurate to three decimals after the decimal point.

Sample Input

2
2 1 -2 2
2 1 0 2

Sample Output

6.283
3.142


#include<bits/stdc++.h>
using namespace std;
const double PI = 3.1415926;
int a,b,l,r;

double F(double x)
{
        return sqrt(b*b*(a*a-x*x)/(a*a));
}
double simpson(double a, double b)
{
        double c=a+(b-a)/2;
        return (F(a)+4*F(c)+F(b))*(b-a)/6;
}
double asr(double a, double b, double eps, double A)
{
        double c=a+(b-a)/2;
        double L=simpson(a,c),R=simpson(c,b);
        if(fabs(L+R-A)<=15*eps) return L+R+(L+R-A)/15.0;
        return asr(a,c,eps/2,L)+asr(c,b,eps/2,R);
}
double asr(double a,double b,double eps)
{
        return asr(a,b,eps,simpson(a,b));
}

int main()
{
        int t;
        scanf("%d",&t);
        while(t--)
        {
                scanf("%d %d %d %d",&a,&b,&l,&r);
                printf("%.3lf\n",asr(l,r,(1e-6))*2);
        }
        return 0;
}



以下轉自 數值問題專題小結:自適應辛普森演算法求定積分

三點辛普森公式




該公式要求f(x)必須是一個全域性函式,用它可以近似的來求解一個定積分,但精度不夠高。因此衍生出一個重要的“變種”,稱為“自適應辛普森法”。

自適應辛普森法

(1)概述:自適應辛普森法(Adaptive Simpson's Rule)是一種數值積分方法,適用於無法求出原函式時的定積分。比直接用辛普森公式的精度更高,而且效率也可觀。

(2)原理:該演算法還是基於三點辛普森公式進行計算,不過需要設定一個精度eps,然後可以根據情況遞迴的劃分區間:容易近似的地方少劃分,不容易近似的地方多劃分。近似程度利用如下公式來判斷:

其中的三個S值是在對應的區間中利用“三點辛普森”公式計算出來的值。c是區間[a,b]的中點,ε就是上述的eps。如果滿足該不等式,就直接返回結果,這裡的結果指的是S(a,c)+S(c,b)+ΔS(ΔS就是上述不等式中小於號之前的部分),否則遞迴呼叫,即再次劃分區間。遞迴呼叫時精度也要相應地減小一半。

double F(double x)
{
	//Simpson公式用到的函式
}
double simpson(double a, double b)//三點Simpson法,這裡要求F是一個全域性函式
{
	double c = a + (b - a) / 2;
	return (F(a) + 4 * F(c) + F(b))*(b - a) / 6;
}
double asr(double a, double b, double eps, double A)//自適應Simpson公式(遞迴過程)。已知整個區間[a,b]上的三點Simpson值A
{
	double c = a + (b - a) / 2;
	double L = simpson(a, c), R = simpson(c, b);
	if (fabs(L + R - A) <= 15 * eps)return L + R + (L + R - A) / 15.0;
	return asr(a, c, eps / 2, L) + asr(c, b, eps / 2, R);
}
double asr(double a, double b, double eps)//自適應Simpson公式(主過程)
{
	return asr(a, b, eps, simpson(a, b));
}