Trapezoidal Rule coding in C Language
Trapezoidal Rule in C Language
Here in this blog we will see how to evaluate the integration of function using the very easy and popular method i.e Trapezoidal Method.
Later in the subsection of this blog we will see a program that evaluate the integration of given function in C Language.
Approach:-
First of all let's us discuss the rule or approach of this method.
We approximate the function f(x) in the interval [xi ,xi+1] by a straight line joining the points (xi,yi) and (xi+1,yi+1).
Popularity:-
This method is very handy to calculate the integration of linear function.
Geometrically, the integral in an interval is approximated by the area of a trapezium.
Trapezoidal Rule |
Formula:-
a[f(x)dx]b = h/2*{y0+yn+2*(y1+y2+y3+y4+.......................+yn-1)}
where a=lower limit of function f(x)
b=upper limit of function f(x)
h(Step-Size)=(b-a)/n;
n=nodal value
C program to solve the integral of function f(x) using Trapezoidal rule.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
void main()
{
float x[100],fx[100],l,u,h,res=0;
int n,i;
clrscr();
printf("\n Enter the lower limit of function: ");
scanf("%f",&l);
printf("\n Enter the upper limit of function: ");
scanf("%f",&u);
printf("\n Enter the nodal value or sub-interval of function: ");
scanf("%d",&n);
h=(u-l)/n;
printf("Step-Size H= %.2f",h);
for(i=0;i<=n;i++)
{
x[i]=l+(i*h);
fx[i]=f(x[i]);
}
printf("\n X: ");
for(i=0;i<=n;i++)
{
printf("\n %.2f\t",x[i]);
}
printf("\n F(X:) ");
for(i=0;i<=n;i++)
{
printf("\n %.2f\t",fx[i]);
}
for(i=0;i<=n;i++)
{
if(i==0||i==n)
{
res=res+fx[i];
}
else
{
res=res+(2*fx[i]);
}
}
res=res*(h/2);
printf("\n THE REQUIRED VALUE OF INTEGRATION IS : %.2f",res);
getch();
}
float f(float x)
{
return (pow((1+x),-1)); //you can change the function //according to your need
}
:) :) :3
The above code is tested under CODE BLOCK IDE with all possible test cases.
Hope you like this blog. Don't hesitate to shower your love. Subscribe and follow the blog for more solution of the coding problems.
Thank You!!!!!!!!
cool
ReplyDelete