Detailed Solutions  A-07        JUNE 2003

 

Q1.     a.         C        

 

b.             B

 

c.              C          It is a property of Gauss formulas.

 

d.             A                         

 

e.              A           Use divided difference interpolation.        

 

f.               A           If

 

g                          k = 4 + (4 > 1) = 4 + 1 = 5.

 

h        D            

 

PART I

 

Q2    

            (i)

            (ii)

            Substituting   in the first formula, we get

                     

                                      

                            

                Hence,                                                                                                    (1)

                or            .                                                                                                                (2)

               

Substituting   in the second formula, we get

               

 

Hence,                                                                                                (3)

or                                                                                                                                (4)

               

Error in the second formula given by ( 4 ) is 3 times the error in the first formula given by ( 2 ). If, we multiply the first formula by 3 and add to the second formula, then from ( 1 ) and (3 )

                               

                Then, the order of the new formula is 3. The new formula is

                .

 

Q 3a        

 

                Comparing the elements, we get

               

               

Hence,

 

Q 3b   #include <stdio.h>

#include <math.h>

#include <conio.h>

void main()

{

clrscr();

float    a[10][10], b[10], x[10], oldx[10], sum, big, c;

float    eps;

int      n, niter, i, j, ii, jj, k, l;

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);

printf("n = %d, niter = %d, eps = %e\n", n, niter, eps);

printf("Input augmented matrix row-wise\n");

printf("Elements of the augmented matrix\n");

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

{

                for (j = 1; j <= n; j++)

                {

                                scanf("%f", &a[i][j]);

                                printf("%f  ", a[i][j]);

                }

                scanf("%f", &b[i]);

                printf("      %f\n", b[i]);

}

printf("Input initial approx. to the solution vector\n");

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

{

                scanf("%f", &oldx[i]);

                printf("%f   ", oldx[i]);

}

printf("\n");

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

x[i] = oldx[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] * oldx[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)

                big = c;

}

if(big <= eps)

                goto l10;

for (l = 1;  l <= n; l++)

                oldx[l] = x[l];

}

printf("ITERATIONS NOT SUFFICIENT\n");

printf("Solution vector at this stage\n");

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

                printf("  %f ", x[i]);

printf("\n");

goto l20;

l10:          printf("Number of iterations = %d\n", ii);

printf("Solution vector\n");

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

                printf("  %f ", x[i]);

printf("\n");

l20:

}

 

Q 4a       Let the uniform mesh be defined as  where  Integrate the given differential equation in interval  to get

               

 

                We note that f (x, y) is the slope of the solution curve and it changes continuously. Approximate the changing slope in  by the fixed slope  at x =. Then, we get the     approximation

               

                where . This method is called the Euler method.

 

                We have,  and

               

 

Q 4b       Gauss-Jordan method gives

               

                 

 

 

                    .  

 

Q 4c       Let the linear polynomial be We have . .  Normal equations for fitting the required linear polynomial are (with w(x)=1):

                               

                This gives . Hence we get . Hence the required linear polynomial is  .

 

Q 5         Power method : Let  be the distinct eigen values such  and

 be the corresponding eigenvectors. Then, any vector  in this vector space of eigenvectors can be written as

 

                                 

                 Then,

                           

 

            Pre-multiplying by A, k-1 times and k times, we get

                                                                  (1)

                                                         (2)

 

            Now, . Hence, as , the right hand sides of  (1) and (2) tend to

                 and . Therefore, all the ratios of the components of the left hand sides of (1)

                and (2) tend to , the largest eigen value in magnitude

 

                                          

 

                The iteration is stopped when the magnitudes of the differences of the ratios are £ given error tolerance. The algorithm is given by

               

                               

                 is the corresponding eigen vector. We have the following results.

       

                The ratios of the components of are [25.5908, 25.0994, 24.9226]T. The three ratios tend to the same eigen value 25 which gives . The corresponding eigen vector is .

 

