programs in c

48
PROGRAMS IN C PROGRAM 1 /* PROGRAM BISECTION Finding simple root of f(x) = 0 using bisection method. Read the end points of the interval (a, b) in which the root lies, maximum number of iterations n and error tolerance eps. */ #include <stdio.h> #include <math.h> float f(); main() { float a, b, x, eps, fa, fx, ff, s; int i, n; FILE *fp; fp = fopen("result","w"); printf("Please input end points of interval (a, b),\n"); printf("in which the root lies\n"); printf("n: number of iterations\n"); printf("eps: error tolerance\n"); scanf("%f %f %d %E",&a, &b, &n, &eps); fprintf(fp,"A = %f, B = %f, N = %d,", a, b, n); fprintf(fp," EPS = %e\n", eps); /* Compute the bisection point of a and b */ x = (a + b)/2.0; for(i = 1; i <= n; i++) { fa = f(a); fx = f(x); if(fabs(fx) <= eps) goto l1; ff = fa * fx; if(ff < 0.0)

Upload: rishabh-agarwal

Post on 14-Apr-2016

13 views

Category:

Documents


1 download

DESCRIPTION

good

TRANSCRIPT

Page 1: Programs in C

PROGRAMS IN C

PROGRAM 1

/* PROGRAM BISECTIONFinding simple root of f(x) = 0 using bisection method. Read the end points of the interval (a, b) in which the root lies, maximum number of iterations n and error tolerance eps. */

#include <stdio.h>#include <math.h>

float f();

main(){ float a, b, x, eps, fa, fx, ff, s; int i, n; FILE *fp;

fp = fopen("result","w");

printf("Please input end points of interval (a, b),\n"); printf("in which the root lies\n"); printf("n: number of iterations\n"); printf("eps: error tolerance\n"); scanf("%f %f %d %E",&a, &b, &n, &eps); fprintf(fp,"A = %f, B = %f, N = %d,", a, b, n); fprintf(fp," EPS = %e\n", eps);

/* Compute the bisection point of a and b */

x = (a + b)/2.0; for(i = 1; i <= n; i++) {

fa = f(a); fx = f(x); if(fabs(fx) <= eps) goto l1; ff = fa * fx; if(ff < 0.0) x = (a + x)/2.0; else

{ a = x; x = (x + b)/2.0; }

} printf("No. of iterations not sufficient\n"); goto l2;

Page 2: Programs in C

l1: fprintf(fp,"ITERATIONS = %d,",i); fprintf(fp," ROOT = %10.7f, F(X) = %E\n", x, fx); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); fclose(fp);l2: return 0; }/******************************************************************/float f(x) float x; { float fun;

fun = cos(x) - x * exp(x);return(fun);

}/*******************************************************************/A = 0.000000, B = 1.000000, N = 40, EPS = 1.000000e-04ITERATIONS = 25, ROOT = 0.5177526, F(X) = 1.434746E-05/*******************************************************************/

PROGRAM 2

/* PROGRAM REGULA-FALSI Finding a simple root of f(x)=0 using Regula-Falsi method. Read the end points of the interval (a, b) in which the root lies, maximum number of iterations n and the error tolerance eps.

*/

#include <stdio.h>#include <math.h>

float f();

main() { float a, b, x, eps, fa, fb, fx; int i, n; FILE *fp;

fp = fopen("result","w"); printf("Input the end points of the interval (a, b) in"); printf("which the root lies"); printf("n: number of iterations\n"); printf("eps: error tolerance\n"); scanf("%f %f %d %E", &a, &b, &n, &eps);

Page 3: Programs in C

fprintf(fp,"a = %f b = %f n = %d", a, b, n); fprintf(fp," eps = %e\n\n", eps);

/* Compute the value of f(x) at a & b and calculate the new approximation x and value of f(x) at x. */ for (i = 1; i <= n; i++)

{ fa = f(a); fb = f(b); x = a - (a - b) * fa / (fa - fb); fx = f(x);

if(fabs(fx) <= eps)

/* Iteration is stopped when abs(f(x)) is less than or equal to eps. Alternate conditions can also be used. */

goto l1; if((fa * fx) < 0.0) b = x; else a = x; }

printf("\nITERATIONS ARE NOT SUFFICIENT"); goto l2;l1: fprintf(fp,"Number of iterations = %d\n", i); fprintf(fp,"Root = %10.7f, f(x) = %e\n", x, fx); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");l2: return 0; }/***************************************************************/float f(x) float x; { float fun;

fun = cos(x) - x * exp(x); return(fun);

}/***************************************************************/a = 0.000000 b = 1.000000 n = 20 eps = 1.000000e-04Number of iterations = 9Root = 0.5177283, f(x) = 8.832585e-05/***************************************************************/

PROGRAM 3

/* PROGRAM SECANT METHODFinding a simple root of f(x) = 0 using Secant method. Read any two approximations to the root, say, a, b; maximum number of iterations n and the error tolerance eps. The method diverges if

Page 4: Programs in C

the approximations are far away from the exact value of the root. */

#include <stdio.h>#include <math.h>

float f();

main() { float a, b, x, eps, fa, fb, fx; int i, n; FILE *fp;

fp = fopen("result","w"); printf("Input any two approximations to the root "); printf("n: number of iterations\n"); printf("eps: error tolerance\n"); scanf("%f %f %d %E", &a, &b, &n, &eps); fprintf(fp,"a = %f b = %f n = %d", a, b, n); fprintf(fp," eps = %e\n\n", eps);

/* Compute the value of f(x) at a & b and calculate the new approximation x and value of f(x) at x. */

for (i = 1; i <= n; i++) { fa = f(a); fb = f(b); x = a - (a - b) * fa / (fa - fb); fx = f(x); if(fabs(fx) <= eps)

/* Iteration is stopped when abs(f(x)) is less than or equal to eps. Alternate conditions can also be used. */

goto l1; a = b; b = x; }

printf("\nITERATIONS ARE NOT SUFFICIENT"); goto l2;l1: fprintf(fp,"Number of iterations = %d\n", i); fprintf(fp,"Root = %10.7f, f(x) = %e\n", x, fx); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");l2: return 0; }/*********************************************************************/float f(x) float x; { float fun;

Page 5: Programs in C

fun = cos(x) - x * exp(x); return(fun);

}/********************************************************************/a = 0.100000 b = 0.200000 n = 40 eps = 1.000000e-04Number of iterations = 5Root = 0.5177556, f(x) = 5.281272e-06/********************************************************************/

PROGRAM 4

/* PROGRAM NEWTON-RAPHSON METHOD Finding a simple root of f(x) = 0 using Newton-Raphson method. Read initial approximation xold. Maximum number of iterations n and error tolerance eps. */

#include <stdio.h>#include <math.h>

float f();float df();

