Answer to Question #311013 in C++ for Bilal

Question #311013

Write a program to calculate simple integral?

1
Expert's answer
2022-03-13T19:07:14-0400

Here is program:

double f(double x)
{
	return sin(x);
}
int main()
{
		int i; 
		double Integral;
		double a = 0.0, b = 1.0; 
		double h = 0.1;
		double n; 

		n = (b - a) / h;
	
		Integral = 0.0;
		for (i = 1; i <= n; i++)
			Integral = Integral + h * f(a + h * (i - 0.5));
		cout << "I1 = " << Integral << "\n";
	
		Integral = h * (f(a) + f(b)) / 2.0;
		for (i = 1; i <= n - 1; i++)
			Integral = Integral + h * f(a + h * i);
		cout << "I2 = " << Integral << "\n";

		Integral = h * (f(a) + f(b)) / 6.0;
		for (i = 1; i <= n; i++)
			Integral = Integral + 4.0 / 6.0 * h * f(a + h * (i - 0.5));
		for (i = 1; i <= n - 1; i++)
			Integral = Integral + 2.0 / 6.0 * h * f(a + h * i);
		cout << "I3 = " << Integral << "\n";
}

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 the first!

Leave a comment