Q 6a       The bound for the error in quadratic interpolation is given by

                               

                where

                Since the data is equispaced, assume without any loss of generality that . Then,

                               

                Let  Setting , we get the stationary points as . The required maximum is

                               

            Therefore,

                We have and   

                Choose h such that

Q 6b       #include <stdio.h>

# include<conio.h>

float power (float m, int n);

long int fact (int m);

void main ( ) {

                                clrscr();

                                int i, a;

                                float val, temp, x;

                                printf ("Input the value of x ");

                                scanf("%f", &x);

                                fflush(stdin);

                                temp = power(x,2);

                                val = 1- (temp/2 );

                                a = 2;

                                for (i=3; i<=25; i++) {

                                                temp = (float) fact(a*(i-1));

                                                val += power (x, (a*(i-1))) / temp;

                                }

                                printf ("The sum of series is %f ", val );

}

float power (float base, int n) {

int i;

float p;

                                p = 1;

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

                                                p = p*base;

                                }

                                return p;

}

long int fact (int n) {

                                int i;

                                long int p = 1;

                                if (n > 1)

                                                for (i=2; i<=n; ++i)

                                                                p = p*i;

                                return p ;

}

       

PART II

Q 7a       #include <stdio.h>

                #include <conio.h>

                float T(int,float);

                void main() {

                                clrscr();

                                int n;

                                float x,Tn1;

                                printf("n = ");

                                scanf("%d", &n);

                                printf("x = ");

                                scanf("%f", &x);

                                Tn1 = T(n,x);

                                printf("The value of Chebyshev Polynomial = %f\n", Tn1);

                }

                float T(int a, float x1) {

                                int m, k;

                                float Tn;

                                if (a == 0) {

                                                return( 1.0);

                                }

                                else if (a == 1) {

                                                return(x1);

                                }

                                else {

                                                m = a - 1;

                                                k = a - 2;

                                                return( 2.0 * x1 * T(m, x1) - T(k, x1));

                                }

                }

 

 

Q 7b           We have the following divided difference table.

               

                Newton’s divided difference interpolation:

               

                Hence,

 

Q 8a       Bairstow’s  formula

                ,

               

          . 

 

               

               

       

                                              _____________________

                                               

                                                _____________________

 

                We have

                               

 

Q 8b       Classical  Runge-Kutta fourth order method.

 

               

 

                We have            

               

 

Q 9a       Gauss-Legendre two point formula

                               

                is transformed to  by the transformation

 

                Transform the interval [5, 12] to [-1, 1]. Use the transformation

                We have  and

                 

 

Q 9b                       #include <stdio.h>

                #include <conio.h>

                void main () {

                                clrscr();

                                FILE * fp;

                                char c;

                                int nw=0,ns=0;

                                fp = fopen ("sample.txt", "r+" );

                                if (fp == NULL)

                                                printf("Cannot open file.\n");

                                else {

                                                while (( c=fgetc(fp)) != EOF) {

                                                if ( c == ' ' || c == '\n' || c == '\t' )

                                                                nw++ ;

                                                if (  c == '.' )

                                                                ns++;

                                                }

                                                printf ("The total number of words and sentences" );

                                                printf (" in the file are % d and % d respectively", nw, ns );

                                }

                                fclose( fp );

                }

 

Q 10a    

                .

                Let the result be true for that is

                               

                Then,     

                                                                   =

                                                                   =

                Hence,   

Q 10 b    Unions contain members whose individual data types may differ from one another. However, the members that compose a union all share the same storage area within the computer’s memory, whereas each member within a structure is assigned its own unique storage area. Thus, unions are used to conserve memory. They are useful for applications involving multiple members, where values need not be assigned to all of the members at any one time.

                union  id {

                                char color[12]

                                int size;

                } shirt, blouse;

                Here we have two union variables, shirt and blouse, of type id. Each variable can represent either a 12-character string (color) or an integer quantity (size) at any one time.

                The 12-character string will require more storage area within the computer’s memory than the integer quantity. Therefore, a block of memory large enough for the 12-character string will be allocated to each union variable. The compiler will automatically distinguish between the 12-character array and the integer quantity within the given block of memory, as required.

