63 125
Assignments Done
99%
Successfully Done
In July 2018

Answer to Question #41625 in C++ for Ali

Question #41625
write a program to approximate the integral of a function of one variable. Your program will use the trapezoid rule.

Test your program using the following functions:

Integral of 1/x from 1 to 2

Integral of x2sin(x) from -1 to 1

Integral of x2e2x from 0 to 3
Expert's answer
#include <stdio.h>    
#include <conio.h>  
#include <math.h>
#define f(x) (1/x) //use macros to define the integrand
//#define f(x) ((x)*(x)*sin(x))  
//#define f(x) ((x)*(x)*Exp(2*(x)))  //uncoment to change integrand
double calcAreaTrap(double a1, double b1){   //function calculates the area of trapezoid of height b1-a1 , and the center line f((a1+b1)/2).
return (b1-a1)*(f(a1)+f(b1))/2;
} // or this function return the approximation value between two end points a1 and b1 , based on the trapezoid method
double calcArea(double A, double B,int N)  // this function will return approximation value between two end points,a and b, based on the method specified by flag
{
double buf = 0; // Declaration and initialization  of the auxiliary variable tha will be contain approximation value between two points , a and b
double h = (B-A)/N;  //Declaration and initialization  of the auxiliary variable tha will be contain value of space step , h = b1-a1
//this part will calculate approximation value between two end points
for (int i =0;i<N;i++) {
buf=buf+calcAreaTrap(A+i*h,A+i*h+h);
}

return buf;
}
void OutPut(double Area){ //this function type on the screen area's value
printf("The integral's value is : %f", Area);
}
int main(int argc, char *argv[])
{
OutPut(calcArea(1,3,1000));// by using all define function program will calculate and output approximated value of the integral of the function f(x) tha define in head by macros in interval (0;3) with space step 0.03
getch();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions