Trapezoidal Rule In C/C++



#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
float f(float);
void main()
{float a,b,h,sum,x,trap;
int n,i,m;
cout<<"Enter the limits a & b & number of subintervals n\n";
cin>>a>>b>>n;
h=(b-a)/n;
sum=0.0;
m=n-1;
for(i=1;i<=m;i++)
{x=a+i*h;
sum=sum+f(x);
}
trap=h*(f(a)+2.0*sum+f(b))/2.0;
cout<<"Value of integral with "<<n<<endl;
cout<<"Subintervals="<<trap;
getche();
}
float f(float x)
{float fun;
fun=1.0/(1+x);
return(fun);
}

posted under |

0 comments:

Post a Comment

Newer Post Older Post Home

About

Cool na? :) Now Learn How to create one by Clicking here


Followers

About This Blog