A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities. Examples:

(i)  The payroll record: an employee is described by a set of attributes such as name, address, social security number, salary etc.

(ii) A point is a pair of co-ordinates, a rectangle is a pair of points and so on.

 

 

Q 11a     We have the exact solution as   Set the error as  Substituting in the given equation, we get

                               

                                               

 

We have two unknowns p and q. Setting  we get Hence, for

the method is of order 2, since   

                 .

The error constant is given by  .

 

Q 11b     Gauss-Seidel method:      

where is the iteration matrix and  The method converges if the spectral radius of is less than 1. We have

                   

                Eigen values of 

                gives 

                We have spectral radius of  Hence, the method converges.

 

               

 

Detailed Solutions A-07      DECEMBER 2003

 

 

Q1.     a.         D         0/0 form. Use Hospital’s rule three times.

.

b.             B         It is a property of Lagrange fundamental polynomials.

 

c.              A         Error equation of Regula-falsi method is .

 

d.             B                             

 

e.              B

 

f.               B

 

g.        C         It is the Gauss-Legendre 3 point formula.

 

h.        D             Expand by Taylor series. The first non-vanishing term is .

 

PART I

 

Q 2a      

           

By forward substitution ofwe get

By backward substitution of we get  .

 

Q 2b     Set  Substitute in the Newton-Raphson method and expand the terms in Taylor series     to obtain

                               

                                         

 

                where  Hence,   The order of the method is s = 2.

               

 

Q 3a     We shall use Gauss elimination method and reduce the augmented matrix [A | b] to [U | z ] where

                U is an upper triangular matrix.

               

               

               

The system has a solution if  Then, the given system has a one parameter family of solutions.

 

Q 3b        Let . New ton’s method :

                 

                 

                  

                   Next approximation is .

 

                  We have,   .

 

                  We are given that  We obtain

                                    

             

                   The solution is . Hence, .

 

Q 4a       (i) Value assigned to &a is 1130. Value assigned to &b is 1134. Value assigned to &c is 1138.

               

                (ii) Value of *pa = 0.002. Value of &(*pa) is 1130.

 

Q 4b       Use New ton’s divided difference interpolation. Rearrange the data in ascending order of .

 

                      x           f(x)             first d.d                 second d.d         third d.d

                0              3                                                        

                     1              3                     0

                3/2          13/4                 1/2                           1/3

                     2              5/3              -19/6                       -11/3                     -2

             

                  New ton’s divided difference formula

                 

                 

 

Q 5a       a = 100, b = 200

                count = 1

                c = 20 * (1 – 1) = 0

                d = 4 * 1 * 1 = 4

                funct1(a, c) = 100

                funct1(b, d) = 196

                count = 2

                c = 20 * (2 – 1) = 20

                d = 4 * 2 * 2 = 16

                funct1(a, c) = 80

                funct1(b, d) = 184

                count = 3

                c = 20 * (3 – 1) = 40

                d = 4 * 3 * 3 = 36

                funct1(a, c) = 60

                funct1(b, d) = 164

                count = 4

                c = 20 * (4 – 1) = 60

                d = 4 * 4 * 4 = 64

                funct1(a, c) = 40

                funct1(b, d) = 136

                count = 5

                c = 20 * (5 – 1) = 80

                d = 4 * 5 * 5 = 100

                funct1(a, c) = 20

                funct1(b, d) = 100

  

Q 5b      Substitute  Then, the given formula becomes

                   

            Make the formula exact for                      

                   

              

         Solving , we get

            For the equation is satisfied. For we get  

                         

         Error term =

 

Q 6a     We generate the data as

                   

                 Least squares approximation : minimum .

                Conditions of extremum gives

               

               

These are the normal equations. We have N =5,    

                   

                Normal equations are 

                     

                 The solution is  The least squares approximation is

                                  .

                   

