/*Write a C program to implement Trapezoidal method*/
#include<stdio.h>
#include<math.h>
float f(float);
float a; float b; float x; float h; float sum; int n; int i;
int main()
{
clrscr();
printf("Enter value for A:");
scanf("%f",&a);
printf("Enter value for B:");
scanf("%f",&b);
printf("Enter number of rectangles:");
scanf("%d",&n);
h=(b-a)/n;
sum=(0.5*h)*(f(a)+f(b));
printf("%f \n",sum);
for(i=1;i<n;i++)
{
sum=sum+h*f(a+(i*h));
printf("%f \n",sum);
}
printf("The value of the integral is:%f\n",sum);
getch();
}
float f(float x)
{
float value;
value=x*x+3;
return value;
}
#include<stdio.h>
#include<math.h>
float f(float);
float a; float b; float x; float h; float sum; int n; int i;
int main()
{
clrscr();
printf("Enter value for A:");
scanf("%f",&a);
printf("Enter value for B:");
scanf("%f",&b);
printf("Enter number of rectangles:");
scanf("%d",&n);
h=(b-a)/n;
sum=(0.5*h)*(f(a)+f(b));
printf("%f \n",sum);
for(i=1;i<n;i++)
{
sum=sum+h*f(a+(i*h));
printf("%f \n",sum);
}
printf("The value of the integral is:%f\n",sum);
getch();
}
float f(float x)
{
float value;
value=x*x+3;
return value;
}
Output:
Enter value for A:25
Enter value for B:30
Enter number of rectangles:12
318.958313
589.377869
868.695007
1157.054321
1454.600586
1761.478516
2077.832764
2403.807861
2739.548584
3085.199707
3440.905762
3806.811523
The value of the integral is:3806.811523
Enter value for B:30
Enter number of rectangles:12
318.958313
589.377869
868.695007
1157.054321
1454.600586
1761.478516
2077.832764
2403.807861
2739.548584
3085.199707
3440.905762
3806.811523
The value of the integral is:3806.811523
0 comments:
Post a Comment