main() { float xold, eps, fx, dfx, xnew; int i, n; FILE *fp;

fp = fopen("result","w"); printf("Input value initial approximation xold\n"); printf("n: number of iterations\n"); printf("eps: error tolerance\n"); scanf("%f %d %E", &xold, &n, &eps); fprintf(fp,"Input value initial approximation xold\n"); fprintf(fp,"number of iterations n,"); fprintf(fp," error tolerance eps\n"); fprintf(fp,"xold = %f n = %d eps = %e\n\n", xold, n, eps);

/* Calculate f and its first derivative at xold */ for(i = 1; i <= n; i++)

{ fx = f(xold); dfx = df(xold); xnew = xold - fx / dfx;

Page 6: Programs in C

fx = f(xnew); if(fabs(fx) <= eps) goto l10;

/* Iteration is stopped when abs(f(x)) is less than or equal to eps. Alternate conditions can also be used. */

xold = xnew;}

printf("\nITERATIONS ARE NOT SUFFICIENT"); goto l20;l10: fprintf(fp,"Iterations = %d", i); fprintf(fp," Root = %10.7f, f(x) = %e\n", xnew, fx); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");l20: return 0;

}/*******************************************************************/float f(x) float x; { float fun;

fun = cos(x) - x * exp(x); return(fun);

}/******************************************************************/float df(x) float x;

{ float dfun; dfun = - sin(x) - (x + 1.0) * exp(x); return(dfun);}

/***************************************************************/Input value initial approximation xoldnumber of iterations n, error tolerance epsxold = 1.000000 n = 15 eps = 1.000000e-04Iterations = 4 Root = 0.5177574, f(x) = 2.286344e-08/***************************************************************/

PROGRAM 5

/* PROGRAM MULLER METHODFinding a root of f(x) = 0 using Muller method. Read three initial approximations x0, x1 and x2, maximum number of iterations n and error tolerance eps. */

#include <stdio.h>

Page 7: Programs in C

#include <math.h>

float f();

main() { float x, x0, x1, x2, fx, fx0, fx1, fx2; float al, dl, c, g, p, q, eps; int i, n; FILE *fp;

fp = fopen("result","w"); printf("Input three initial approximations : x0, x1, x2\n"); printf("number of iterations : n, \n"); printf("error tolerance : eps\n"); scanf("%f %f %f %d %E", &x0, &x1, &x2, &n, &eps); fprintf(fp,"Input three initial approximations x0, x1, x2\n"); fprintf(fp,"Number of iterations n and error tolerance eps\n"); fprintf(fp,"x0 = %f, x1 = %f, x2 = %f\n", x0, x1, x2); fprintf(fp,"n = %d, eps%e\n", n, eps);

/* Compute f(x) at x0, x1 and x2 */ for(i = 1; i <= n; i++)

{ fx0 = f(x0); fx1 = f(x1); fx2 = f(x2);

/* Calculate the next approximation x */ al = (x2 - x1) / (x1 - x0); dl = 1.0 + al; g = al * al * fx0 - dl * dl * fx1 + (al + dl) * fx2; c = al * (al * fx0 - dl * fx1 + fx2); q = g * g - 4.0 * dl * c * fx2; if(q < 0.0) q = 0.0; p = sqrt(q); if(g < 0.0) p = - p; al = - 2.0 * dl * fx2 / (g + p); x = x2 + (x2 - x1) * al; fx = f(x);

if(fabs(fx) <= eps) goto l10;

/* Iteration is stopped when abs(f(x)) is less than or equal to eps. Alternate conditions can also be used. */

x0 = x1; x1 = x2; x2 = x;}

Page 8: Programs in C

printf("ITERATIONS ARE NOT SUFFICIENT\n"); fprintf(fp,"\nITERATIONS ARE NOT SUFFICIENT"); goto l20;l10: fprintf(fp,"ITERATIONS = %d ROOT = %10.7f", i, x); fprintf(fp," F(x) = %e\n", fx); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); fclose(fp);l20: return 0; }/***************************************************************/float f(x) float x; { float fun;

fun = cos(x) - x * exp(x); return(fun);

}/******************************************************************/Input three initial approximations x0,x1, x2Number of iterations n and error tolerance epsx0 = -1.000000, x1 = 0.000000, x2 = 1.000000n = 10, eps1.000000e-06ITERATIONS = 4 ROOT = 0.5177574 F(x) = 2.286344e-08/******************************************************************/

PROGRAM 6

/* PROGRAM BAIRSTOW METHODExtraction of a quadratic factor from a polynomial {x**n + a[1] * x**(n - 1) + ..... + a[n-1] * x + a[n] = 0}of degree greater than two using Bairstow method. n gives the degree of the polynomial. a[i] represents coefficients of polynomial in decreasing powers of x. p & q are initial approximations. m is the number of iterations and eps is the desired accuracy. */

#include <stdio.h>#include <math.h>

main() { float a[10], b[10], c[10], p, q, cc, den, delp; float delq, eps; int i, n, m, j, k, l; FILE *fp;

Page 9: Programs in C

fp = fopen("result","w");

printf("Input initial approximations of p & q:\n"); printf("Degree of polynomial : n,\n"); printf("Number of iterations :m,\n"); printf("Desired accuracy :eps,\n"); scanf("%f %f %d %d %E", &p, &q, &n, &m, &eps); fprintf(fp," Input initial approximations of p & q:\n"); fprintf(fp,"Degree of polynomial: n,\n"); fprintf(fp,"Number of iterations :m,\n"); fprintf(fp,"Desired accuracy :eps,\n"); fprintf(fp,"p = %f, q = %f, n = %d", p, q, n); fprintf(fp," m = %d, eps = %e\n", m, eps);

/* Read coefficients of polynomial in decreasing order */ printf("Input coefficients of polynomial in decreasing"); printf(" order\n"); fprintf(fp,"Coefficients of polynomial are\n"); for (i = 1; i <= n; i++)

{ scanf("%f", &a[i]); fprintf(fp," %.4f",a[i]); }

fprintf(fp,"\n");

/* generate b[k] & c[k] */ for (j = 1; j <= m; j++)

{ b[1] = a[1] - p; b[2] = a[2] - p * b[1] - q; for (k = 3; k <= n; k++) b[k] = a[k] - p * b[k-1] - q * b[k-2]; c[1] = b[1] - p; c[2] = b[2] - p * c[1] - q; l = n - 1; for (k = 3; k <= l; k++) c[k] = b[k] - p * c[k-1] - q * c[k-2]; cc = c[n-1] - b[n-1]; den = c[n-2] * c[n-2] - cc * c[n-3]; if(fabs(den) == 0.0) { fprintf(fp,"WRONG INITIAL APPROXIMATION\n"); printf("\n WRONG INITIAL APPROXIMATION\n"); goto l2; } delp = -(b[n] * c[n-3] - b[n-1] * c[n-2]) / den; delq = -(b[n-1] * cc - b[n] * c[n-2]) / den; p = p + delp; q = q + delq; if((fabs(delp) <= eps) && (fabs(delq) <= eps)) goto l2; }

printf("ITERATIONS NOT SUFFICIENT\n"); fprintf(fp,"ITERATIONS NOT SUFFICIENT\n");

Page 10: Programs in C

goto l3;l2: fprintf(fp,"ITERATIONS = %d, P = %11.7e, ", j, p); fprintf(fp, "Q = %11.7e\n", q); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); fclose(fp);l3: return 0; }/*****************************************************************/Input initial approximations of p & q:Degree of polynomial: n,Number of iterations :m,Desired accuracy :eps,p = 0.500000, q = 0.500000, n = 4 m = 10, eps = 1.000000e-06Coefficients of polynomial are 1.0000 2.0000 1.0000 1.0000ITERATIONS = 7, P = 9.9999994e-01, Q = 1.0000000e+00/*****************************************************************/

