SlideShare a Scribd company logo
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
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
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
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
*/
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){
3
4
return (x * x * x - x - 4);
}
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
*/
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();
}
7
8
float f(float x){
return (x * x * x - x - 4);
}
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
*/
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();
}
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
*/
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();
}
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
*/
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();
}
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
*/
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();
}
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
*/
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);
}
}
19
20
printf("nSolution does not converge in %d iterations", maxiter);
getch();
}
21
/* Output 8
Enter the value of N: 5
Enter 5 pairs of (x, y):
2 10
3 14
4 18
5 20
6 26
X Y D1 D2 D3 D4
2.00 10.00 4.00 0.00 -2.00 8.00
3.00 14.00 4.00 -2.00 6.00
4.00 18.00 2.00 4.00
5.00 20.00 6.00
6.00 26.00
*/
22
8. Program to generate Newton Forward Differences Table.
#include<stdio.h>
#include<conio.h>
void main(){
int i, j, n, m, l;
float d[20][20], x[20], y[20];
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]);
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];
}
}
clrscr();
m = 6;
l = 6;
gotoxy(m, l);
printf("X");
l += 2;
gotoxy(m, l);
for(i = 0; i < n; i++){
printf("%.2f", x[i]);
l += 2;
gotoxy(m, l);
}
m += 8;
l = 6;
gotoxy(m, l);
printf("Y");
l += 2;
gotoxy(m, l);
for(i = 0; i < n; i++){
printf("%.2f", y[i]);
l += 2;
gotoxy(m, l);
}
23
24
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();
}
25
/* Output 9
Enter the value of N: 5
Enter 5 pairs of (x, y):
7.00 98
7.25 80
7.50 66
7.75 55
8.00 50
X Y D1 D2 D3 D4
7.00 98.00
7.25 80.00 -18.00
7.50 66.00 -14.00 4.00
7.75 55.00 -11.00 3.00 -1.00
8.00 50.00 -5.00 6.00 3.00 4.00
*/
26
9. Program to generate Newton Backward Differences Table.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int i, j, n, m, l, o = 4;
float d[20][20], x[20], y[20];
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]);
for(j = 0; j < n-1; j++){
for(i = j+1; i < n; i++){
if(j == 0)
d[i][j] = y[i] - y[i-1];
else
d[i][j] = d[i][j-1] - d[i-1][j-1];
}
}
clrscr();
m = 6;
l = 6;
gotoxy(m, l);
printf("X");
l += 2;
gotoxy(m, l);
for(i = 0; i < n; i++){
printf("%.2f", x[i]);
l += 2;
gotoxy(m, l);
}
m += 8;
l = 6;
gotoxy(m, l);
printf("Y");
l += 2;
gotoxy(m, l);
for(i = 0; i < n; i++){
printf("%.2f", y[i]);
l += 2;
gotoxy(m, l);
27
28
}
for(j = 0; j < n-1; j++){
m += 8;
l = 6;
gotoxy(m, l);
printf("D%d", j+1);
l += o;
gotoxy(m, l);
for(i = j+1; i < n; i++){
printf("%.2f", d[i][j]);
l += 2;
gotoxy(m, l);
}
o += 2;
}
getch();
}
29
/* Output 10
Enter the value of N: 5
Enter 5 pairs of (x, y):
2 10
3 17
5 23
9 35
12 46
X Y D1 D2 D3 D4
2.00 10.00 7.00 -1.33 0.19 -0.02
3.00 17.00 3.00 0.00 0.01
5.00 23.00 3.00 0.10
9.00 35.00 3.67
12.00 46.00
*/
30
10. Program to generate Newton Divided Difference Table.
#include<stdio.h>
#include<conio.h>
void main(){
int i, j, n, m, l;
float d[20][20], x[20], y[20];
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]);
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]);
}
}
exit(0);
clrscr();
m = 6;
l = 6;
gotoxy(m, l);
printf("X");
l += 2;
gotoxy(m, l);
for(i = 0; i < n; i++){
printf("%.2f", x[i]);
l += 2;
gotoxy(m, l);
}
m += 8;
l = 6;
gotoxy(m, l);
printf("Y");
l += 2;
gotoxy(m, l);
for(i = 0; i < n; i++){
printf("%.2f", y[i]);
l += 2;
gotoxy(m, l);
31
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();
}
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
*/
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();
}
35
/* Output 12
*/
36
12. Program to implement Newton Backward Difference Interpolation
Polynomial.
#include<stdio.h>
#include<conio.h>
void main(){
int i, j, k, n;
float a, d[20][20], x[20], y[20], u, prod, sum, fact;
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 = n-1;
while(a > x[i])
i--;
k = i;
u = (x[k] - a)/ (x[k] - x[k-1]);
for(j = 0; j < n-1; j++){
for(i = j+1; i < n; i++){
if(j == 0)
d[i][j] = y[i] - y[i-1];
else
d[i][j] = d[i][j-1] - d[i-1][j-1];
}
}
sum = y[k];
for(i = 0; i < k-1; i++){
prod = 1.0;
fact = 1.0;
for(j = 0; j < i; j++){
prod *= (u+j);
fact *= (j+1);
}
sum += d[k][i] * prod/ fact;
}
37
38
printf("nInterpollated value of Y = %.4f", sum);
getch();
}
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
*/
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();
}
41
/* Output 14
Enter the values of a and b: 0 2
Enter the number of sub-intervals: 4
Integral: 1.0678
*/
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();
}
43
/* Output 15
Enter the values of a and b: 1 2
Enter the number of sub-intervals: 8
Integral: 0.2031
*/
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();
}
45
/* Output 16
Enter the values of a and b: 0 2
Enter the number of sub-intervals: 9
Integral: 0.0833
*/
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();
}

