Tuesday, February 01, 2011

Lin's Method


/* The following C program implements Lin’s method for determining the complex root of a polynomial equation, by extracting a quadratic factor of the form x2 + Px + Q from a polynomial equation. The polynomial and the initial approximations to P and Q are read as input. */

Source code:
#include<stdio.h>
#include<math.h>
#define max 10
#define min 0.0005

void main()
{
 float a[max], b[max], p, q, error, tempp, tempq;
 int i, degree, j=0;

 /* accepting the polynomial equation in the following form,
            xn + a0xn-1 + a1xn-2 +……….+ an-2x + an-1 = 0      */

 printf("\n Enter degree of polynomial?  ");
 scanf("%d",&degree);

 for(i=degree; i>=0; i--)
 {
  printf("\n Enter coefficient of x^%d:  ",i);
  if(i<degree)
  {
             scanf("%f",&a[degree-1-i]);
             a[degree-1-i]/=a[degree];
  }
  else
            scanf("%f",&a[i]);
  }
  a[degree]=1;

  printf("\n X^2 + PX + Q \n Enter guesses for P and Q:  ");
  scanf("%f %f",&p,&q);

  error=1;
  while(error>=min)
  {
            for(i=0;i<=degree-3;i++)
           
if(i==0)
               b[i]=a[i]-p;
             else if(i==1)
                        b[i]=a[i]-p*b[i-1]-q;
             else
                   b[i]=a[i]-p*b[i-1]-q*b[i-2];

             tempq=q;
             q=a[degree-1]/b[degree-3];

             tempp=p;
             p=(a[degree-2]-q*b[degree-4])/b[degree-3];

             tempp=fabs(p-tempp);
             tempq=fabs(q-tempq);

             if(tempp>tempq)
                         error=tempp;
             else
                         error=tempq;

            j++;                                                      /* iteration no. */

  }

  printf("\n\n X^2 + %.3f*X + %.3f",p,q);
  printf("\n\n ---> Total iterations = %d",j);

  }
Output: 

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.