PROGRAM 7

/* PROGRAM GAUSS ELIMINATION METHODSolution of a system of nxn linear equations using Gauss elimination method with partial pivoting. The program is for a 10x10 system. Change the dimension if higher order system is to be solved. */

#include <stdio.h>#include <math.h>

main() { float a[10][11], x[10], big, ab, t, quot, sum; int n, m, l, i, j, k, jj, kp1, nn, ip1; FILE *fp;

fp = fopen("result","w"); printf("Input number of equations : n\n"); scanf("%d", &n); fprintf(fp,"Order of the system = %d\n", n); m = n + 1; l = n - 1; printf("Input the augmented matrix row-wise\n"); fprintf(fp,"Elements of the augmented matrix :\n");

Page 11: Programs in C

for (i = 1; i <= n; i++){ for (j = 1; j <= m; j++) { scanf("%f", &a[i][j]); fprintf(fp," %.6f", a[i][j]); } fprintf(fp,"\n");}

for (k = 1; k <= l; k++) { big = fabs(a[k][k]); jj = k; kp1 = k + 1; for(i = kp1; i <= n; i++) { ab = fabs(a[i][k]);

if((big - ab) < 0.0) { big = ab; jj = i; }

} if((jj - k) > 0) { for (j = k; j <= m; j++)

{ t = a[jj][j]; a[jj][j] = a[k][j]; a[k][j] = t; }

} for (i = kp1; i <= n; i++) { quot = a[i][k]/a[k][k];

for (j = kp1; j <= m; j++) a[i][j] = a[i][j] - quot*a[k][j];

} for (i = kp1; i <= n; i++) a[i][k] = 0.0; }

x[n] = a[n][m]/a[n][n]; for (nn = 1; nn <= l; nn++)

{ sum = 0.0; i = n - nn; ip1 = i + 1; for(j = ip1; j <= n; j++) sum = sum + a[i][j]*x[j]; x[i] = (a[i][m] - sum)/a[i][i]; }

fprintf(fp,"SOLUTION VECTOR\n"); for (i = 1; i <= n; i++)

fprintf(fp," %8.5f", x[i]); fprintf(fp,"\n"); printf("PLEASE SEE FILE 'result' FOR RESULTS\n"); return 0; }/***************************************************************/Order of the system = 3

Page 12: Programs in C

Elements of the augmented matrix : 1.000000 1.000000 1.000000 6.000000 3.000000 3.000000 4.000000 20.000000 2.000000 1.000000 3.000000 13.000000SOLUTION VECTOR 3.00000 1.00000 2.00000/***************************************************************/

PROGRAM 8

/* PROGRAM JORDAN METHODMatrix inversion and solution of NXN system of equations using Gauss Jordan method. If the system of equations is larger than 15x15, change the dimensions if the float statement. */

#include <stdio.h>#include <math.h>

main() { float a[15][15], ai[15][15], b[15], x[15]; float aa[15][30], big, ab, t, p, sum; int n, m, m2, i, j, lj, k, kp1, jj, lk, li, l3; FILE *fp;

fp = fopen("result","w"); printf("Input order of matrix : n\n"); scanf("%d", &n); printf("Input augmented matrix row-wise\n"); for (i = 1; i <= n; i++)

{ for (j = 1; j <= n; j++) scanf("%f", &a[i][j]); scanf("%f", &b[i]);

} fprintf(fp,"Order of the system = %d\n", n); fprintf(fp,"Elements of the augmented matrix :\n"); for (i = 1; i <= n; i++)

{ for (j = 1; j <= n; j++) fprintf(fp," %8.4f", a[i][j]);

fprintf(fp," %8.4f\n", b[i]); }

m = n + n; m2 = n + 1;/* generate the augmented matrix aa. */ for (i = 1; i <= n; i++)

{ for (j = 1; j <= n; j++) aa[i][j] = a[i][j];

Page 13: Programs in C

} for (i = 1; i <= n; i++)

{ for (j = m2; j <= m; j++) aa[i][j] = 0.0;

} for (i = 1; i <= n; i++)

{ j = i + n; aa[i][j] = 1.0; }

/* Generate elements of b matrix. */ for (lj = 1; lj <= n; lj++)

{ /* Search for the largest pivot. */ k = lj; if(k < n) { jj = k;

big = fabs(aa[k][k]);kp1 = k + 1;for(i = kp1; i <= n; i++) { ab = fabs(aa[i][k]); if((big - ab) < 0.0)

{ big = ab; jj = i; }

}/* Interchange rows if required. */

if((jj - k) != 0) { for (j = k; j <= m; j++)

{ t = aa[jj][j]; aa[jj][j] = aa[k][j]; aa[k][j] = t; }

} } p = aa[lj][lj]; for (i = lj; i <= m; i++)

aa[lj][i] = aa[lj][i] / p; for (lk = 1; lk <= n; lk++)

{ t = aa[lk][lj]; for (li = lj; li <= m; li++) { if((lk - lj) != 0)

aa[lk][li] = aa[lk][li] - aa[lj][li] * t; }}

} for (i = 1; i <= n; i++)

{ for (j = m2; j <= m; j++) { l3 = j - n;

ai[i][l3] = aa[i][j]; } }

fprintf(fp,"\n INVERSE MATRIX\n");

Page 14: Programs in C

for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) fprintf(fp," %11.5f", ai[i][j]); fprintf(fp,"\n"); }

for (i = 1; i <= n; i++) { sum = 0.0; for (k = 1; k <= n; k++) sum = sum + ai[i][k] * b[k]; x[i] = sum; }

fprintf(fp,"\n SOLUTION VECTOR\n"); for (i = 1; i <= n; i++)

fprintf(fp," %11.5f", x[i]); fprintf(fp,"\n"); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); fclose(fp); return 0; }/*********************************************************************/Order of the system = 4Elements of the augmented matrix : 3.0000 4.0000 2.0000 2.0000 6.0000 2.0000 5.0000 3.0000 1.0000 4.0000 2.0000 2.0000 6.0000 3.0000 3.0000 1.0000 2.0000 4.0000 6.0000 6.0000 INVERSE MATRIX 0.59756 -0.46341 0.17073 -0.20732 -0.14024 0.35366 -0.18293 0.07927 -0.18902 0.08537 0.23171 -0.06707 0.07317 -0.09756 -0.12195 0.21951 SOLUTION VECTOR 1.00000 0.50000 -0.50000 1.00000/**********************************************************************/

PROGRAM 9

/* PROGRAM GAUSS-SEIDELProgram to solve a system of equations using Gauss-Seidel iteration method. Order of the matrix is n, maximum number of iterations is niter, error tolerance is eps and the initial approximation to the solution vector x is oldx. If the system of equations is larger than 10x10, change the dimensions in float. */

Page 15: Programs in C

#include <stdio.h>#include <math.h>

main() { float a[10][10], b[10], x[10], oldx[10], sum, big, c; float eps; int n, niter, i, j, ii, jj, k, l; FILE *fp;

fp = fopen("result","w");

printf("Input the order of matrix : n\n"); printf("Input the number of iterations : niter\n"); printf("Input error tolerance : eps\n"); scanf("%d %d %e", &n, &niter, &eps); fprintf(fp,"n = %d, niter = %d, eps = %e\n", n, niter, eps); printf("Input augmented matrix row-wise\n"); fprintf(fp,"Elements of the augmented matrix\n"); for (i = 1; i <= n; i++)

{ for (j = 1; j <= n; j++) { scanf("%f", &a[i][j]); fprintf(fp,"%f ", a[i][j]); } scanf("%f", &b[i]); fprintf(fp," %f\n", b[i]);}

printf("Input initial approx. to the solution vector\n"); fprintf(fp,"Initial approx. to solution vector :\n"); for (i = 1; i <= n; i++)

{ scanf("%f", &oldx[i]); fprintf(fp,"%f ", oldx[i]);}

fprintf(fp,"\n"); for (i = 1; i <= n; i++)

x[i] = oldx[i];

/* Compute the new values for x[i] */ for (ii = 1; ii <= niter; ii++)

{ for (i = 1; i <= n; i++) { sum = 0.0; for (j = 1; j <= n; j++)

{ if((i - j) != 0) sum = sum + a[i][j] * x[j]; }

x[i] = (b[i] - sum) / a[i][i]; } big = fabs(x[1] - oldx[1]); for (k = 2; k <= n; k++) { c = fabs(x[k] - oldx[k]); if(c > big)

Page 16: Programs in C

big = c; } if(big <= eps) goto l10; for (l = 1; l <= n; l++) oldx[l] = x[l];}

printf("ITERATIONS NOT SUFFICIENT\n"); fprintf(fp,"ITERATIONS NOT SUFFICIENT\n"); goto l20;l10: fprintf(fp,"Number of iterations = %d\n", ii); fprintf(fp,"Solution vector\n"); for(i = 1; i <= n; i++)

fprintf(fp," %f ", x[i]); fprintf(fp,"\n"); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");l20: return 0; }/*********************************************************************/n = 4, niter = 30, eps = 1.000000e-06Elements of the augmented matrix3.000000 4.000000 2.000000 2.000000 6.0000002.000000 5.000000 3.000000 1.000000 4.0000002.000000 2.000000 6.000000 3.000000 3.0000001.000000 2.000000 4.000000 6.000000 6.000000Initial approx. to solution vector :0.100000 0.100000 0.100000 0.100000 Number of iterations = 28Solution vector 1.000000 0.500000 -0.500000 1.000000/*********************************************************************/

PROGRAM 10

/* PROGRAM POWER METHODProgram to find the largest eigen value in magnitude and the corresponding eigen vector of a square matrix A of order n using power method. If the order of the matrix is greater than 10, change the dimensions in float. */

#include <stdio.h>#include <math.h>

main()

Page 17: Programs in C

{ float lambda[10], a[10][10], v[10], y[10], max, sum, eps; float big, c; int i, j, n, ii, niter, k, l; FILE *fp;

fp = fopen("result","w");

/* Read the order of matrix A, number of iterations, coefficients of matrix A and the initial vector c.*/

printf("Input the order of matrix :n\n"); printf("Input number of iterations : niter\n"); printf("Input error tolerance : eps\n"); scanf("%d %d %e", &n, &niter, &eps); fprintf(fp,"Order of the matrix = %d\n", n); fprintf(fp,"Number of iterations = %d\n", niter); fprintf(fp,"Error tolerance = %e\n", eps); printf("Input the coefficients of matrix row-wise\n"); fprintf(fp,"Elements of the matrix\n"); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++)

{ scanf("%f", &a[i][j]); fprintf(fp," %f", a[i][j]); } fprintf(fp,"\n");

} printf("Input the elements of the approx. eigen vector\n"); fprintf(fp,"Approx. eigen vector\n"); for (i = 1; i <= n; i++) { scanf("%f", &v[i]);

fprintf(fp," %f", v[i]); } fprintf(fp, "\n"); for (ii = 1; ii <= niter; ii++) { for (i = 1; i <= n; i++)

{ sum = 0.0; for (k = 1; k <= n; k++)

sum = sum + a[i][k] * v[k]; y[i] = sum; } for (i = 1; i <= n; i++) lambda[i] = fabs(y[i] / v[i]);

/* Normalise the vector y. */ max = fabs(y[1]); for (i = 2; i <= n; i++) { if(fabs(y[i]) > max)

max = fabs(y[i]); } for (i = 1; i <= n; i++)

Page 18: Programs in C

v[i] = y[i] / max; big = 0.0; for (j = 1; j <= n - 1; j++) { for (i = j + 1; i <= n; i++) {

c = fabs(lambda[j] - lambda[i]);if(big < c) big = c;

} } if(big <= eps) goto l1;

} printf("NUMBER OF ITERATIONS NOT SUFFICIENT\n"); fprintf(fp,"NUMBER OF ITERATIONS NOT SUFFICIENT\n"); goto l2;l1: fprintf(fp,"Number of iterations = %d\n", ii); fprintf(fp,"Approx. to Eigen value = "); for (l = 1; l <= n; l++)

fprintf(fp," %f", lambda[l]); fprintf(fp,"\n"); fprintf(fp,"Eigen-vector = "); for (l = 1; l <= n; l++)

fprintf(fp," %f", v[l]); fprintf(fp,"\n"); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");l2: return 0; }/*******************************************************************/Order of the matrix = 3Number of iterations = 20Error tolerance = 1.000000e-04Elements of the matrix -15.000000 4.000000 3.000000 10.000000 -12.000000 6.000000 20.000000 -4.000000 2.000000Approx. eigen vector 1.000000 1.000000 1.000000Number of iterations = 19Approx. to Eigen value = 19.999981 20.000076 19.999981Eigen-vector = -1.000000 0.499998 1.000000/*******************************************************************/

PROGRAM 11

Page 19: Programs in C

/* PROGRAM : LAGRANGE METHOD Programme for Lagrange interpolation. */

#include <stdio.h>#include <math.h>

