Tuesday 22 May 2012

C Program Of NA Using Trapezoidal Interpolation


/*Trapezoidal rule */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],y[MAX],sum=0,h,res;
clrscr();
printf("Solution using Trapezoidal rule \n");
printf("Enter Number of data points : ");
scanf("%d",&n);
printf("Enter value of table value set by set\n");
for(i=1;i<=n;i++)
{
printf("Enter value of x[%d] and y[%d] ",i,i);
scanf("%f%f",&x[i],&y[i]);
}
h=x[2]-x[1];
printf("\nInterval :=%f\n",h);
sum=y[1]+y[n];
for(i=2;i<n;i++)
sum=sum+y[i]*2;

res=sum*(h/2);
printf ("Integral from %3.2f to %3.2f is : = %f",x[1],x[n],res);
getch();
}
OUTPUT :-
Solution using Trapezoidal rule
Enter Number of data points : 6
Enter value of table value set by set
Enter value of x[1] and y[1] 7.47 1.93
Enter value of x[2] and y[2] 7.48 1.95
Enter value of x[3] and y[3] 7.49 1.98
Enter value of x[4] and y[4] 7.50 2.01
Enter value of x[5] and y[5] 7.51 2.03
Enter value of x[6] and y[6] 7.52 2.06

Interval :=0.010000
Integral from 7.47 to 7.52 is : = 0.099652

No comments:

Post a Comment