More Related Content

What's hot (20)

PPTX
Recursion
Abdur Rehman
 
PPTX
Linear dependence & independence vectors
Rakib Hossain
 
PPTX
Application of Derivatives
Abdullah Al Mamun
 
PPTX
Functions in c++
Rokonuzzaman Rony
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PDF
Computer graphics lab report with code in cpp
Alamgir Hossain
 
PPT
Bucket sort
Hossain Md Shakhawat
 
DOCX
Java questions for viva
Vipul Naik
 
PPTX
Mini Project PPT
Faiz Ahmad Khan
 
PPTX
Combinatorics.pptx
HanachiTan
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Access specifier
zindadili
 
PDF
Arrays in python
moazamali28
 
PPTX
Geometric transformation 2d chapter 5
geethawilliam
 
PPTX
Methods of variation of parameters- advance engineering mathe mathematics
Kaushal Patel
 
PPTX
List in Python
Siddique Ibrahim
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
DOCX
Report 02(Binary Search)
Md. Bashartullah (Rabby)
 
Recursion
Abdur Rehman
 
Linear dependence & independence vectors
Rakib Hossain
 
Application of Derivatives
Abdullah Al Mamun
 
Functions in c++
Rokonuzzaman Rony
 
Chapter 03 python libraries
Praveen M Jigajinni
 
Computer graphics lab report with code in cpp
Alamgir Hossain
 
Java questions for viva
Vipul Naik
 
Mini Project PPT
Faiz Ahmad Khan
 
Combinatorics.pptx
HanachiTan
 
Data structure array
MajidHamidAli
 
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
 
Access specifier
zindadili
 
Arrays in python
moazamali28
 
Geometric transformation 2d chapter 5
geethawilliam
 
Methods of variation of parameters- advance engineering mathe mathematics
Kaushal Patel
 
List in Python
Siddique Ibrahim
 
Divide and conquer
Dr Shashikant Athawale
 
Report 02(Binary Search)
Md. Bashartullah (Rabby)
 

Similar to Computer Oriented Numerical Methods Practical File (20)

PPT
Numerical method
Kumar Gaurav
 
PDF
Assignment on Numerical Method C Code
Syed Ahmed Zaki
 
PDF
C++ TUTORIAL 9
Farhan Ab Rahman
 
PDF
BCSL 058 solved assignment
Indira Gnadhi National Open University (IGNOU)
 
DOCX
Trabajo Scilab
alexistorres
 
DOCX
Matlab lab manual
nmahi96
 
DOCX
scientific computing
saurabhramteke7
 
DOC
Sary
sarylozano
 
PDF
C++ TUTORIAL 7
Farhan Ab Rahman
 
PPTX
2. Fixed Point Iteration.pptx
saadhaq6
 
