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

C++ Programming I need the result with input/output please to match it with matl

ID: 3819154 • Letter: C

Question

C++ Programming I need the result with input/output please to match it with matlab! C-code to integrate (-x2) Use N of diff values, N number of steps/intervals. Print out all xi, fi f(i) and the result. Determine and print the appropriate value of N based on the results for different N values. Integrate the function by both Simpson and Trapezoidal rule, then compare the results with it calculated from mathematical software such as Matlab! C-code must contain: 1) use of arrays 2) use of pointers 3) use of structure 4) use of union 5) use of functions and function calls 6) formatted output on screen 7) saving of the same output on a data file

Explanation / Answer

//Simpson’s method

#include<iostream>
#include<cmath>
using namespace std;
double f(double x)
{
//this is the function whose definite integral is to be calcuated here
   // here took the value of e=2.71828
double a= pow(2.71828,-(x*x));   
return a;
}

int main()
{   
  
cout.precision(4); //this is to set the precision
cout.setf(ios::fixed);
int n,i; //here we declare n for subintervals and i is just looping
double a,b,c,h,sum=0,integral;
  
cout<<" Enter the limits of integration, Initial limit,a= "; // take the limits
cin>>a;
cout<<" Final limit, b="; //get the limits
cin>>b;
  
cout<<" Enter the no. of subintervals(n), n=";   
cin>>n;
  
double x[n+1],y[n+1];
  
   h=(b-a)/n; //here we take the width of the subintervals
for (i=0;i<n+1;i++)
{ //this loop is to calculate the values of x0...xn and y0,...yn
x[i]=a+i*h; //and store them in arrays
y[i]=f(x[i]);
}
for (i=1;i<n;i+=2)
{
   // based on rule this loop will calculate 4*(y1+y3+y5+...+yn-1)
sum=sum+4.0*y[i];   
}
for (i=2;i<n-1;i+=2)
{
   /*this loop is to calculate 4*(y1+y3+y5+...+yn-1)+2*(y2+y4+y6+...+yn-2)*/
sum=sum+2.0*y[i];
   }
integral=h/3.0*(y[0]+y[n]+sum);
cout<<" Definite integral of your function is"<<integral<<" "<<endl;
return 0;
}

#include<iostream>
#include<cmath>
using namespace std;
double f(double x)   
{
//this is the function whose definite integral is to be calcuated here
   // here took the value of e=2.71828
double a= pow(2.71828,-(x*x));   
return a;
}
int main()
{
int n,i; //here we declare n for subintervals and i is just looping
double a,b,h,sum=0,integral;
cout<<"Enter the limits, Initial limit,a="; // take the limits
cin>>a;
cout<<"Final limit, b=";
cin>>b;
cout<<"Enter the no. of subintervals(n)="; // take the value of n
cin>>n;
double x[n+1],y[n+1];
h=(b-a)/n; //here we take the width of the subintervals
for (i=0;i<=n;i++)
{ //this loop is to calculate the values of x0...xn and y0,...yn
x[i]=a+i*h;   
y[i]=f(x[i]);
}
for (i=1;i<n;i++)
{ // this loop will calculate h*(y1+...+yn-1)
sum=sum+h*y[i];
}
integral=h/2.0*(y[0]+y[n])+sum;
cout<<"Definite integral of your function is "<<integral<<endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote