c++ problem please use the folowing cpp file to solve this problem: 1. Simpson’s
ID: 3577836 • Letter: C
Question
c++ problem
please use the folowing cpp file to solve this problem:
1. Simpson’s Composite (Numerical Integration)
To find the approximate area under a curve given by a function f(x). The area
is computed relative to the x axis and it is negative when the function is
below 0.
This is represented by
I = ? f(xi) ?xi from x1 = a to x2m-1 = b
The algorithm uses the Simpson's composite rule, which approximate the area
between x0 and x2 as
?x*(f(x0) + 4*f(x1) + f(x2))/3
INPUT endpoints a,b; positive integer m
OUTPUT s=approximation xI to I
*/
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
double simpsonRule(double, double, int);
int main()
{
// Read input parameters
cout<<"Enter the integration lower and upper boundaries ";
double a, b;
cin >> a >> b;
if(a>b){
cout<<"Error: lower limit must be smaller than upper one ";
return 1;
}
cout<<"Enter the integration steps (>1) ";
int n;
cin >> n;
if(n < 2) {
cout<<"Error: integration steps must be larger than 1 ";
return 1;
}
// Call simpsonRule function
double xI = simpsonRule(a, b, n/2); // note: m = half the integration steps
// Write output
cout << "The integration between "<<a<< " and "<<b<<" results in "<<endl;
cout << xI << endl;
return 0;
}
// Note: The funtion is set to zero if it is undefined
double f(double x)
{
// original
double xx = x*x;
return (xx<1)? sqrt(1.-xx): 0;
}
//
// SIMPSON RULE
//
double simpsonRule(double a, double b, int m)
{
// Step 1 Set h = (b - a)/(2m)
double h = (b-a)/(2.*m);
// Step 2 Set xI0 = f(a) + f(b)
// xI1 = 0 (summation of f(x2i-1) )
// xI2 = 0 (summation of f(x2i) )
double xI0 = f(a) + f(b);
double xI1 = 0;
double xI2 = 0;
//Step 3 For i = 1, …, 2*m-1 do Steps 4 and 5
for(int i=1; i<2*m; ++i){
// Step 4 Set x = a + i*h
double x = a + i*h;
// Step 5 If i is even then set xI2 = xI2 + f(x)
// else set xI1 = xI1 + f(x)
if (i%2 == 0)
xI2 += f(x);
else
xI1 += f(x);
}
// Step 6 Set xI = h*(xI0 + 2*xI2 + 4*xI1)/3.
double xI = h*(xI0 + 2*xI2 + 4*xI1)/3.;
// Step 7 OUTPUT (xI)
return xI;
}
thank you :)
An engineering problem involves the determination of the length of a flat sheet of metal required to build a sheet of corrugated roofing. The cross-section of the roofing is shaped into the form of a sine wave by a machine. A f(x) Af As (Ar)2 (Af)2 Ar f(z) sin z As Af 1+ (cos z) Az Supposed a corrugated sheet of length three feet is needed, the height of each wave is one inch from the center line, and each wave has a period of approximately 2T inches. The problem of finding the length of the initial flat sheet is one of finding the arc length Lof the curve given in this case by f(x) sin x from x 0 inches to 36 inches. The arc length can be approximated as the sum of the length of many small straight segments 4s. The length of each segment is calculated using the Pythagoras theorem. For f(x)- sin x, it can be shown that the slope of As is AflAx cos x. The total arc length is 36 1 cos z) Ar which converges to the actual arc length as Ax get smaller and smaller. Problem 1.1 Compute the arc length L by integrating the square root term in the summation between 0 and 36 inches. Use enough integration steps to obtain a result accurate to two decimal places expressed in inches.Explanation / Answer
double simpsonRule(double a, double b, int m)
{
// Step 1 Set h = (b - a)/(2m)
double h = (b-a)/(2.*m);
// Step 2 Set xI0 = f(a) + f(b)
// xI1 = 0 (summation of f(x2i-1) )
// xI2 = 0 (summation of f(x2i) )
double xI0 = f(a) + f(b);
double xI1 = 0;
double xI2 = 0;
//Step 3 For i = 1, …, 2*m-1 do Steps 4 and 5
for(int i=1; i<2*m; ++i){
// Step 4 Set x = a + i*h
double x = a + i*h;
// Step 5 If i is even then set xI2 = xI2 + f(x)
// else set xI1 = xI1 + f(x)
if (i%2 == 0)
xI2 += f(x);
else
xI1 += f(x);
}
// Step 6 Set xI = h*(xI0 + 2*xI2 + 4*xI1)/3.
double xI = h*(xI0 + 2*xI2 + 4*xI1)/3.;
// Step 7 OUTPUT (xI)
return xI;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.