1. S.C.D. Govt. College
Practical File
Computer Oriented
Numerical Methods
Submitted to:
Prof. Neha Bhatia
Signature
Submitted by:
Jasbir Singh
BCA-3rd
sem
7114
Subject Code: BCA-16-304
SESSION 2018-19
2. I have taken efforts in this practical file. I am highly indebted
to the Numerical Methods Lab teacher Prof. Neha Bhatia for
her guidance and constant supervision as well as for providing
necessary information regarding the programs and also for her
support in completing the practical file.
I would like to express my gratitude towards my parents for
their kind co-operation and encouragement which helped me
in the completion of this practical file.
My thanks and appreciations also go to my classmates in
developing the practical file and to the people who have
willingly helped me out with their abilities.
Place: Ludhiana Name: Jasbir Singh
Date: 29/09/2018 Signature
Acknowledgment
3. Table of Contents
1. Program to solve non-linear equation using the bisection method............................2
2. Program to solve non-linear equation using the false position method.....................6
3. Program to solve non-linear equation using Newton-Raphson method..................10
4. Program to solve non-linear equation using Secant method. ..................................12
5. Program to solve system of linear equation using Gauss Elimination Method.......14
6. Program to solve system of linear equation using Gauss Jordan Method...............16
7. Program to solve system of linear equation using Gauss Seidel Method................18
8. Program to generate Newton Forward Differences Table.......................................22
9. Program to generate Newton Backward Differences Table. ...................................26
10. Program to generate Newton Divided Difference Table.........................................30
11. Program to implement Newton Forward Difference Interpolation Polynomial. ....34
12. Program to implement Newton Backward Difference Interpolation Polynomial...36
13. Program to implement Newton Divided Differences Interpolation Polynomial. ...40
14. Program to implement Trapezoidal Rule for a known function. ............................42
15. Program to implement Simpsons 1/3 Rule for a known function...........................44
16. Program to implement Simpsons 3/8 Rule for a known function...........................46
4. 1
/* Output 1
Enter two initial approximations x1, x2: 1 2
Enter a very small number epsilon: .0001
f(1.00) = -4.000000, f(2.00) = 2.000000
Next approximation after 1 iteration is 1.500000, f(x) = -2.125000
Next approximation after 2 iteration is 1.750000, f(x) = -0.390625
Next approximation after 3 iteration is 1.875000, f(x) = 0.716797
Next approximation after 4 iteration is 1.812500, f(x) = 0.141846
Next approximation after 5 iteration is 1.781250, f(x) = -0.129608
Next approximation after 6 iteration is 1.796875, f(x) = 0.004803
Next approximation after 7 iteration is 1.789062, f(x) = -0.062730
Next approximation after 8 iteration is 1.792969, f(x) = -0.029046
Next approximation after 9 iteration is 1.794922, f(x) = -0.012142
Next approximation after 10 iteration is 1.795898, f(x) = -0.003675
Next approximation after 11 iteration is 1.796387, f(x) = 0.000563
Next approximation after 12 iteration is 1.796143, f(x) = -0.001556
Next approximation after 13 iteration is 1.796265, f(x) = -0.000497
Next approximation after 14 iteration is 1.796326, f(x) = 0.000033
Root = 1.7963
*/
5. 2
1. Program to solve non-linear equation using the bisection method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
void main(){
int i = 0;
float x1, x2, x3, f1, f2, f3, epsilon;
clrscr();
printf("nEnter two initial approximations x1, x2: ");
scanf("%f %f", &x1, &x2);
printf("nEnter a very small number epsilon: ");
scanf("%f", &epsilon);
f1 = f(x1);
f2 = f(x2);
printf("nf(%.2f) = %9f, f(%.2f) = %9f", x1, f1, x2, f2);
if(f1 * f2 > 0){
printf("nInitial approximations x1 = %f, x2 = %f are not proper.",
x1, x2);
exit(0);
}
do{
x3 = (x1 + x2)/ 2;
i++;
f3 = f(x3);
printf("nNext approximation after %2d iteration is %f, f(x) =
%9f", i, x3, f3);
if(f1 * f3 < 0){
x2 = x3;
f2 = f3;
}
else{
x1 = x3;
f1 = f3;
}
}while((fabs(f3) > epsilon) && (f3 != 0));
/* Using relative error
while((fabs((x2 - x1)/ x2) > epsilon) && (f3 != 0));
*/
printf("nRoot = %.4f", x3);
getch();
}
float f(float x){
8. 5
/* Output 2
Enter two initial approximations x1, x2: 1 2
Enter two very small number epsilon and delta: .0001 .001
f(1.00) = -4.000000, f(2.00) = 2.000000
Next approximation after 1 iteration is 1.666667, f(x) = -1.037037
Next approximation after 2 iteration is 1.780488, f(x) = -0.136098
Next approximation after 3 iteration is 1.794474, f(x) = -0.016025
Next approximation after 4 iteration is 1.796107, f(x) = -0.001863
Next approximation after 5 iteration is 1.796297, f(x) = -0.000217
Next approximation after 6 iteration is 1.796319, f(x) = -0.000025
Root = 1.7963
*/
9. 6
2. Program to solve non-linear equation using the false position method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
void main(){
int i = 0;
float x1, x2, x3, f1, f2, f3, epsilon, delta;
clrscr();
printf("nEnter two initial approximations x1, x2: ");
scanf("%f %f", &x1, &x2);
printf("nEnter two very small number epsilon and delta: ");
scanf("%f %f", &epsilon, &delta);
f1 = f(x1);
f2 = f(x2);
printf("nf(%.2f) = %9f, f(%.2f) = %9f", x1, f1, x2, f2);
if(f1 * f2 > 0){
printf("nInitial approximations x1 = %f, x2 = %f are not proper.",
x1, x2);
exit(0);
}
do{
if(fabs(f2 - f1) < delta){
printf("nSlope curve is too small.");
exit(0);
}
x3 = ((x1 * f2) - (x2 * f1))/ (f2 - f1);
i++;
f3 = f(x3);
printf("nNext approximation after %2d iteration is %f, f(x) =
%9f", i, x3, f3);
if(f1 * f3 < 0){
x2 = x3;
f2 = f3;
}
else{
x1 = x3;
f1 = f3;
}
}while((fabs(f3) > epsilon) && (f3 != 0));
printf("nRoot = %.4f", x3);
getch();
}
12. 9
/* Output 3
Enter the value of x1: 25
Enter two very small number epsilon and delta: .0001 .001
Enter the no. of iterations: 15
Next approximation after 1 iteration is 16.677694, f(x) = 4618.147729
Next approximation after 2 iteration is 11.136602, f(x) = 1366.068406
Next approximation after 3 iteration is 7.455189, f(x) = 402.903083
Next approximation after 4 iteration is 5.024248, f(x) = 117.803195
Next approximation after 5 iteration is 3.447847, f(x) = 33.538952
Next approximation after 6 iteration is 2.480273, f(x) = 8.777765
Next approximation after 7 iteration is 1.977401, f(x) = 1.754469
Next approximation after 8 iteration is 1.813896, f(x) = 0.154220
Next approximation after 9 iteration is 1.796511, f(x) = 0.001640
Next approximation after 10 iteration is 1.796322, f(x) = -0.000000
Next approximation after 11 iteration is 1.796322, f(x) = -0.000000
Error = 0.000000
Root = 1.7963
*/
13. 10
3. Program to solve non-linear equation using Newton-Raphson method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x*x-x-4)
#define df(x) (3*x*x-1)
void main(){
int i, n;
float x1, x2, epsilon, delta, err;
clrscr();
printf("nEnter the value of x1: ");
scanf("%f", &x1);
printf("nEnter two very small number epsilon and delta: ");
scanf("%f %f", &epsilon, &delta);
printf("nEnter the no. of iterations: ");
scanf("%d", &n);
for(i = 0; i < n; i++){
if(df(x1) < delta){
printf("nSlope of curve is too small.");
goto exit;
}
x2 = x1 - f(x1)/ df(x1);
printf("nNext approximation after %2d iteration is %9f, f(x) =
%11f", (i+1), x2, f(x2));
err = fabs((x2 - x1)/ x2);
if(err < epsilon){
printf("nnError t= %f", err);
printf("nRoot t= %.4f", x2);
goto exit;
}
x1 = x2;
}
printf("nnSolution does not converge after %d iterations.", n);
exit:
getch();
}
14. 11
/* Output 4
Enter two initial approximations x1, x2: 1 2
Enter very small number epsilon & delta: .0001 .001
Enter no. of iterations: 10
Next approximation after 1 iteration is 2.000000, f(x) = 2.000000
Next approximation after 2 iteration is 1.666667, f(x) = -1.037037
Next approximation after 3 iteration is 1.780488, f(x) = -0.136098
Next approximation after 4 iteration is 1.797682, f(x) = 0.011815
Next approximation after 5 iteration is 1.796308, f(x) = -0.000117
Error = 0.000007
Root = 1.7963
*/
15. 12
4. Program to solve non-linear equation using Secant method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x*x-x-4)
void main(){
int i, n;
float x1, x2, x3, epsilon, delta, err;
clrscr();
printf("nEnter two initial approximations x1, x2: ");
scanf("%f %f", &x1, &x2);
printf("nEnter very small number epsilon & delta: ");
scanf("%f %f", &epsilon, &delta);
printf("nEnter no. of iterations: ");
scanf("%d", &n);
for(i = 1; i <= n; i++){
if(fabs(f(x2) - f(x1)) < delta){
printf("nSlope curve is too small.");
goto exit;
}
x3 = ((x1 * f(x2)) - (x2 * f(x1)))/ (f(x2) - f(x1));
printf("nNext approximation after %2d iteration is %9f, f(x) =
%11f", i, x2, f(x2));
err = fabs((x3 - x2)/ x3);
if(err < epsilon){
printf("nnError t= %f", err);
printf("nRoot t= %.4f", x2);
goto exit;
}
x1 = x2;
x2 = x3;
}
printf("nSolution does not converge after %d iterations.", n);
exit:
getch();
}
16. 13
/* Output 5
Enter the number of equations: 3
Enter the coefficients of unknowns and RHS value of equation 1
2 -3 1 -1
Enter the coefficients of unknowns and RHS value of equation 2
1 4 5 25
Enter the coefficients of unknowns and RHS value of equation 3
3 -4 1 2
Solution of system linear equations is
x[1] = 8.700
x[2] = 5.700
x[3] = -1.300
*/
17. 14
5. Program to solve system of linear equation using Gauss Elimination
Method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 50
void main(){
int i, j, k, n;
float a[max][max], x[max], u, sum;
clrscr();
printf("nEnter the number of equations: ");
scanf("%d", &n);
for(i = 0; i < n; i++){
printf("nEnter the coefficients of unknowns and RHS value of
equation %dn", i+1);
for(j = 0; j < n+1; j++)
scanf("%f", &a[i][j]);
}
for(k = 0; k < n-1; k++){
if(a[k][k] == 0){
printf("nDivision by zero.");
exit(0);
}
for(i = k+1; i < n; i++){
u = a[i][k]/ a[k][k];
for(j = 0; j < n+1; j++)
a[i][j] -= u * a[k][j];
}
}
for(i = n-1; i >= 0; i--){
sum = 0;
for(j = i+1; j < n; j++)
sum += a[i][j] * x[j];
x[i] = (a[i][n] - sum)/ a[i][i];
}
printf("nSolution of system linear equations is");
for(i = 0; i < n; i++)
printf("nx[%d] = %6.3f", i+1, x[i]);
getch();
}
18. 15
/* Output 6
Enter the number of equations: 3
Enter the coefficients of unknowns and RHS value of equation 1
2 -3 1 -1
Enter the coefficients of unknowns and RHS value of equation 2
1 4 5 25
Enter the coefficients of unknowns and RHS value of equation 3
3 -4 1 2
Solution of system of linear equation is
x[1] = 8.700
x[2] = 5.700
x[3] = -1.300
*/
19. 16
6. Program to solve system of linear equation using Gauss Jordan Method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 50
void main(){
int i, j, k, n;
float a[max][max], x[max], u, sum, temp;
clrscr();
printf("nEnter the number of equations: ");
scanf("%d", &n);
for(i = 0; i < n; i++){
printf("nEnter the coefficients of unknowns and RHS value of
equation %dn", i+1);
for(j = 0; j < n+1; j++)
scanf("%f", &a[i][j]);
}
for(k = 0; k < n; k++){
if(a[k][k] == 0){
printf("nDivision by zero.");
exit(0);
}
temp = a[k][k];
for(j = k; j < n+1; j++)
a[k][j] /= temp;
for(i = 0; i < n; i++){
if(i != k){
u = a[i][k];
for(j = k; j < n+1; j++)
a[i][j] -= u * a[k][j];
}
}
}
printf("nSolution of system of linear equation is");
for(i = 0; i < n; i++){
x[i] = a[i][n];
printf("nx[%d] = %6.3f", i+1, x[i]);
}
getch();
}
20. 17
/* Output 7
Enter the number of equations: 3
Enter the coefficients of unknown and RHS value of equation 1
5 2 1 12
Enter the coefficients of unknown and RHS value of equation 2
1 4 2 15
Enter the coefficients of unknown and RHS value of equation 3
1 2 5 20
Enter the number of iterations: 15
Enter a very small of epsilon: .01
Solution coverages after 6 iterations
x[1] = 1.000
x[2] = 2.000
x[3] = 3.000
*/
21. 18
7. Program to solve system of linear equation using Gauss Seidel Method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(){
int n, maxiter, i, j, k;
float a[10][11], x[10], sum, temp, error, e, bigerror;
clrscr();
printf("nEnter the number of equations: ");
scanf("%d", &n);
for(i = 0; i < n; i++){
printf("nEnter the coefficients of unknown and RHS value of
equation %dn", i+1);
for(j = 0; j < n+1; j++)
scanf("%f", &a[i][j]);
}
printf("nEnter the number of iterations: ");
scanf("%d", &maxiter);
printf("nEnter a very small of epsilon: ");
scanf("%f", &e);
for(i = 0; i < n; i++)
x[i] = 0;
for(k = 1; k <= maxiter; k++){
bigerror = 0;
for(i = 0; i < n; i++){
sum = 0;
for(j = 0; j < n; j++){
if(i != j)
sum += a[i][j] * x[j];
}
temp = (a[i][n]-sum)/ a[i][i];
error = fabs((temp-x[i])/ temp);
if(error > bigerror)
bigerror = error;
x[i] = temp;
}
if(bigerror <= e){
printf("nSolution coverages after %d iterations", k);
for(i = 0; i < n; i++)
printf("nx[%d] = %.3f", i+1, x[i]);
exit(0);
}
}
35. 32
}
for(j = 0; j < n-1; j++){
m += 8;
l = 6;
gotoxy(m, l);
printf("D%d", j+1);
l += 2;
gotoxy(m, l);
for(i = 0; i < n-j-1; i++){
printf("%.2f", d[i][j]);
l += 2;
gotoxy(m, l);
}
}
getch();
}
36. 33
/* Output 11
Enter the value of N: 5
Enter 5 pairs of (x, y):
2 9
2.25 10.06
2.5 11.25
2.75 12.56
3 14
Enter the value of X to interpollate the value of Y: 2.35
Interpollated value of Y = 10.5214
*/
37. 34
11. Program to implement Newton Forward Difference Interpolation
Polynomial.
#include<stdio.h>
#include<conio.h>
void main(){
int i, j, k, n, m, l;
float a, d[20][20], x[20], y[20], u, prod, sum;
clrscr();
printf("nEnter the value of N: ");
scanf("%d", &n);
printf("nEnter %d pairs of (x, y):n", n);
for(i = 0; i < n; i++)
scanf("%f %f", &x[i], &y[i]);
printf("nEnter the value of X to interpollate the value of Y: ");
scanf("%f", &a);
if((a < x[0]) || (a > x[n-1])){
printf("nThe value lies outside the tabulated range.");
getch();
exit(0);
}
i = 1;
while(a < x[i])
i++;
k = i-1;
u = (a - x[k])/ (x[k+1] - x[k]);
for(j = 0; j <n-1; j++){
for(i = 0; i < n-j; i++){
if(j == 0)
d[i][j] = y[i+1] - y[i];
else
d[i][j] = d[i+1][j-1] - d[i][j-1];
}
}
sum = y[k];
for(i = 0; i < n-k; i++){
prod = 1.0;
for(j = 0; j <= i; j++)
prod *= (u-j) / (j+1);
sum += d[k][i] * prod;
}
printf("nInterpollated value of Y = %.4f", sum);
getch();
}
42. 39
/* Output 13
Enter the value of N: 5
Enter 5 pairs of (x, y):
2.00 9.00
2.25 10.06
2.50 11.25
2.75 12.56
3.00 14.00
Enter the value of X to interpollate the value of Y: 2.35
Interpollated value of Y = 10.5214
*/
43. 40
13. Program to implement Newton Divided Differences Interpolation
Polynomial.
#include<stdio.h>
#include<conio.h>
void main(){
int i, j, k, n, m, l;
float a, d[20][20], x[20], y[20], prod, sum;
clrscr();
printf("nEnter the value of N: ");
scanf("%d", &n);
printf("nEnter %d pairs of (x, y): n", n);
for(i = 0; i < n; i++)
scanf("%f %f", &x[i], &y[i]);
printf("nEnter the value of X to interpollate the value of Y: ");
scanf("%f", &a);
if((a < x[0]) || (a > x[n-1])){
printf("nThe value lies outside the tabulated range.");
getch();
exit(0);
}
i = 1;
while(a < x[i])
i++;
k = i-1;
for(j = 1; j <= n; j++){
for(i = 0; i < n-j; i++){
if(j == 1)
d[i][j-1] = (y[i+1]-y[i])/ (x[i+j] - x[i]);
else
d[i][j-1] = (d[i+1][j-2] - d[i][j-2])/ (x[i+j] - x[i]);
}
}
sum = y[k];
for(i = 0; i < n-k; i++){
prod = 1.0;
for(j = 0; j <= i; j++)
prod *= (a - x[k+j]);
sum += d[k][i] * prod;
}
printf("nInterpollated value of Y = %.4f", sum);
getch();
}
44. 41
/* Output 14
Enter the values of a and b: 0 2
Enter the number of sub-intervals: 4
Integral: 1.0678
*/
45. 42
14. Program to implement Trapezoidal Rule for a known function.
#include<stdio.h>
#include<conio.h>
#include<process.h>
float f(float x){
return (1/(1 + x * x * x * x));
}
void main(){
int i, n;
float a, b, h, sum, intg;
clrscr();
printf("nEnter the values of a and b: ");
scanf("%f %f", &a, &b);
printf("nEnter the number of sub-intervals: ");
scanf("%d", &n);
if(a > b){
printf("Invalid Input.");
getch();
exit(0);
}
h = (b-a)/ n;
sum = f(a) + f(b);
for(i = 1; i < n; i++)
sum += 2 * f(a + (i * h));
intg = sum * (h/2);
printf("nIntegral: %.4f", intg);
getch();
}
46. 43
/* Output 15
Enter the values of a and b: 1 2
Enter the number of sub-intervals: 8
Integral: 0.2031
*/
47. 44
15. Program to implement Simpsons 1/3 Rule for a known function.
#include<stdio.h>
#include<conio.h>
#include<process.h>
float f(float x){
return (1/(1 + x * x * x * x));
}
void main(){
int i, n;
float a, b, h, sum, intg;
clrscr();
printf("nEnter the values of a and b: ");
scanf("%f %f", &a, &b);
printf("nEnter the number of sub-intervals: ");
scanf("%d", &n);
if(a > b){
printf("Invalid Input...");
getch();
exit(0);
}
h = (b-a)/ n;
sum = f(a) + f(b);
for(i = 1; i < n; i++){
if((i % 2) == 0)
sum += 2 * f(a + (i * h));
else
sum += 4 * f(a + (i * h));
}
intg = sum * (h/3);
printf("nIntegral: %.4f", intg);
getch();
}
48. 45
/* Output 16
Enter the values of a and b: 0 2
Enter the number of sub-intervals: 9
Integral: 0.0833
*/
49. 46
16. Program to implement Simpsons 3/8 Rule for a known function.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 50
float f(float x){
return (1/(1 + x * x));
}
void main(){
int i, n;
float a, b, h, x[max], y[max], sum, intg;
clrscr();
printf("nEnter the values of a and b: ");
scanf("%f %f", &a, &b);
printf("nEnter the number of sub-intervals: ");
scanf("%d", &n);
if(a > b){
printf("Invalid Input...");
getch();
exit(0);
}
h = (b-a)/ n;
for(i = 0; i <= n; i++){
x[i] = a + i * h;
y[i] = f(x[i]);
}
sum = y[0] + y[n];
for(i = 1; i < n; i++){
if((i % 3) != 0)
sum += 3 * y[i];
else
sum += 2 * y[i];
}
intg = sum * 3 * (h/8);
printf("nIntegral: %.4f", intg);
getch();
}