main() { float x[10], y[10], xin, yout, sum; int n, i, j; FILE *fp;

fp = fopen("result","w");

/* Read in data. */

printf("Input number of points : n\n"); scanf("%d", &n); fprintf(fp,"Number of points = %d\n", n); printf("Input the abscissas \n"); fprintf(fp,"The abscissas are :\n"); for (i = 1; i <= n; i++)

{ scanf("%f", &x[i]); fprintf(fp,"%8.4f ", x[i]);}

fprintf(fp,"\n"); printf("Input the ordinates\n"); fprintf(fp,"The ordinates are :\n"); for (i = 1; i <= n; i++)

{ scanf("%f", &y[i]); fprintf(fp,"%8.4f ", y[i]);}

fprintf(fp,"\n");

/* Read in x value for which y is desired. */

printf("Input value of x for which y is required\n"); scanf("%f", &xin); fprintf(fp,"The value of x for which y is required is "); fprintf(fp,"%5.3f\n", xin);/* Compute the value of y. */

yout = 0.0; for (i = 1; i <= n; i++)

{ sum = y[i]; for (j = 1; j <= n; j++) { if(i != j) sum = sum * (xin - x[j]) / (x[i] - x[j]); } yout = yout + sum;}

Page 20: Programs in C

fprintf(fp,"Lagrange interpolation based on %d points\n",n); fprintf(fp,"At x = %5.3f, y = %8.5f\n", xin, yout); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n"); fclose(fp); return 0; }/************************************************************************/Number of points = 6The abscissas are : 0.0000 1.0000 2.0000 4.0000 5.0000 6.0000The ordinates are : 1.0000 14.0000 15.0000 5.0000 6.0000 19.0000The value of x for which y is required is 3.000Lagrange interpolation based on 6 pointsAt x = 3.000, y = 10.00000/*******************************************************************/

PROGRAM 12

/* NEWTON-GREGORY INTERPOLATIONProgram for interpolation in a uniformly spaced table using Newton -Gregory formula. */

#include <stdio.h>#include <math.h>

main() { float y[10], d[10], xi, xf, x, h, fm, fj; float yout, fnum, fden, x0, y0, u, ffx, ffxx; int n, m, i, j, k; FILE *fp;

fp = fopen("result","w");

/* Read in starting value and last value of x, the step size and the y values. n gives the total number of nodal points.*/

printf("Input the number of abscissas,\n"); printf("starting value of x,\n"); printf("last value of x and\n"); printf("the step size\n"); scanf("%d %f %f %f", &n, &xi, &xf, &h); fprintf(fp,"The number of abscissas = %d\n", n);

Page 21: Programs in C

fprintf(fp,"The starting value of x = %f\n", xi); fprintf(fp,"The last value of x = %f\n", xf); fprintf(fp,"The step size = %f\n", h); printf("Input the ordinates\n"); fprintf(fp,"The ordinates are :\n"); for (i = 1; i <= n; i++)

{ scanf("%f", &y[i]); fprintf(fp,"%f ", y[i]);}

fprintf(fp,"\n");

/* Read in value of x for which y is desired and m the degree of the polynomial to be used. Maximum value of m is 15.*/

printf("Input x for which interpolation is required\n"); printf("and the degree of polynomial\n"); scanf("%f %d", &x, &m); fprintf(fp,"The value of x for which interpolation is "); fprintf(fp,"required is %f\n", x); fprintf(fp,"The degree of polynomial = %d\n", m); fm = m + 1; ffx = x - xi - fm * h / 2.0; ffxx = xf - x - fm * h / 2.0; if(ffx > 0.0) { if(ffxx <= 0.0)

j = n - m; else j = (x - xi) / h - fm / 2.0 + 2.0;

} else

j = 1; fj = j; x0 = xi + (fj - 1.0) * h; y0 = y[j];

/* Calculate required differences d[i] and y. */ for (i = 1; i <= m; i++)

{ d[i] = y[j+1] - y[j]; j = j + 1;}

for (j = 2; j <= m; j++){ for (i = j; i <= m; i++) { k = m - i + j; d[k] = d[k] - d[k-1]; }}

u = (x - x0) / h; yout = y0; fnum = u; fden = 1.0; for (i = 1; i <= m; i++)

Page 22: Programs in C

{ yout = yout + fnum/fden * d[i]; fnum = fnum * (u - i); fden = fden * (i + 1);}

fprintf(fp,"Newton-Gregory interpolation of degree %d\n", m); fprintf(fp,"At x = %7.5f, y = %7.5f\n", x, yout); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); fclose(fp); return 0; }/*****************************************************************************/The number of abscissas = 5The starting value of x = 0.100000The last value of x = 0.500000The step size = 0.100000The ordinates are :1.400000 1.560000 1.760000 2.000000 2.280000The value of x for which interpolation is required is 0.250000The degree of polynomial = 4Newton-Gregory interpolation of degree 4At x = 0.25000, y = 1.65500/*****************************************************************************/

PROGRAM 13

/* CUBIC SPLINE INTERPOLATIONProgram for cubic spline interpolation for arbitrary set of points. The second derivatives at the end points are assumed as zeros (natural spline). */

#include <stdio.h>#include <math.h>

main() { float x[20], y[20], sdr[20], a[20], b[20], c[20], r[20]; float t, xx, dxm, dxp, del, f; int n, i, j, nm1, nm2, k; FILE *fp;

fp = fopen("result","w");

/* Read n the number of points, x and y values. */ printf("Input number of points\n");

Page 23: Programs in C

scanf("%d", &n); fprintf(fp,"Number of points = %d\n", n); printf("Input abscissas\n"); fprintf(fp,"The abscissas are :\n"); for (i = 1; i <= n; i++)

{ scanf("%f", &x[i]); fprintf(fp,"%f ", x[i]);}

fprintf(fp,"\n"); printf("Input ordinates\n"); fprintf(fp,"The ordinates are :\n"); for (i = 1; i <= n; i++)

{ scanf("%f", &y[i]); fprintf(fp,"%f ", y[i]);}

fprintf(fp,"\n");

/* Read the value of x for which y is required. */ printf("Input x for which interpolation is required\n"); scanf("%f", &xx); fprintf(fp,"The value of x for which interpolation "); fprintf(fp,"is required is %f\n", xx);

/* Calculate second order derivatives needed in cubic spline interpolation. a, b and c are the three diagonals of the tridiagonal system. r is the right hand side. */

nm2 = n - 2; nm1 = n - 1; sdr[1] = 0.0; sdr[n] = 0.0; c[1] = x[2] - x[1]; for (i = 2; i <= nm1; i++)

{ c[i] = x[i+1] - x[i]; a[i] = c[i-1]; b[i] = 2.0 * (a[i] + c[i]); r[i] = 6.0*((y[i+1]-y[i])/c[i]-(y[i]-y[i-1])/c[i-1]);}

/* Solve the tridiagonal system. */ for (i = 3; i <= nm1; i++)

{ t = a[i] / b[i-1]; b[i] = b[i] - t * c[i-1]; r[i] = r[i] - t * r[i-1];}

sdr[nm1] = r[nm1] / b[nm1]; for (i = 2; i <= nm2; i++)

{ k = n - i; sdr[k] = (r[k] - c[k] * sdr[ k + 1]) / b[k];}

Page 24: Programs in C

/* Calculate the corresponding value of y. Find the proper interval. */ for (i = 1; i <= nm1; i++)

{ j = i; if(xx <= x[ i + 1]) goto l1;}

l1: dxm = xx - x[ j ]; dxp = x[ j + 1] - xx; del = x[ j + 1] - x[ j]; f = sdr[ j ] * dxp * (dxp * dxp / del - del)/6.0; f = f + sdr[ j + 1] * dxm * (dxm * dxm / del - del) / 6.0; f = f + y[ j ] * dxp / del + y[ j + 1] * dxm / del; fprintf(fp,"At x = %6.4f, interpolated value using ", xx); fprintf(fp,"%d points is y = %8.4f\n", n, f); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); fclose (fp); return 0; }/********************************************************************************/Number of points = 5The abscissas are :0.000000 1.000000 2.000000 3.000000 4.000000The ordinates are :1.000000 2.000000 33.000000 244.000000 1025.000000The value of x for which interpolation is required is 1.750000At x = 1.7500, interpolated value using 5 points is y = 21.1819/*******************************************************************************/

