2019年1月1日 星期二

辛普森積分法則

Simpson's rule 辛普森積分法則

From Wikipedia, the free encyclopedia
Jump to navigationJump to search

,In numerical analysisSimpson's rule is a method for numerical integration, the numerical approximation of definite integrals. Specifically, it is the following approximation for  equally spaced subdivisions (where  is even): (General Form)
where  and .






//Simpson's 1/3rd Rule for Evaluation of Definite Integrals
#include<iostream>
#include<cmath>
using namespace std;
double f(double x)
{
    double a=1/(1+x*x);    //write the function whose definite integral is to be calcuated here
    return a;
}
int main()
{    cout.precision(4);        //set the precision
    cout.setf(ios::fixed);
    int n,i;            //n is for subintervals and i is for loop
    double a,b,c,h,sum=0,integral;
    cout<<"\nEnter the limits of integration,\n\nInitial limit,a= ";
    cin>>a;
    cout<<"\nFinal limit, b=";                //get the limits of integration
    cin>>b;
    cout<<"\nEnter the no. of subintervals(IT SHOULD BE EVEN), \nn=";        //get the no. of subintervals
    cin>>n;
    double x[n+1],y[n+1];
    h=(b-a)/n;                        //get the width of the subintervals
    for (i=0;i<n+1;i++)
    {                        //loop to evaluate x0,...xn and y0,...yn
        x[i]=a+i*h;                //and store them in arrays
        y[i]=f(x[i]);
    }
    for (i=1;i<n;i+=2)
    {
        sum=sum+4.0*y[i];                //loop to evaluate 4*(y1+y3+y5+...+yn-1)
    }
    for (i=2;i<n-1;i+=2)
    {
        sum=sum+2.0*y[i];                /*loop to evaluate 4*(y1+y3+y5+...+yn-1)+
                                        2*(y2+y4+y6+...+yn-2)*/
    }
    integral=h/3.0*(y[0]+y[n]+sum);    //h/3*[y0+yn+4*(y1+y3+y5+...+yn-1)+2*(y2+y4+y6+...+yn-2)]
    cout<<"\nThe definite integral  is "<<integral<<"\n"<<endl;
    return 0;
}



======================
輸出畫面

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


Enter the limits of integration,

Initial limit,a= 0

Final limit, b=6

Enter the no. of subintervals(IT SHOULD BE EVEN),
n=10

The definite integral  is 1.4002




沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...