Your task is to write a function that compute a definite int

Your task is to write a function that compute a definite integral, namely, integral _a^b f(x) dx where f is an arbitrary continuous unary function. Naturally, you should provide a numerical solution using a simple method such as the trapezoidal method over a collection of boxes that sub-divide the integration interval [a, b]. Formally, given f, a, b and n, subdivide the range [a, b] into n sub-intervals (of width b-a/n) and compute the integral as the sum of the surfaces of the trapezoid embedded in each sub-interval. fun integrate f a b n =

Solution

Simply if you need a c related function you can go with the following code-

double definateintegral(double(*g)(double y), double a, double b, int m)
{
double width = (b - a ) / m;
double calculatearea = 0.0;
for (int j = 0; j < m; j ++) {
calculatedarea += f(a + (j + 0.5) * width) * width;
}
return area;
}

int main()
{
cout.precision(8);
cout << integral(sin, 0, M_PI / 4, 12);
return 0;
}

Basically if you considered genral algorithm you can use

Step a Set z = (b - a)/n.
Step b Set YJ 0 = f(a) + f(b);
YI 1 = 0; (Summation of f(y2j-1).)
YJ 2 = O. (Summation of f (Y2j)\')
Step c For j = 1, ... ,n - 1 do Steps d and e.
Step d Set Y = a + jz.
Step e when j is even then set YI2 = YI2 + f(Y)
else set YJl = YJl + f(Y).
Step f Set YJ = h(YJO + 2· YJ2 + 4· YJl)/3.
Step g OUTPUT (Yl);
STOP.

 Your task is to write a function that compute a definite integral, namely, integral _a^b f(x) dx where f is an arbitrary continuous unary function. Naturally,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site