Fixed-Point Iteration Method

Fixed point : A point, say, s is called a fixed point if it satisfies the equation x = g(x).
ALGORITHM:
  1. Given an equation f(x) = 0.
  2. Convert f(x) = 0 into the form x = g(x).
  3. Let the initial guess be xi.
  4. Do xi+1 = g(xi).
  5. If |xi+1 - xi| < tolerance, root = xi+1, Else go to step 4.
  6. STOP.
PROGRAM in C:
#include<stdio.h> 
#include<conio.h>
#include<math.h>

float f(float x)
{
  return ((pow(x,2)+2)/3); /* x2 -3x+2 = 0 */
}

void main()
{
  float x0,x1=0,tmp,eps=0.001;
  int iteration=0;
  printf("Enter initial guess: ");
  scanf("%f",&x0);
  do
  {
    tmp=x1;
    x1=f(x0);
    iteration++;
    printf("\n%d\t%f",iteration,x1);
    x0=x1;
  }while(fabs(tmp-x1)>eps);
  printf("\n\n Real root=%0.4f\n Iteration=%d",x1,iteration);
  getch();

No comments:

Post a Comment