Ex:
Formula:
 
Formula:
               Here x0=0 and x0+nh=6  hence h= (6-0)/4 =1.5
 therefor interval is 1.5 in each term 
               lower bound                                                upper bound
x 
 | 
  
       0 
 | 
  
    1.5 
 | 
  
    3 
 | 
  
   4.5 
 | 
  
    6 
 | 
 
f(x) 
 | 
  
1 
 | 
  
    .30 
 | 
  
  .10 
 | 
  
  .04 
 | 
  
  .02 
 | 
 
Now apply the trapezoidal formula :
 = 1.5/2[(1+.02) +2(.30+.10+.04)]
   = 1.5/2[1.027+2(.44)]
   =1.5/2[1.027+.88)]
   =1.5/2[1.907]   =1.4
C Code:
#include<stdio.h>
float f(float);
void main()
{ float a,b,h,i,n,k=0;
   printf("\nEnter the lower and upper limits for function");
   scanf("%f%f",&a,&b);
   printf("\nEnter the difference");
   scanf("%f",&h);
//Calculate the integral
printf("\nx\tf(x)");
for(i=a;i<=b;i=i+h)
{ printf("\n%f\t%f",i,f(i));
k=k+f(i);
}
k=k-(f(a)+f(b));
k=(h/2.0)*(f(a)+f(b))+h*k;
printf("\nThe definite integral=%f",k);
}
float f(float x)
{ return(1/(1+x*x));
}
Output:
Enter the lower and upper limits for function
0                                                                 
6                                                                                                                  
Enter the difference 1.5                                                                                                                        
x                f(x)                                                                                                             
0.000000        1.000000                                                                                                         
1.500000        0.307692                                                                                                         
3.000000        0.100000                                                                                                         
4.500000        0.047059                                                                                                         
6.000000        0.027027                                                                                                         
The definite integral= 1.452397
No comments:
Post a Comment