Q 6b       #include <stdio.h>

            #include <math.h>

            #include <conio.h>

            #define d    b * b - 4 * a * c

            #define root if (d > 0) {           

                                                printf("The two real roots are ");                      

                                                printf("%f %f", (-b+sqrt(d))/(2.0*a), (-b-sqrt(d))/(2.0*a));        

                                }                                             

                                else if (d == 0) {   

                                                printf("The repeated roots are ");    

                                                printf("%f %f", -b/(2.0*a), -b/(2.0*a));          

                                }                                             

                                else {                     

                                                printf("The complex pair is ");          

                                                printf("%.2f+%.2fi    ", -b/(2.0*a), sqrt(abs(d))/(2.0*a));                              

                                                printf("and    %.2f-%.2fi", -b/(2.0*a), -sqrt(abs(d))/(2.0*a));                     

                                }

 

            void main() {

                clrscr();

                int a, b, c;

                printf("Enter the value of a, b, c : ");

                scanf("%d %d %d", &a, &b, &c);

                root;

            }

PART II

 

Q 7a       Partition method :

                 

               

                 where

                Let the partition of the given matrix be

   

Q 7b       Power method :

                 

               

                 All  ratios tend to the largest eigen value in magnitude.

                 

               

                 After two iterations, the ratios for  are 95/4, 5, 70/9. The ratios have not yet converged.

 

Q 8a       Jacobi method : Let  be the largest off-diagonal element in magnitude. Choose θ such that

                 

                    or      .

 

If , then , if ; and , if . Then, the elements  form the 2×2 rotation sub-matrix. Repeat the procedure until A is reduced to a diagonal matrix. Then, the eigen values are on the diagonal of this matrix. The product of the rotation matrices  gives the matrix of eigenvectors. The first column of S is the eigenvector corresponding to the eigen value in the first (1×1) location of the diagonal matrix, etc.

 

 First rotation : is the largest off-diagonal element.

 

      

Second rotation :  is the largest off-diagonal element.

 or  θ = 0.40741346 radians,  sin θ = 0.396236, cos θ = 0.918149.

 

 

Iterations have not converged since A is not reduced as yet as a diagonal matrix. The eigen values may be near 5.9, -1, 0.08. The columns of S are the corresponding eigenvectors.

 

Q 8b       #include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <conio.h>

char** swap(int, char *p1[]);

void main() {

                                clrscr();

                                char *p[10];

                                int i=0,n,j;

                                printf("Enter no of strings : ");

                                scanf("%d", &n);

                                for (i=0; i<n; i++) {

                                                p[i] = (char *) malloc(2*n);

                                                printf("Enter string : ");

                                                scanf("%s",p[i]);

                                                fflush(stdin);

                                }

                                char **q = swap (n, p);

                                printf("The ascending order of strings is :\n");

                                for (i=0; i<n; i++)

                                                printf("%d : %s\n",i+1, *(q+i));

}

char** swap(int n1, char *p1[]) {

                                char *temp;

                                int i,j;

                                for (i=0; i<n1; i++) {

                                                for (j=i+1; j<n1; j++) {

                                                                if (strcmp(*(p1+i), *(p1+j)) > 0) {

                                                                                temp = *(p1+i);

                                                                                *(p1+i) = *(p1+j);

                                                                                *(p1+j) = temp;

                                                                }

                                                }

                                }

                                return p1;

}

 

Q 9a       The bound on the error in linear interpolation is given by

               

                Now, the stationary point of   is  Hence,

| Error | ,

 where  and 

 We have and .

 We require    or    

 

Q 9b        (i) Make the formula exact for f ( x ) = 1, x, .

                   

                 The solution of this system is The formula is given by

                   

              (ii) Make the formula exact for f ( x ) = 1, x, .

                               

  

 

    The solution of this system is    The formula is given by

   

                               

                Both the methods are exact for .

For , the error constant for the first method is  and for the second method is . Hence the second method is better since it has a smaller error constant.

 

Q 10a     (i)

#include <stdio.h>

