Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simpson?s numerical integration routine. This function returns an // approximati

ID: 643112 • Letter: S

Question

Simpson?s numerical integration routine. This function returns an // approximation of the integral of f over the interval when using n subintervals in the approximation process. II This function has to be used as it is without modifying. double Simpson(double a, double b, int n) Write a C++ program that has the user to input a positive value x (of type double), and then approximates C(x) by using Simpson?s method. For each integral you compute, first approximate it by using Simpson?s method (written above) with 10 intervals, then with 20 intervals, and so on, each time increasing the number of intervals by 10 until the last estimate uses 50 intervals. For each approximation, the program should print out the number of intervals and the result (look at the example below). The program should consist of three functions: main, and the function f(t)=cos (pi t^2/2) This process should be in a continuation loop, as shown in the following example: Input X: 3.14159265

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

const double pi = std::acos(-1);

double f(double x){
   return cos((pi * x * x) / 2);
}

double simpson(double a, double b, int n){
   double sum = 0.0, delta_x = (b-a)/n;
   int i;
   sum = f(a) + f(b);
   for(i = 1; i < n; i++){
       if(i % 2 == 1)
           sum = sum + 4.0 * f(a + i * delta_x);
       else
           sum = sum + 2.0 * f(a + i * delta_x);
   }
   return delta_x * sum / 3.0;
}

int main(){
   double x;
   char option;
   while(true){
       cout << "Input x: ";
       cin >> x;
       cout << "Intervals C(x): result:" << endl;
       for(int i = 10; i <= 50; i += 10){
           cout << i << " " << setprecision(8) << simpson(0, x, i) << endl;
       }
       cout << "Input another x value (y/n)?";
       cin >> option;
       if(option != 'y' && option != 'Y'){
           break;
       }
   }
   return 0;
}