How would I find the definite integral (between any 2 limits, say a and b) of the absolute value of sin(x)?
I can calculate for the interval 0 to Pi, and from 0 to 2*Pi, but what if the user enters a value far outside 2*Pi? Could I create a while loop?
EDIT: Here is a look at my code and its purpose. I have left my analytical function blank for now because I am still working on it.
/*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));
}