Tải bản đầy đủ (.pdf) (8 trang)

A textbook of Computer Based Numerical and Statiscal Techniques part 62 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (56.59 KB, 8 trang )

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;


float p;
float nr, dr;
float x = 0, y = 0;
clrscr();
printf(“enter the no. of term–“);
scanf(“%d”, & n);
printf(“enter the value in the form of x–”);
for(i = 0; i < n; i++)
COMPUTER PROGRAMMING IN ‘C’ LANGUAGE
597
{
printf(“enter the value of x%d”, i+1);
scanf(“%f”, & ax[i]);
}
printf(“enter the value in the form of y”);
for(i = 0; i < n; i++)
{
printf(“enter the value of y%d “, i+1);
scanf(“%f”, & ay[i]);
}
printf(“enter the value of x for”);
printf(“which you want the value of y”);
scanf(“%f”, %x);
for(i = 0; i < n; i++)
{
nr = 1;
dr = 1;
for(j = 0; j < n; j++)
{
if(j != i)

{
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.

13.28 ALGORITHM FOR TRAPEZOIDAL 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)
Step 7. for i = 1; i < n; i++
Step 8. sum += 2 * func(a + i)
Step 9. End loop i
Step 10. Result = sum *h/2
Step 11. Print output
Step 12. End of the program and start of section func
Step 13. temp = 1/(1+(x * x))
Step 14. Return temp
Step 15. End of section func.
13.29 PROGRAMMING FOR TRAPEZOIDAL RULE
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float h, a, b, s1 = 0, s2 = 0, s3 = 0, s = 0, c, f, y;
int i;
clrscr ( );
printf(“Integrate the equation 1/(1+x^2) with limit 0 to 2”);
printf(“\n enter the initial and final limits =:”);
COMPUTER PROGRAMMING IN ‘C’ LANGUAGE
599
scanf(“%f %f”, &a, &b);

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)

Step 7. for i = 3; i < n; i += 2
Step 8. sum += 2 * func(a + (i–1) * h) + 4 * func(a + i * h)
Step 9. End loop i
Step 10. Result = sum * h/3
Step 11. Print output
Step 12. End of the program and start of section func
600
COMPUTER BASED NUMERICAL AND STATISTICAL TECHNIQUES
Step 13. temp = 1/ (1+(x * x))
Step 14. Return temp
Step 15. End of section func.
13.31 PROGRAMMING FOR SIMPSON’S 1/3 RULE
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float h, a, b, s1 = 0, s2 = 0, s3 = 0, s4 = 0, s = 0, c, f, y;
int i = 0;
clrscr ( );
printf(“Integrate the equation 1/(1+x^2) with limit 0 to 2”);
printf(“\n enter the initial and final limits =:”);
scanf(“%f %f”, &a, &b);
printf(“\n enter the interval = ”);
scanf(“%f”, &h);
c = 0, y = 0;
while (y <= b)
{
s1 = 0;
s2 = 0;

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)

Step 7. for i = 1; i < n; i++
Step 8. if i%3 = 0
Step 9. sum += 2 * func(a + i * h)
Step 10. else
Step 11. sum += 3 * func(a + (i) * h)
Step 12. End loop i
Step 13. Result = sum * 3 * h/8
Step 14. Print output
Step 15. End of the program and start of section func
Step 16. temp = 1/(1+(x * x))
Step 17. Return temp
Step 18. End of section func.
13.33 PROGRAMMING FOR SIMPSON’S 3/8 RULE
#include<stdio.h>
#include<conio.h>
float sim(float);
void main()
{
float res, a, b, h, sum;
int i, j, n;
602
COMPUTER BASED NUMERICAL AND STATISTICAL TECHNIQUES
clrscr ( );
printf(“Enter the Range\n”);
printf(“\n Enter the Lower limit a=”);
scanf(“%f”, &a);
printf(“\n Enter the Upper limit b=”);
scanf(“%f”, &b);
printf(“\n Enter the number of sub-intervals= ”);
scanf(“%d”, &n);

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


13.34
ALGORITHM FOR FITTING A STRAIGHT LINE OF THE FORM
Y = a + bX
Step 1. Start of the program for fitting straight line
Step 2. Input the number of terms observe
Step 3. Input the array ax and ay
Step 4. for i = 0 to observe
Step 5. sum1 + = x[i]
Step 6. sum2 + = y[i]
Step 7. xy[i] = x[i] * y[i]
Step 8. sum3 += xy[i]
Step 9. End of the loop i
Step 10. for i = 0 to observe
Step 11. x2[i] = x[i] * x[i]
Step 12. sum4 += x2[i]
Step 13. End of the loop i
Step 14. temp1 = (sum2 * sum4) – (sum3 * sum1)
Step 15. a = temp1/((observe * sum4) – (sum1 * sum1)
Step 16. b = (sum2 – observe * a)/sum1
Step 17. Print output a, b and line “a + bx”
Step 18. End of the program.
qqq

×