596
COMPUTER BASED NUMERICAL AND STATISTICAL TECHNIQUES
13.26 ALGORITHM FOR LAGRANGE’S INTERPOLATION METHOD
Step 1. Start of the program to interpolate the given data
Step 2. Input the number of terms n
Step 3. Input the array ax and ay
Step 4. For i = 0; i < n; i++
Step 5. nr = 1
Step 6. dr = 1
Step 7. for j = 0; j < n; j++
Step 8. if j !=1
(a) nr = nr * (x – ax[j])
(b) dr * (ax [i] – ax [j])
Step 9. End loop j
Step 10. y + =( nr / dr)*ay[i]
Step 11. end loop i
Step 12. print output x, y
Step 13. End of the program.
13.27 PROGRAMMING FOR LAGRANGE’S INTERPOLATION METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>
void main()
{
int n;
int i, j;
float ax[100];
float ay[100];
float h;
{
nr = nr*(x–ax[j]);
dr = dr*(ax[i]–ax[j]);
}
y = y+(nr/dr)*ay[i];
}
printf(“when x = %5.2f, y = %6.8f”, x, y);
printf(“press enter to exit”);
getch();
}
OUTPUT
Enter the no. of term –5
Enter the value in form of x–
Enter the value of x1 – 5
Enter the value of x2 – 7
Enter the value of x3 – 11
Enter the value of x4 – 13
Enter the value of x5 – 17
Enter the value in the form of y–
Enter the value of y1 – 150
598
COMPUTER BASED NUMERICAL AND STATISTICAL TECHNIQUES
Enter the value of y2 – 392
Enter the value of y3 – 1452
Enter the value of y4 – 2366
Enter the value of y5 – 5202
Enter the value of x for
Which you want the value of y – 9.0
When x = 9.0, y = 810.00
Press enter to exit.
printf(“\n enter the interval = ”);
scanf(“%f”, &h);
c = 0, y = 0;
for (i = 0; y <= b; i++)
{
s1 = 0;
s2 = 0;
c = h;
f = 1/ (1+(y*y));
if (i == a :: y == b)
s1 = f/2;
else
s2 = f;
s3 = s3+_(s1 + s2);
y = y + c;
}
s = s3*h;
printf(“the exact value of the function = %f”, s);
getch();
}
OUTPUT
Enter the interval = 0.5
The exact value of the function = 1.103846.
13.30 ALGORITHM FOR SIMPSON’S 1/3 RULE
Step 1. Start of the program for numerical integration
Step 2. Input the upper and lower limits a and b
Step 3. Obtain the number of subinterval by h = (b–a)/n
Step 4. Input the number of subintervals
Step 5. sum = 0
Step 6. sum = func(a) + 4 * func(a + h) + func(b)
s4 = 0;
c = h;
f = 1/(1+(y*y));
if (i == a :: y == b)
s1 = f;
else
if ((i! = a :: i! = b) && (i%2 == 1))
s2 = 4*f;
else if((i! = a :: i! = b) && (i%2 == 0))
s4 = 2*f;
s3 = s3 + (s1 + s2 + s4);
y = y + c;
i++;
}
COMPUTER PROGRAMMING IN ‘C’ LANGUAGE
601
s = s3*(h/3);
printf(“the exact value of the function = %f”, s);
getch();
}
OUTPUT
Enter the interval = 1
The exact value of the function = 1.066667.
13.32 ALGORITHM FOR SIMPSON’S 3/8 RULE
Step 1. Start of the program for numerical integration
Step 2. Input the upper and lower limits a and b
Step 3. Obtain the number of subinterval by h = (b–a)/n
Step 4. Input the number of subintervals
Step 5. sum = 0
Step 6. sum = func(a) + func(b)
h = (b–a)/n;
sum = 0;
res = 1;
sum = sim(a) + sim(b);
for (i = 1; i < n; i++)
{
if (i%3 == 0)
sum += 2*sim(a + i*h);
else.
sum += 3*sim(a + i*h);
}
res = sum*3*h/8;
printf(“\n value of the integral is: % .4f”, res);
getch();
}
float sim(float x)
{
float temp;
temp = 1/(1+(x*x));
return temp;
}
OUTPUT
Enter the Range
Lower limit a = 0
Upper limit b = 6
Enter the number of subintervals = 6
Value of the integral is: 1.3571
COMPUTER PROGRAMMING IN ‘C’ LANGUAGE
603