Page 25: Programs in C

PROGRAM 14

/* TRAPEZOIDAL RULE OF INTEFRATIONProgram to evaluate the integral of f(x) between the limits a to b using Trapezoidal rule of integration based on n subintervals or n+1 nodal points. The values of a, b and n are to be read and the integrand is written as a function subprogram. The program is tested for f(x) = 1 / (1 + x). */

#include <stdio.h>#include <math.h>

float f();

main() { float a, b, h, sum, x, trap; int n, i, m; FILE *fp;

fp = fopen("result","w");

printf("Input limits a & b and no. of subintervals n\n"); scanf("%f %f %d", &a, &b, &n); fprintf(fp,"Limits are a = %f, b = %f\n", a, b); fprintf(fp,"Number of subintervals = %d\n", n); h = (b - a) / n; sum = 0.0; m = n - 1; for (i = 1; i <= m; i++)

{ x = a + i * h; sum = sum + f(x);}

trap = h * (f(a) + 2.0 * sum + f(b)) / 2.0; fprintf(fp,"Value of integral with %d ", n); fprintf(fp,"Subintervals = %14.6e\n", trap); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); return 0; }/**************************************************************/float f(x) float x; { float fun;

fun = 1.0 / (1.0 + x);return(fun);

}/***********************************************************************/Limits are a = 0.000000, b = 1.000000

Page 26: Programs in C

Number of subintervals = 8Value of integral with 8 subintervals = 6.941218e-01/**********************************************************************/

PROGRAM 15

/* SIMPSON RULE OF INTEGRATIONProgram to evaluate the integral of f(x) between the limits a to b using Simpsons rule of integration based on 2n subintervals or 2n+1 nodal points. The values of a, b and n are to be read and the integrand is written as a function subprogram. The program is tested for f(x) = 1 / (1 + x). */

#include <stdio.h>#include <math.h>

float f();

main() { float a, b, h, x, sum, sum1, sum2, simp; int n, i, n1, n2; FILE *fp;

fp = fopen("result","w");

printf("Input limits a & b and half the no. of "); printf("subintervals n\n"); scanf("%f %f %d", &a, &b, &n); fprintf(fp,"The limits are a = %f, b = %f\n", a, b); h = (b - a) / (2.0 * n); sum = f(a) + f(b); sum1 = 0.0; n1 = 2 * n - 1; for (i = 1; i <= n1; i = i+2)

{ x = a + i * h; sum1 = sum1 + f(x);}

n2 = 2 * n - 2; sum2 = 0.0; for (i = 2; i <= n2; i = i+2)

{ x = a + i * h; sum2 = sum2 + f(x);}

simp = h * (sum + 4.0 * sum1 + 2.0 * sum2) / 3.0; fprintf(fp,"Value of integral with ");

Page 27: Programs in C

fprintf(fp,"%d Subintervals = %14.6e\n", 2 * n, simp); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); return 0; }/*******************************************************************************/float f(x) float x; { float fun;

fun = 1.0 / (1.0 + x);return(fun);

}/******************************************************************************/The limits are a = 0.000000, b = 1.000000Value of integral with 8 subintervals = 6.931545e-01/******************************************************************************/

PROGRAM 16

/* ROMBERG INTEGRATIONProgram to evaluate the integral of f(x) between the limits a and b using Romberg integration based on Trapezoidal rule. Values of a, b and desired accuracy are to be read and the integrand is written as a function subprogram. Array r gives Romberg table. n gives number of extrapolations. The program is tested for f(x) = 1 / (1 + x). */

#include <stdio.h>#include <math.h>

float f();

main() { float r[15][15], a, b, h, jj, kk, x, diff, eps; int n, i, j, k, m, l, ii; int x1, x2; FILE *fp;

fp = fopen("result","w");

printf("Input limits a & b, \n"); printf("the maximum no. of extrapolations n and\n"); printf("the error tolerance eps\n");

Page 28: Programs in C

scanf("%f %f %d %E", &a, &b, &n, &eps); fprintf(fp,"The limits are : a = %f, b = %f\n", a, b); fprintf(fp,"The maximum number of extrapolations = %d\n", n); fprintf(fp,"The error tolerance = %11.4e\n", eps); i = 1; h = b - a; r[1][1] = 0.5 * h * (f(a) + f(b)); for (ii = 1; ii <= n; ii++)

{ h = h/2.0; x2 = 1;

for (x1 = 1; x1 <= (ii - 1); x1++) x2 = x2 * 2; j = x2;

i = i + 1; r[i][1] = 0.5 * r[I - 1][1]; for (k = 1; k <= j; k++) { x = a + (2.0 * k - 1.0) * h; r[i][1] = r[i][1] + h * f(x); } for (k = 2; k <= i; k++) {

x2 = 1; for (x1 = 1; x1 <= (k - 1); x1++) x2 = x2 * 4; jj = x2 * r[i][k - 1] - r[i – 1][k - 1]; kk = x2 - 1; r[i][k] = jj/kk;

} diff = fabs(r[i][i] - r[i][i - 1]); if(diff <= eps) { fprintf(fp,"Romberg table after %d ", i - 1);

fprintf(fp,"extrapolations\n");for (l = 1; l <= i; l++){ for (m = 1; m <= l; m++) fprintf(fp,"%10.6f ", r[l][m]); fprintf(fp,"\n");}goto l2;

}}

printf("Number of extrapolations are not sufficient\n"); fprintf(fp,"Number of extrapolations are not sufficient\n"); goto l1;l2: printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");l1: return 0; }/********************************************************************************/float f(x) float x;

Page 29: Programs in C

{ float fun;fun = 1.0 / (1.0 + x);return(fun);

}/********************************************************************************/The limits are : a = 0.000000, b = 1.000000The maximum number of extrapolations = 5The error tolerance = 1.0000e-06ROMBERG TABLE AFTER 3 EXTRAPOLATIONS 0.750000 0.708333 0.694444 0.697024 0.693254 0.693175 0.694122 0.693155 0.693148 0.693147/********************************************************************************/

PROGRAM 17

/* EULER METHOD FOR SOLVING FIRST ORDER INITIAL VALUE PROBLEMProgram to solve an IVP, dy/dx = f(x,y), y(x0) = y0, using Euler method. The initial values x0, y0, the final value xf and the step size are to be read. f(x,y) is written as a function subprogram. */#include <stdio.h>#include <math.h>

float f();

main() { float x0, y0, h, xf, x, y; int i, iter; FILE *fp;

fp = fopen("result","w");

printf("Input initial point x0, initial value y0,\n"); printf("step size h and final value xf\n"); scanf("%f %f %f %f", &x0, &y0, &h, &xf); fprintf(fp,"Initial point x0 = %f, initial ", x0); fprintf(fp,"value y0 = %f\n", y0); fprintf(fp,"Step size = %f\n", h); fprintf(fp,"Final value = %f\n", xf); iter = (xf - x0) / h + 1; for (i = 1; i <= iter; i++)

{ y = y0 + h * f(x0,y0); x = x0 + h;

Page 30: Programs in C

if(x < xf) { x0 = x; y0 = y; }}

fprintf(fp,"At x = %6.4f, y = %12.6e\n",x, y); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); fclose(fp); return 0; }/********************************************************************************/float f(x, y) float x, y; { float fun;

fun = -2.0 * x * y * y;return(fun);

}/********************************************************************************/

