Hello, I need this program to work in Visual C++, Visual Studios 2015 please, Cr
ID: 3839387 • Letter: H
Question
Hello, I need this program to work in Visual C++, Visual Studios 2015 please,
Create a program to solve the integral of a function using Simpsons rule. The upper and lower limits will be positive and the value of n must be even. The user will be requested to select witch function to solve from the following choices:
1)You are given the following three mathematical functions:
a) 1/(1+x2)
b) ex^2
1)The “main” function request the user to select one of the mathematical functions above that they would like to solve. The “main” function will also request the upper and lower limits along with the value of n.
2) You must have a separate function that calculates (delta x)/3.
3) You must have a separate function that calculates the series in Simpsons rule
(f0 + 4 f1 ……….+4fn-1 +fn)
4) You will need a function that completes the overall calculation and prints out the solution. It should also print which function was solved, the upper and lower limits along with the value of n . Provided that n is large enough the answer to the integral of the function should be accurate to three decimal places.
This needs to be in C++ format,
Thank you
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
double deltaX3(double l, double u)
{
//To calculate (delta x) / 3
return ((u - l) / 3);
}
double fx(int choice, double xi)
{
//To return f(x) according to choice
if(choice == 1)
{
return (1 / (1 + (xi * xi)));
}
else if(choice == 2)
{
return (2.71828 * (xi * xi));
}
}
void integral(int choice, int n, double l, double u)
{
//To calculate the integral
int i;
double xi = l, dx3, h, f0, fn, fodd = 0.0, feven = 0.0, sum;
h = (u - l) / n; //Interval width
dx3 = deltaX3(l, u);
f0 = fx(choice, l); //f(x0)
fn = fx(choice, u); //f(xn)
//Calculating summation parts
for(i = 1; i <= (n - 1); i++)
{
xi += h;
if(i % 2 == 0)
feven += fx(choice, xi); //Adding up the f(x) for even terms
else
fodd += fx(choice, xi); //Adding up the f(x) for odd terms
}
sum = (dx3 / n) * (f0 + (4 * fodd) + (2 * feven) + fn); //Calculating integral using simpsons rule formula
//Displays output according to choice
if(choice == 1)
cout<<setprecision(3)<<" The solution to the function 1/(1 + x^2): "<<sum;
else
cout<<setprecision(3)<<" The solution to the function ex^2: "<<sum;
cout<<" Lower limit: "<<l<<" Upper limit: "<<u;
cout<<" value of n: "<<n<<endl;
return;
}
int main()
{
while(true)
{
int ch, n;
double l, u;
//Menu
cout<<" 1. 1/(1 + x^2)";
cout<<" 2. ex^2";
cout<<" 3. Exit";
cout<<" Enter choice: ";
cin>>ch;
if(ch < 1 || ch > 3)
cout<<" Invalid choice. Try again. ";
else if(ch == 3)
{
cout<<" Exiting... ";
return 0;
}
else
{
cout<<" Enter lower limit: ";
cin>>l;
cout<<" Enter upper limit: ";
cin>>u;
cout<<" Enter the value of n: ";
cin>>n;
integral(ch, n, l, u);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.