HDU 1724 ——————自适应辛普森法

版权声明:转载请注明出处 https://blog.csdn.net/Hpuer_Random/article/details/83311967

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=PIab )

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));
}

猜你喜欢

转载自blog.csdn.net/Hpuer_Random/article/details/83311967
今日推荐