Write a program to calculate an approximate integral of the function f(x) = 0.1x
ID: 3797128 • Letter: W
Question
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 BExplanation / Answer
#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);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.