void main() {

                                int i, sum=0;

                                for (i=2; i<100; i+=3)

                                                if (i%5 == 0)

                                                                if ((i/5)%2 ==0)

                                                                                sum += i;

                                printf("The sum is %d", sum);

}

                 

Q 10a (ii)

#include <stdio.h>

void main() {

                                int i, sum=0;

                                for (i=2; i<100; i+=3)

                                                sum += (i%5 == 0) ? (((i/5)%2 == 0) ? i : 0) : 0;

                                printf("The sum is %d", sum);

}

 

           

Q 10b       Given system is and Gauss-Seidel method is

                    where, iteration matrix = H =  and

                      

                   

                    Eigen values of H : gives the characteristic equation  as

                                    

                    Hence,  and

                     Spectral radius of

         Hence, the iteration converges.

 

 

Q 11a         Runge-Kutta classical fourth order method              

                                    

                               

 

                     We have .

                     

                   

                     

 

Q 11b         The initial value problem ( I V P ) is  

                   Existence theorem : Let   be continuous and bounded at all points in some closed rectangular  region about  , that is, in  Then, the I V P has at least one solution.

Now,  is continuous in every rectangle about

Also,  for  and all y. Hence, the given I V P has at least one solution. Uniqueness theorem:  Let the I V P satisfy the existence theorem and let f (x, y ) satisfy the Lipschitz condition

                   

                                                 .

                     Then, the solution of the I V P is unique. A sufficient condition is should be bounded, that is,  in the considered domain.

                     Now, , and   for  and for all  y . Hence, the solution is unique.

 

                   

 

Detailed Solution     A-07             June 2004

 

Q1.     a.         C         Set 

b.             A          

c.              D         Romberg formula: We obtain the result as 0.5867.

d.             B        

e.              C          The solution is a = 0,

                        b = 5.  y = 5x.

f.               A          Iteration matrix = H =  Spectral radius of H =

g.             D

        

h.             B

   

PART I

 

Q 2a             Root lies in (-1, 0 )

                .

               

We get

 

Q 2b       #include <stdio.h>

                 #include <math.h>

                 main()

                 {

                                float xinitial, eps, fx, dfx, xnew;

                                int i, n;

                                float f(float s);

                                float df(float s);

                                printf("Input value initial approximation xinitial\n");

                                printf("n: number of iterations\n");

                                printf("eps: error tolerance\n");

                                scanf("%f %d %e", &xinitial, &n, &eps);

                                printf("xinitial = %f  n = %d  eps = %e\n\n", xinitial, n, eps);

                /* Calculate f and its first derivative at xinitial     */

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

                                {

                fx = f(xinitial);

                                                dfx = df(xinitial);                                                                                                                                                                   xnew = xinitial - fx / dfx;                                                                                                                                                                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.           */                                                                                                            xinitial = xnew;

                                }

                                printf("\nITERATIONS ARE NOT SUFFICIENT");                                                                                                       goto l20;                                                                                                                                                                               

l10:          printf("Iterations = %d", i);

                printf("  Root = %10.7f,  f(x) = %e\n", xnew, fx);

l20:          return 0;

                }

float f(float x)

{

float fun;

                                fun = 7.0*x*x*x+8*x*x+8.0*x+1;

return(fun);

}

float df(float x)

{

float dfun;

                                dfun = 21.0*x*x+16.0*x+8.0;

                                return(dfun);

}

 

 

Q 3a       Bairstow’s  formula

 

                ,

               

         

                               

                                                1              4.5               3                4                       

                                         -0.4       -1.64           -0.104

                                                            -1.1        -4.51

                                              1                4.1           0.26         -0.614

                                    -0.4       -1.48           

                                                     -1.1

                                                                                               

                                                1              3.7      -2.32

                                          _______________________________             

 

                                               

 

Q 3b      

               

 

 

Q 4a      

          .

            

              Iteration matrix =

            

              Eigen values  of H : .

               

                       

                Rate of convergence   =

 