PDF
C++ TUTORIAL 6
Farhan Ab Rahman
 
PDF
21221
inKFUPM
 
PDF
C language numanal
aluavi
 
PPTX
Finding root of equation (numarical method)
Rajan Thakkar
 
PDF
Numerical methods course project report
Zeeshan Ali
 
PDF
C++ TUTORIAL 10
Farhan Ab Rahman
 
PDF
Numerical differentation with c
Yagya Dev Bhardwaj
 
PDF
Numerical Methods for Engineers 6th Edition Chapra Solutions Manual
webavaq
 
DOCX
Martha
MarthaPaz
 
Numerical method
Kumar Gaurav
 
Assignment on Numerical Method C Code
Syed Ahmed Zaki
 
C++ TUTORIAL 9
Farhan Ab Rahman
 
Trabajo Scilab
alexistorres
 
Matlab lab manual
nmahi96
 
scientific computing
saurabhramteke7
 
C++ TUTORIAL 7
Farhan Ab Rahman
 
2. Fixed Point Iteration.pptx
saadhaq6
 
C++ TUTORIAL 6
Farhan Ab Rahman
 
21221
inKFUPM
 
C language numanal
aluavi
 
Finding root of equation (numarical method)
Rajan Thakkar
 
Numerical methods course project report
Zeeshan Ali
 
C++ TUTORIAL 10
Farhan Ab Rahman
 
Numerical differentation with c
Yagya Dev Bhardwaj
 
Numerical Methods for Engineers 6th Edition Chapra Solutions Manual
webavaq
 
Martha
MarthaPaz
 
Ad

More from Harjinder Singh (6)

PDF
A4 Size Designer Project Sheets
Harjinder Singh
 
PDF
Data Structures Practical File
Harjinder Singh
 
PDF
Object Oriented Programming Using C++ Practical File
Harjinder Singh
 
PPTX
Solar System
Harjinder Singh
 
PDF
Classification of Computers
Harjinder Singh
 
PDF
Economics Class 12 CBSE project on GST (Goods and Services Tax)
Harjinder Singh
 
A4 Size Designer Project Sheets
Harjinder Singh
 
Data Structures Practical File
Harjinder Singh
 
Object Oriented Programming Using C++ Practical File
Harjinder Singh
 
Solar System
Harjinder Singh
 
Classification of Computers
Harjinder Singh
 
Economics Class 12 CBSE project on GST (Goods and Services Tax)
Harjinder Singh
 
Ad

Recently uploaded (20)

PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PPTX
How Physics Enhances Our Quality of Life.pptx
AngeliqueTolentinoDe
 
PDF
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
PPTX
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
PPTX
ENGLISH -PPT- Week1 Quarter1 -day-1.pptx
garcialhavz
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PPTX
Project 4 PART 1 AI Assistant Vocational Education
barmanjit380
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PPTX
How to use _name_search() method in Odoo 18
Celine George
 
PPTX
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
PPTX
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
PDF
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
PPTX
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
PPTX
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
How Physics Enhances Our Quality of Life.pptx
AngeliqueTolentinoDe
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
ENGLISH -PPT- Week1 Quarter1 -day-1.pptx
garcialhavz
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
Project 4 PART 1 AI Assistant Vocational Education
barmanjit380
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
How to use _name_search() method in Odoo 18
Celine George
 
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
 

