在C++中,如何计算解析值∫b一种| 罪x |dX∫ab|sin⁡x|dx?

计算科学 C++ 正交
2021-11-25 22:02:44

我如何找到 sin(x) 绝对值的定积分(在任意 2 个限制之间,比如 a 和 b)?

我可以计算从 0 到 Pi 以及从 0 到 2*Pi 的区间,但是如果用户输入的值远远超出 2*Pi 怎么办?我可以创建一个while循环吗?

编辑:这是我的代码及其用途。我暂时将我的分析功能留空,因为我仍在研究它。

/*This program computes the area between the sine curve and the x-axis
It estimates the area using the Trapezoidal method and Simpson's 1/3 rule
and compares all three values */

#include <iostream> //preprocessor directive needed in order to use std::cout
                    //and std::cin
#include <iomanip> //preprocessor directive needed in order to use
                   //a manipulator which uses an argument
#include <cmath>   //preprocessor directive needed in order to use trig, exp and
                   //log functions

using namespace std; //avoids having to uses std:: with cout and cin

//declare the variables
double a, b;
int n;

//prototypes for f(), analytical(), and integrateTrap()
double f (double x);
double analytical (double a, double b);
double integrateTrap (double a, double b, int n);
double integrateSimpson (double a, double b, int n);

int main (int argc, char* argv[])
{  
    //tell the user what the program does
    cout << "The program estimates the integral of sin(x) between\n the limits"
    "[a,b] using both the Trapezoidal method and Simpson's 1/3 rule" << endl;

    cout << "\n\nEnter the lower limit a as a floating point number: ";
    cin >> a;
    cout << "\n\nEnter the upper limit b as a floating point number: ";
    cin >> b;
    cout << "\n\nEnter the number of intervals (an integer>0): ";
    cin >> n;

    //call integrateTrap()
    cout << "\n\nThe estimate of the interval between the limits [" << a << ", "
    << b << "] using \nthe Trapezoidal rule = " << integrateTrap(a,b,n) << endl;

    //call integrateSimpson()
    cout << "\n\nThe estimate of the interval between the limits [" << a << ", "
    <<b<< "] using\nSimpson's 1/3 rule = " << integrateSimpson (a,b,n) << endl;

    //call analytical()
    cout << "\n\nThe analytical answer = " << analytical(a,b) << endl;

    //allow the user to see the results before ending the program
    cout << "\nPress Enter to end the program";
    cin.get();
    cin.get();
}

///////////////////////////////////////////////////////////////////////////////

//define sin(x)
double f (double x)
{
       return abs(sin(x));
}

////////////////////////////////////////////////////////////////////////////////

//define analytical() to be the answer to the definite integral
double analytical(double a, double b)
{

}

////////////////////////////////////////////////////////////////////////////////

//define integrateTrap()
double integrateTrap (double a, double b, int n)
{
       //declare and initialise the variables
       double h = (b-a)/n;
       double x = a;
       double sum = 0.0; 

       //sum up the area of each interval
       for (int i = 1; i <= n; i++) 
       {
           x = a + i*h;
           sum += f(x);    
       }
       return (h/2)*(f(a)+ 2*sum + f(b));     
}

////////////////////////////////////////////////////////////////////////////////

//define integrateSimpson()
double integrateSimpson (double a, double b, int n)
{
       //declare and initialise the variables
       double h = (b-a)/n;
       double even = 0.0, odd=0, x;
       int i;

       //sum up the area of each interval
       for (i = 0, x=a+h; i<n; x=x+h, i++) 
       {
            if(i % 2 == 0)
            {
                 even += f(x);
            }
            else
            {
                 odd += f(x);
            }

       }  
       return (h/3)*(f(a)+ 2*even + 4*odd + f(b));
}
3个回答

您可以直接求积分的最终答案:

ab|sin(x)|dx=2(bπaπ)+cos({aπ}π)cos({bπ}π)

其中表示地板运算,而表示提取数字小数部分的小数部分运算。c{c}

编辑:这是我的分析功能的更新版本。测试了几个值,它似乎工作。感谢安科的帮助

double analytical(double a, double b)
{
  const double PI = 2*acos(0);

  if ( b-a <=PI)
  {
       return abs(-cos(b) + cos(a));
  }
  else if ((b-a) > PI)
  {
       float area=0;
       double aReduced = fmod(a,PI);
       double bReduced = fmod(b,PI);

       while (b-PI > a)
       {
            b = b - PI;
            area += 2;
       }

       area += abs(-cos(PI)+cos(a)) + abs(-cos(b)+cos(PI));
       return area;
  }
}

看起来您正在寻找函数fmodremainder,或者remquo如果您愿意接受不可移植的最大间隔数。