Q 4b       The Power method is given as the following

               

                 is given as 

               

               

                Ratios of    and the corresponding eigen vector is v5.

 

Q 5a       Substitute   in the secant method to obtain

               

                         =

                        =

                        =

                        =

                where

                Hence, we have      

                 gives   , or  Hence,

                  Comparing the powers of  , we get

                , .   Since, , we get  = 1.62.

 

Q 5b                       # include <stdio.h>

                                void main ( )

                                {   

int years,method,i;

                                                float depreciation,value,original;

                                                printf  ( "Method used : ( 1 ) Straight line.. ( 2 ) Sum.." );

                                                scanf  ("%d", &method );

                                                printf  ( "Original value: " );

                                                scanf  ( " %f  ",  &original );

                                                printf  ( "No. of  years : " );

                                                scanf  ( " %d " , &years );

                                                switch (method)

                                                {

                                                case 1 :

printf  ( " \ n  Straight line method \ n " );

                                                                depreciation = original / years;

                                                                for ( i =1; i <= years; ++i )

                                                                {

                                                                original -= depreciation ;

                                                                                printf  ( "End of  year % 2d ", i );

                                                                                printf  ( " Depreciation : % f  ", depreciation );

                                                                                printf  ( " Current value : % f  \ n ", original );

                                                                }

                                                                break ;

                                                case 2 :

                                                                printf  ( " \ n Sum of year's digits method \  n " );

                                                                value = original ;

                                                                for ( i = 1 ; i <= years; ++i )

                                                                {

                                                                depreciation = ( years - i + 1 ) * original / ( years*(years+1)/2);

                                                                                value -=depreciation;

printf  ( " end of  year  % 2d ", i );

                                                                                printf  ( "depreciation : % f  ", depreciation );

                                                                                printf  ( " current value : % f  \ n ", value );

                                                                }

                                                                break ;

                                                }

                                }

 

 

Q 6a       Inverse power method:

               

                 is the required eigen vector .     .         

                .                                                  

 

½Ratios½:   4.791,  4.789,  4.791 .        

 

Q 6b      

                .              

                Sturm sequence: Define . Then

                 

                .

                .

                Form the Sturm table

                                 

                                -3        +          -          +          -            3

                                -2        +          -          -          +            2

                                -1        +          -          -          +            2

                                  0        +          -          -          +            2

                                  1        +          +          -          +            2

                                  2        +          +          -          0            eigenvalue

                                  3        +          +          -          -            1

                                  4        +          +          +         +             0

             From the Sturm table, we find that there are eigen values in ( - 3, -2 ), ( 3, 4 ) and 2 is an eigen         value.

                \½ Largest eigen value½ lies in ( 3, 4 ).

PART II

Q 7a       Error in quadratic interpolation is bounded by  

                We have and  = 960 for .

                    gives   

 

Q 7b       x             f (x)         f           f      f

               

                -1            1.2          

                  0            1.0           -0.2

                  1            3.8           2.8           3.0

                  2            9.6             5.8         3.0          0

                  3            18.4           8.8         3.0           0

                  4            30.2         11.8         3.0           0

               

                We have h = 1.0.

               

                          =                .

 

 

Q 8a       .

                We have   

               

                Romberg value = 

 

 

Q 8b         #include <stdio.h>

#include <math.h>

float f();

main()

