曲线下该区域的C 程序.(上限)

C ++ program for the area under a curve. (the upper limit)?

本文关键字:程序 上限 区域 曲线      更新时间:2023-10-16

这是我对上限的所拥有的。我对n的价值是5。但是我想我在某个地方搞砸了,因为当我手工计算出它的价值不是相同的值时。有人知道我的程序怎么了?

#include<iostream> 
#include<stdlib.h> 
#include<math.h> 
#include<iomanip> 
#include<fstream> 
using namespace std; 
double f(double x) 
{ 
    return (0.6*pow(x,2)) +4; 
} 
int main ( ) 
{ 
    double a, b, delx, x, area; 
    int n; 
    cout << "Welcome. To begin, please enter a value for 'a' followed by a value for 'b'. n"; 
    cin>>a>>b; 
    cout << "You have entered a = " << a<<" & b = " <<b <<". Now, input an n value n"; 
    cin >>n; 
    delx = (b-a)/n; 
    area = 0; 
    for (int i=0;i<=n;i++) 
    { 
        area = area + f(a+i*delx)*delx; 
    } 
    cout << "The area under the curve of f(x) = 0.6x^2 + 4 is "; 
    cout << setprecision(5) << area; 

    system ("pause"); 
}
int main()
{
    double a=0, b=5; //a: lower limit, b: upper limit of integration
    int n=20; //the number of intervals
    double dx = (b-a)/n; //step size
    double area = 0; //our goal to find
    for (int i=0;i<n;i++) 
    { 
        //using trapezoidal method
        //the area of a trapezoid is (lower base + upper base)*height/2
        //f(a+i*dx): upper base, f(a+(i+1)*dx): lower base
        //dx: height of the trapezoid
        area = area + (f(a+i*dx)+f(a+(i+1)*dx))*dx/2.0; 
    } 
    std::cout<<area;
}

尽管您的方法不是不正确(但不是理想的)梯形方法会更快地收敛。在您的方法中,您可以将N(间隔数)设置为比梯形更高的数字,以获得相似的准确度。