Computer Oriented Numerical Methods Practical File

  • 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){
  • 6. 3
  • 7. 4 return (x * x * x - x - 4); }
  • 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(); }
  • 10. 7
  • 11. 8 float f(float x){ return (x * x * x - x - 4); }
  • 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); } }
  • 22. 19
  • 23. 20 printf("nSolution does not converge in %d iterations", maxiter); getch(); }
  • 24. 21 /* Output 8 Enter the value of N: 5 Enter 5 pairs of (x, y): 2 10 3 14 4 18 5 20 6 26 X Y D1 D2 D3 D4 2.00 10.00 4.00 0.00 -2.00 8.00 3.00 14.00 4.00 -2.00 6.00 4.00 18.00 2.00 4.00 5.00 20.00 6.00 6.00 26.00 */
  • 25. 22 8. Program to generate Newton Forward Differences Table. #include<stdio.h> #include<conio.h> void main(){ int i, j, n, m, l; float d[20][20], x[20], y[20]; 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]); 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]; } } clrscr(); m = 6; l = 6; gotoxy(m, l); printf("X"); l += 2; gotoxy(m, l); for(i = 0; i < n; i++){ printf("%.2f", x[i]); l += 2; gotoxy(m, l); } m += 8; l = 6; gotoxy(m, l); printf("Y"); l += 2; gotoxy(m, l); for(i = 0; i < n; i++){ printf("%.2f", y[i]); l += 2; gotoxy(m, l); }
  • 26. 23
  • 27. 24 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(); }
  • 28. 25 /* Output 9 Enter the value of N: 5 Enter 5 pairs of (x, y): 7.00 98 7.25 80 7.50 66 7.75 55 8.00 50 X Y D1 D2 D3 D4 7.00 98.00 7.25 80.00 -18.00 7.50 66.00 -14.00 4.00 7.75 55.00 -11.00 3.00 -1.00 8.00 50.00 -5.00 6.00 3.00 4.00 */
  • 29. 26 9. Program to generate Newton Backward Differences Table. #include<stdio.h> #include<conio.h> #include<graphics.h> void main(){ int i, j, n, m, l, o = 4; float d[20][20], x[20], y[20]; 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]); for(j = 0; j < n-1; j++){ for(i = j+1; i < n; i++){ if(j == 0) d[i][j] = y[i] - y[i-1]; else d[i][j] = d[i][j-1] - d[i-1][j-1]; } } clrscr(); m = 6; l = 6; gotoxy(m, l); printf("X"); l += 2; gotoxy(m, l); for(i = 0; i < n; i++){ printf("%.2f", x[i]); l += 2; gotoxy(m, l); } m += 8; l = 6; gotoxy(m, l); printf("Y"); l += 2; gotoxy(m, l); for(i = 0; i < n; i++){ printf("%.2f", y[i]); l += 2; gotoxy(m, l);
  • 30. 27
  • 31. 28 } for(j = 0; j < n-1; j++){ m += 8; l = 6; gotoxy(m, l); printf("D%d", j+1); l += o; gotoxy(m, l); for(i = j+1; i < n; i++){ printf("%.2f", d[i][j]); l += 2; gotoxy(m, l); } o += 2; } getch(); }
  • 32. 29 /* Output 10 Enter the value of N: 5 Enter 5 pairs of (x, y): 2 10 3 17 5 23 9 35 12 46 X Y D1 D2 D3 D4 2.00 10.00 7.00 -1.33 0.19 -0.02 3.00 17.00 3.00 0.00 0.01 5.00 23.00 3.00 0.10 9.00 35.00 3.67 12.00 46.00 */
  • 33. 30 10. Program to generate Newton Divided Difference Table. #include<stdio.h> #include<conio.h> void main(){ int i, j, n, m, l; float d[20][20], x[20], y[20]; 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]); 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]); } } exit(0); clrscr(); m = 6; l = 6; gotoxy(m, l); printf("X"); l += 2; gotoxy(m, l); for(i = 0; i < n; i++){ printf("%.2f", x[i]); l += 2; gotoxy(m, l); } m += 8; l = 6; gotoxy(m, l); printf("Y"); l += 2; gotoxy(m, l); for(i = 0; i < n; i++){ printf("%.2f", y[i]); l += 2; gotoxy(m, l);
  • 34. 31
  • 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(); }
  • 39. 36 12. Program to implement Newton Backward Difference Interpolation Polynomial. #include<stdio.h> #include<conio.h> void main(){ int i, j, k, n; float a, d[20][20], x[20], y[20], u, prod, sum, fact; 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 = n-1; while(a > x[i]) i--; k = i; u = (x[k] - a)/ (x[k] - x[k-1]); for(j = 0; j < n-1; j++){ for(i = j+1; i < n; i++){ if(j == 0) d[i][j] = y[i] - y[i-1]; else d[i][j] = d[i][j-1] - d[i-1][j-1]; } } sum = y[k]; for(i = 0; i < k-1; i++){ prod = 1.0; fact = 1.0; for(j = 0; j < i; j++){ prod *= (u+j); fact *= (j+1); } sum += d[k][i] * prod/ fact; }
  • 40. 37
  • 41. 38 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(); }