{

                 float   a, b, h, sum, x, trap;

                int     n, i, m;

                printf("Input limits a & b and no. of subintervals n\n");

                scanf("%f %f %d", &a, &b, &n);

                 printf("Limits are a = %f, b = %f\n", a, b);

                 printf("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;

                printf("Value of integral with %d ", n);

                printf("Subintervals = %14.6e\n", trap);

                return 0;

}

float   f(float x)

{

float    fun;

                fun = 1.0 / (10.0 + 2.0 * x + x * x);

                return(fun);

}

 

Q 9a         minimum .   

                  Normal equations :           

                 We have N = 6,   

                Normal equations are 

                Solution is

 

Q 9b      

               

                .  

 

Q 10a     Choose three points as 

               

               

 

                The solution of this system is    

 

Q 10b     # include <stdio.h>

long  fibonacci ( int count );

                main ( )

                {

                       int count, n;

                        printf( " Number of fibonacci numbers : " );

                       scanf(" %d ",  &n);

                       printf  ( " \n" );

                       for ( count = 1 ; count <= n ; ++count )

                                printf  ( " \n i = %2d , F = %1d", count , fibonacci(count) );

                }

                long fibonacci ( int count )

                                /* F = 1 for i < 3 , and F = F1 + F2 for  i > = 3 */

                 {

                                static long int   f1 = 1, f2 =1 ;

                                 long int  f ;

                                 f = ( count < 3 ) ? 1 : f1 + f2 ;

                                 f2 = f1 ;

                                 f1 = f ;                                                                                                                                                   

                                 return f;

                }

 

Q 11a 

               

               

 

Q 11b         

               

Detailed Solutions  A-07     December 2004

 

Q1.     a.         B         Relative error =            

b.             A         Number of iterations required ³ [ log(b – a) – log e ]/ log2 = – log  e /log2.

c.              D         Iteration matrix = H =  Spectral radius of H =

d.             C        

e.              B          Least squares error =

f.               A          Use Taylor series expansions to obtain error as

g.             C         

 

h.             B          Upper case of ‘g’ is taken as ‘case G’.

 

PART I

 

 

Q 2a       Perform the operations

            . Now, perform the operations

            . Perform

                       

            .

Q 2b       .       We get   L =

 

   Set  .    Solve  then solve.

                        gives 

                 gives 

 

 

Q 3a       Set  Substitute in the Newton-Raphson method and expand the terms in Taylor series to obtain

                               

                                           

 

                where  Hence,   The order of the method is p = 2.

                               

   Q 3b    , where  Substituting  k = 0, 1, 2, . . ., we get

                 and   

Here,   and

                Find k such that   We get k = 3.

 

Q 4a       Root lies in .          .     .

We get

 

Q 4b       # include  <stdio.h>

# include  <math.h>

                float f(float);

                main ( )

                {

                    float a, b, x, eps, fa, fb, fx;

                    int   i, n;

                    printf ("Input two approximations to the root \ n");

                    scanf ("%f  %f ",&a, &b);

                    printf ("input m: number of iterations \ n");

                    scanf ("% d", &n);

                    printf ("eps: error tolerance \ n");

                    scanf ("%E", &eps);

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

                    {

fa = f(a);

                                fb = f(b);

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

                                fx = f(x);

                                if  (fabs (fx) <= eps)

                                                goto l0;

                                a = b;

                                b = x;

                     }

                     printf (" \ n Number of iterations given are not sufficient");

                     goto l5;

     l0:            printf ("Number of iterations = % d \ n", i);

                     printf ("Root  =% 10.7f, f(x) = % e \ n", x, fx);

                                                                                                                                                                                              l5:                           return 0;

                 }

                float f(float x)

                {

float fun;

                                fun = x*x;

                                return (fun);

                }

 

Q 5a      

 We get

                 

 

  Q 5b     Iteration matrix =     

                Eigen vaules :  gives                                                                                                                                                                                                                                 Hence,  or .

            Spectral radius of

                Rate of convergence =

 

Q 6a      

                 Newton’s method:

                .    

               

Next approximation is 

               

            Using the given initial approximation,  we get

               

The solution of the system is .

Hence, .

 

  Q 6b     # include <stdio.h>

#define  SIZE 100

main ( )

{

                                int i, n, f [SIZE];

                                void reordering (int n, int f [ ] );

                                printf (" \ n Total number of numbers to be entered");

                                scanf (" % d", & n);

                                printf (" \n");

                                for ( i = 0; i < n ; ++i )

                                {

                                                printf ( " i = % d, f = ", i +1);

                                                scanf (" % d", & f [ i ] );

                                }

                                reordering  (n, f );

                                printf ( " \ n \ n Numbers in ascending order : \ n \ n" );

                                for ( i = 0; i < n; ++i )

                                                printf ( " i = % d, f = % d \n ", i +1, f[ i ] );

                                                                                                                                                                                                                }

                                                                                                                                                                                                                void reordering (int n, int f [ ] )

                {

                                int i , item, temp;

                                for (item = 0; item < n - 1; ++item)

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

                                                                if ( f [ i ] < f [ item ] )

                                                                {

                                                                                temp = f [ item ];

                                                                                f [ item ] = f [ i ];

                                                                                f [ i ] = temp;

                                                                }

                                return;

                }

 

                                                                PART II

 

Q 7a       Data is not equispaced . Use the divided difference formula.

 

               

 

Form the divided difference table.

x              f(x)          first d.d       second d.d       third d.d      fourth d.d

0                    –2.5

1                    –0.5             2.0

2                    10.5            11.0               4.5   

5               187.5           59.0               12.0                  1.50

7               515.5           164.0             21.0                  1.50           0.0

10            1502.5         329.0             33.0                  1.50           0.0      

.

 

Q 7b     where            .   h = 0.1, .

              

*    .

*

Q 8a           minimum

                    For minimum, we have the necessary conditions

                   

               Normal equations are    ,    .

               We get

                     Normal equations are    ,   .

                    Solution is

 

 

Q 8b       # include  <stdio.h>

                # include  <math.h>

                main ( )

                {

                                float x[10], y[10], xin, yout, sum;

                                int  n, i, j;

                                printf ("Input number of points: n \ n");

                                scanf ( "%d" , & n);

                                printf ( "Input the abscissas \ n");

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

                                                scanf ( "% f ", & x[ i ] );

                                printf ( "Input the ordinates \ n");

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

                                                scanf ( "%f ",  & y[ i ] );

                                printf ( "Input the value x for which interpolation is required \ n" );

                                scanf ( "% f ",  & xin );

                                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;

                                }

                                printf ( " At x = % 5.3 f ,  y = % 8.5 f \ n" , xin, yout ) ;

                                return 0;

                }

 

