Write a C program to find a root of $x^3 - 3*x + 1 = 0$ by fixed point iteration method (including convergence check)

1.9k Views Asked by At

The code works fine. But I want to include the convergence criterion which is as follows:

if the equation is written in the form $x=g(x)$, then condition of convergence is: $g'(x)<1$.

Note that: $g(x)=\sqrt [3]{3\,x-1}$ and $g'(x)=\left( 3\,x-1 \right) ^{-2/3}$

Please modify my code for convergence.

  /* g (x) = (3x - l)^(l/3 ), x0=5 */
/* Fixed po int of Iteration Method */
#include<stdio.h>
#include<conio.h>
#include<math.h>
float g(float x)
{
return(pow((3*x-1), (.3333 )));
}

void main()
{
int n, i ;
float x0 ,xl ;
clrscr() ;
printf ("Enter xO , n");
scanf ("%f%d" ,&x0,&n);
for (i= 1;i<=n;i++ )
{

xl=g(x0);
printf("\t\t i=%d \t\t x1=%f\n\n", i,xl);
if(fabs (xl-x0)<.0001)
break ;
else
x0=xl;
}
printf ("The root is %£ ", xl);
getch();
}
1

There are 1 best solutions below

5
On BEST ANSWER

Cleaned the code a bit, and used another function $g(x) = (x^3 + 1)/3$ and $g'(x) = x^2$. So as long as you start in $|x|<1$ convergence will work

/* g (x) = (3x - l)^(l/3 ), x0=5 */
/* Fixed po int of Iteration Method */
#include <stdio.h>
#include <math.h>

float g(float x)
{
  return (x * x * x + 1) / 3.;
}

float dg(float x)
{
  return x * x;
}

int main()
{
  int n, i;
  float x0, xl, d;
  char convergence;
  printf ("Enter x0, n: ");
  scanf ("%f %d", &x0, &n);

  for (i = 1; i <= n; i ++)
  {
    xl = g(x0);
    d = dg(x0);
    convergence = fabs(d) < 1 ? 't' : 'f';

    printf("\t\t i = %d \t\t x1 = %f \t\t g'(x1) = %f \t\t convergence = %c\n", i, xl, d, convergence);
    if(fabs (xl - x0) < .0001)
      break;
    else
      x0 = xl;
  }

  printf ("The root is %f\n", xl);
}

This is a test

Enter x0, n: 0.1 10
         i = 1       x1 = 0.333667       g'(x1) = 0.010000       convergence = t
         i = 2       x1 = 0.345716       g'(x1) = 0.111333       convergence = t
         i = 3       x1 = 0.347107       g'(x1) = 0.119520       convergence = t
         i = 4       x1 = 0.347273       g'(x1) = 0.120483       convergence = t
         i = 5       x1 = 0.347294       g'(x1) = 0.120599       convergence = t
The root is 0.347294