Write a program to calculate an approximate integral of the
Write a program to calculate an approximate integral of the function f(x) = 0.1x^3 + 1 over the interval [a, b] using the trapezoidal method with n subintervals, where a. 6, and n are taken from standard input. In lecture 5, you learned how to calculate an approximate integral using the rectangle rule. In this problem, you will use the trapezoidal rule to approximate the integral (see Figure [1]). At the start of the program, prompt the user to input the interval bounds and the number of subintervals by printing \"Integrate over [A, B] with N subintervals. Input A B N:\ \". If B
Solution
#include<stdio.h>
float f(float x)
{
return ((0.1)*(x*x*x)+1);
}
void main()
{
float a,b,h,s;
int i,n;
printf(\"\ Enter values of a,b,n: \");
scanf(\"%f %f %d\",&a,&b,&n);
if(b<a)
printf(\" a must be less than or equal to b\");
else if(n<0)
printf(\"\ n must be a non negative\");
else
{
h=(b-a)/n;
s=f(a)+f(b);
for(i=1;i<=n-1;i++)
s+=2*f(a+i*h);
printf(\"value of integral is %3f\ \",(h/2)*s);
}
}
![Write a program to calculate an approximate integral of the function f(x) = 0.1x^3 + 1 over the interval [a, b] using the trapezoidal method with n subinterval Write a program to calculate an approximate integral of the function f(x) = 0.1x^3 + 1 over the interval [a, b] using the trapezoidal method with n subinterval](/WebImages/35/write-a-program-to-calculate-an-approximate-integral-of-the-1104569-1761584329-0.webp)