Q 9a      

                           

                   

            ( i ) Comparing, we get  a + b + c = 0, 2a + b = - 1, 4a + b = 0.

                        The solution is a = 1/2, b = -2, c = 3/2.

                ( ii ) Error term =

                ( iii )  Formula is given by  

                            Round off error        

                                                .

 

 

 

Q 9b       .

                .

                Error : .     Richardson’s estimate =

                                                                                       =.

 

Q 10a         Make the formula exact for  i = 0, 1, 2.     

                    .

                    Solution is

                    Error term: Set

                    Error  = .

 

Q 10b     # include  <stdio.h>

                # include  <math.h>

                float f(float,float);

                main ( )

                {

                                float x0,y0,h,xf,x,y;

                                int  i, iter ;

                                FILE *fp ;

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

                                printf ( "Input initial  point  initial value , \ n");

                                printf ( "Step size h and final value xf \ n");

                                scanf ( "%f  %f  %f  %f ", &x0, &y0, &h, &xf );

                                fprintf ( fp , "x0  = %f ",  x0 );

                                fprintf ( fp , "y0  = %f,  h = %f ", y0, 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 ;

                                                if(x<xf)

                                                {

                                                                x0  = x ;

                                                                y0 = y ;

                                                }

                                }

                                fprintf ( fp , " At x = % 6.4 f, y =% 12.6 e \ n", x, y );

                                printf ( " \ n See FILE `result' for results \ n  \ n" );

                                fclose ( fp ) ;

                                return 0;

                }

                float f (float  x, float y)

                {

float fun ;

                                fun = x * x + y * y ;

                                return ( fun ) ;

                }

Q 11a    ( i ) Gauss-Legendre  2 –point formula :

                        ;                .

 

             ( ii ) Gauss-Chebyshev  2 –point formula :

                                           

               

 

 

 Q 11b    ( i ) Euler method :               

                 

                             

               

                 ( ii ) Taylor series method of order four :