Initial point x0 = 0.000000, initial value y0 = 1.000000Step size = 0.100000Final value = 1.000000At x = 1.0000, y = 5.036419e-01/********************************************************************************/

PROGRAM 18

/* RUNGE-KUTTA CLASSICAL FOURTH ORDER METHODProgram to solve the IVP, dy/dx = f(x,y), y(x0) = y0 using the classical Runge-Kutta fourth order method with steps h and h/2 and also computes the estimate of the truncation error. Input parameters are: initial point, initial value, number of intervals and the step length h. Solutions with h, h/2 and the estimate of truncation error are available as output. The right hand side f(x,y) is computed as a function subprogram.

*/

#include <stdio.h>#include <math.h>

float f();

main()

Page 31: Programs in C

{ float u[20], v[40], x0, y0, h, k1, k2, k3, k4; float h1, v1, te, x1, u1; int n, i, m, nn, ij; FILE *fp;

fp = fopen("result","w");

printf("Input initial point x0, initial value y0,\n"); printf("number of intervals n and step size h\n"); scanf("%f %f %d %f", &x0, &y0, &n, &h); fprintf(fp,"Initial point x0 = %f, initial ", x0); fprintf(fp,"value y0 = %f\n", y0); fprintf(fp,"Number of intervals = %d,\n", n); x1 = x0; for (m = 1; m <= 2; m++)

{ if(m == 1) { nn = n; u[0] = y0; } else { nn = 2 * n;

h = h / 2.0;v[0] = y0;

} for (i = 1; i <= nn; i++) { if(m == 1)

{ u1 = u[i-1]; h1 = h / 2.0; k1 = h * f(x0, u1); k2 = h * f(x0 + h1, u1 + 0.5 * k1); k3 = h * f(x0 + h1, u1 + 0.5 * k2); k4 = h * f(x0 + h, u1 + k3); u[i] = u1 + (k1 + 2.0 * (k2 + k3) + k4)/6.0; x0 = x0 + h; }

else { v1 = v[i-1]; h1 = h / 2.0; k1 = h * f(x1, v1); k2 = h * f(x1 + h1, v1 + 0.5 * k1); k3 = h * f(x1 + h1, v1 + 0.5 * k2); k4 = h * f(x1 + h, v1 + k3); v[i] = v1 + (k1 + 2.0 * (k2 + k3) + k4)/6.0; x1 = x1 + h; }

}}

te = 16.0 * (v[nn] - u[n]) / 15.0; fprintf(fp,"Step = %4.2f\n", 2.0*h); fprintf(fp,"Solution at nodal points\n"); for (i = 1; i <= n; i++)

Page 32: Programs in C

fprintf(fp,"%11.7f ", u[i]); fprintf(fp,"\n"); fprintf(fp,"Step = %4.2f\n", h); fprintf(fp,"Solution at nodal points\n"); for (i = 1; i <= 2 * n; i++) {

if(i == n + 1)fprintf(fp,"\n");

fprintf(fp,"%11.7f", v[i]); } fprintf(fp,"\n"); fprintf(fp,"Estimate of truncation error at "); fprintf(fp,"xf = %12.5e\n", te); printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); return 0; }/********************************************************************************/float f(x, y) float x, y; { float fun;

fun = - 2.0 * x * y * y;return(fun);

}/********************************************************************************/Initial point x0 = 0.000000, initial value y0 = 1.000000Number of intervals = 5,Step = 0.10Solution at nodal points 0.9900990 0.9615382 0.9174306 0.8620682 0.7999992Step = 0.05Solution at nodal points 0.9975063 0.9900990 0.9779951 0.9615384 0.9411764 0.9174311 0.8908685 0.8620689 0.8316008 0.8000000Estimate of truncation error at xf = 7.62939e-07/********************************************************************************/

PROGRAM 19

/* MILNE'S METHOD FOR SOLVING FIRST ORDER IVPProgram to solve an IVP, dy/dx = f(x,y), y(x0) = y0, using Milne-Simpson method. The initial values x0, y0, the final value xf and the step size h are to be read. Starting values are calculated using classical fourth order Runge-Kutta method. Adams-

Page 33: Programs in C

Bashforth method of third order is used as a predictor and Milne-Simpson method is iterated tillabs(yold - ynew) <= err where err is error tolerance. */

#include <stdio.h>#include <math.h>float f();

