C Program to implement Trapezoidal Rule



Sumit Kar, Numerical Methods, C, Timus Rak



/*
Trapezoidal Rule
Equation: x/(1+x) dx
lower limit=0 upper limit=1 number of interval=6
*/
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,h,x,y,s=0;
float x0,y0,xn,yn,r;
int i,n;
float f(float);
clrscr();
printf("\nEnter lower limit value:: ");
scanf("%f",&a);
printf("\nEnter upper limit value:: ");
scanf("%f",&b);
printf("\nEnter the number of intervals:: ");
scanf("%d",&n);
h=(b-a)/n;
x0=a;
y0=f(a);
yn=f(b);
x=x0+h;
for(i=1;i<=n-1;i++)
{
y=f(x);
s=s+y;
x=x+h;
}
r=(h/2)*(y0+yn+2*s);
printf("\nResult is:: %f",r);
getch();
}
float f(float x)
{
return x/(1+x);
}


Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C