main() { float x[21], y[21], k1, k2, k3, k4, x0, y0; float h, f0, f1, f2, f3, x1, y1, p, yold, eps; int n, i, miter, iter, niter, m; FILE *fp;

fp = fopen("result","w");

printf("Input initial point x0, initial value y0\n"); printf("number of steps m, step size h,\n"); printf("error tolerance eps\n"); scanf("%f %f %d %f %E", &x0, &y0, &m, &h, &eps); fprintf(fp,"Initial point = %f\n", x0); fprintf(fp,"Initial value = %f\n", y0); fprintf(fp, "Error tolerance = %e\n", eps); printf("Input maximum number of iterations per step\n"); scanf("%d", &niter); fprintf(fp,"Maximum number of Milne iterations = "); fprintf(fp,"%d\n", niter); x[0] = x0; y[0] = y0; for (i = 1; i <= 2; i++)

{ x1 = x[i - 1]; y1 = y[i - 1]; k1 = h * f(x1 , y1); k2 = h * f(x1 + 0.5 * h , y1 + 0.5 * k1); k3 = h * f(x1 + 0.5 * h , y1 + 0.5 * k2); k4 = h * f(x1 + h, y1 + k3); y[i] = y1 + (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0; x[i] = x1 + h;}

miter = 0; for (i = 3; i <= m; i++)

{ iter = 0; x1 = x[i - 1]; y1 = y[i - 1]; f0 = f(x[i - 3] , y[i - 3]); f1 = f(x[i - 2] , y[i - 2]); f2 = f(x1 , y1); y[i] = y1 + h * (23.0 * f2 - 16.0 * f1 + 5.0 * f0) / 12.0; x[i] = x1 + h; p = y[i - 2] + h * (4.0 * f2 + f1) / 3.0;

l2: yold = y[i];

Page 34: Programs in C

iter = iter + 1; miter = miter + 1; f3 = f(x[i], yold); y[i] = p + h * f3 / 3.0; if((fabs(yold - y[i]) - eps) <= 0) goto l3; if (iter > niter) { printf("Iteration bound is not sufficient"); fprintf(fp,"Iteration bound is not sufficient"); goto l1; } goto l2;

l3: printf(" ");}

fprintf(fp,"Step = %6.4f\n", h); fprintf(fp,"Total number of Milne correctors used = "); fprintf(fp,"%d\n", miter); fprintf(fp,"Solution at nodal points\n"); for (i = 1; i <= m; i++) {

fprintf(fp,"%11.7f", y[i]);if (i == 5) fprintf(fp,"\n");

} printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");l1: fclose(fp); return 0; }/********************************************************************************/float f(x, y) float x, y; { float fun;

fun = - 2.0 *x *y *y;return(fun);

}/********************************************************************************/Initial point = 0.000000Initial value = 1.000000Error tolerance = 1.000000e-06Maximum number of Milne iterations = 5Step = 0.1000Total number of Milne correctors used = 28Solution at nodal points 0.9900990 0.9615382 0.9174208 0.8620606 0.7999858 0.7352871 0.6711303 0.6097542 0.5524795 0.5000020

Page 35: Programs in C

/********************************************************************************/

PROGRAM 20

/* SHOOTING METHOD FOR SOLVING SECOND ORDER LINEAR BVPProgram to solve the linear two point boundary value problemu" = p[x](du/dx) + q[x]u + r[x] = G(x, u, du/dx), u[a] = s1, u[b] = s2, by shooting method using the super-position principle. The inital value problem is solved by the fourth order Runge-Kutta method for 2x2 system. It requires two approximations of the slope of the solution curve at the starting point of integration. The linear function G is given as a function subprogram. */

#include <stdio.h>#include <math.h>

float f();float g();

main() { float u[50], v[50], w[50], k[3][5], h, a, b, ya, yb, va; float x0, x1, x2, u1, v1, c1, c2, app1, app2; int n, i, j, ij; FILE *fp;

fp = fopen("result","w");

printf("Input end points of interval of integration "); printf("a & b,\nvalues at boundary points ya & yb,\n"); printf("two approximations to the slope app1 & app2,\n"); printf("number of intervals n\n"); scanf("%f %f %f %f %f %f", &a, &b, &ya, &yb, &app1, &app2); scanf("%d", &n); fprintf(fp,"End points are a = %4.2f, b = %4.2f\n", a, b); fprintf(fp,"Values at boundary points are ya = %4.2f", ya); fprintf(fp,", yb = %4.2f\n", yb); fprintf(fp,"Two approximations to the slope are:\n"); fprintf(fp,"app1 = %f, app2 = %f\n", app1, app2); fprintf(fp,"Number of intervals = %d\n", n); h = (b - a) / n; u[0] = ya; v[0] = app1; x0 = a; for (j = 1; j <= n; j++)

{ x1 = x0 + h / 2.0;

Page 36: Programs in C

x2 = x0 + h; u1 = u[j-1]; v1 = v[j-1]; for (i = 1; i <= 2; i++) k[i][1] = h * f(i, x0, u1, v1); for (i = 1; i <= 2; i++) k[i][2] = h * f(i, x1, u1 + 0.5 * k[1][1], v1 + 0.5 * k[2]

[1]); for (i = 1; i <= 2; i++) k[i][3] = h * f(i, x1, u1 + 0.5 * k[1][2], v1 + 0.5 * k[2]

[2]); for (i = 1; i <= 2; i++) k[i][4] = h * f(i, x2, u1 + k[1][3], v1 + k[2][3]); u[j] = u1 + (k[1][1] + 2.0 * (k[1][2] + k[1][3]) + k[1]

[4]) / 6.0; v[j] = v1 + (k[2][1] + 2.0 * (k[2][2] + k[2][3]) + k[2]

[4]) / 6.0; x0 = x0 + h;}

w[0] = ya; v[0] = app2; x0 = a; for (j = 1; j <= n; j++)

{ x1 = x0 + h / 2.0; x2 = x0 + h; u1 = w[j - 1]; v1 = v[j - 1]; for (i = 1; i <= 2; i++) k[i][1] = h * f(i, x0, u1, v1); for (i = 1; i <= 2; i++) k[i][2] = h * f(i, x1, u1 + 0.5 * k[1][1], v1 + 0.5 * k[2]

[1]); for (i = 1; i <= 2; i++) k[i][3] = h * f(i, x1, u1 + 0.5 * k[1][2], v1 + 0.5 * k[2]

[2]); for (i = 1; i <= 2; i++) k[i][4] = h * f(i, x2, u1 + k[1][3], v1 + k[2][3]); w[j] = u1 + (k[1][1] + 2.0 * (k[1][2] + k[1][3]) + k[1]

[4]) / 6.0; v[j] = v1 + (k[2][1] + 2.0 * (k[2][2] + k[2][3]) + k[2]

[4]) / 6.0; x0 = x0 + h;}

c2 = (yb - u[n])/(w[n] - u[n]); c1 = 1.0 - c2; for (i = 1; i <= n; i++)

u[i] = c1 * u[i] + c2 * w[i]; fprintf(fp,"Step h = %6.2f\n", h); fprintf(fp,"Solution at nodal points\n"); for (i = 1; i <= n - 1; i++)

fprintf(fp,"%12.5e ", u[i]); fprintf(fp,"\n");

Page 37: Programs in C

printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n"); return 0; }/********************************************************************************/float f(i, x, z1, z2) float x, z1, z2; int i; { float fun;

if(i == 1) fun = z2;else fun = g(x, z1, z2);return(fun);

}/********************************************************************************/float g(xx, zz1, zz2) float xx, zz1, zz2; { float fung;

fung = zz1 + xx; return(fung); }/********************************************************************************/End points are a = 0.00, b = 1.00Values at boundary points are ya = 0.00, yb = 0.00Two approximations to the slope are:app1 = 0.100000, app2 = 0.200000Number of intervals = 5Step h = 0.20Solution at nodal points-2.86791e-02 -5.04826e-02 -5.82589e-02 -4.42937e-02/********************************************************************************/