15,917,971 members
3.00/5 (1 vote)
See more:
I am trying to write a program to find roots using Fixed Point Iteration method and I am getting zero everytime I run this. entering p0=1, Tol=.01

Algorithm:

INPUT initial approximation p0; tolerance TOL; maximum number of iterations N0.
OUTPUT approximate solution p or message of failure.

Step 1 Set i=1.
Step 2 While i <= N0 do Steps 3-6.
Step 3 Set p=g(p0). (Compute pi.)
Step 4 If |p-p0|<tol mode="hold"> OUTPUT (p); (The procedure was successful.)
STOP.
Step 5 Set i=i+1.
Step 6 Set p0=p. (Update p0.)
Step 7 OUTPUT ('The method failed after N0 iterations, N0=', N0);
(The procedure was unsuccessful.)
STOP.

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

double f(double x)
{
return x*x*x*x-3*x*x-3;  //change equation for each problem
}

int main()
{
double p, p0, Tol;
int i=1;
int No;

printf("Enter approximate p: ");
scanf ("%lf", &p0);

printf("Desired Tolerance: ");
scanf ("%lf", &Tol);

printf("Maximum Iterations: ");
scanf ("%d", &No);

while (i<=No)
{
p = p0;

if((fabs(p-p0))<Tol)
{
printf("%lf", &p);
break;
}
printf("Iteration %d: Current value = %lf\n", i, p);

i++;  //i=i+1
p0=p;

if (i>No)
{
printf("Method Failed after %d", No);
printf(" iterations");
}```

Here is the updated one:

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

double f(double x)
{
return x*x*x*x-3*x*x-3;  //change equation for each problem
}

double g(double x)
{
return pow(3*x*x+3,.25);
}

int main()
{
double p, p0, Tol;
int i=1;
int No;

printf("Enter approximate p: ");
scanf ("%lf", &p0);

printf("Desired Tolerance: ");
scanf ("%lf", &Tol);

printf("Maximum Iterations: ");
scanf ("%d", &No);

while (i<=No)
{
p = g(p0);

if((fabs(p-p0))<Tol)
{
//printf("%lf", &p);
break;
}
printf("Iteration %d: Current value = %lf\n", i, p);

i++;  //i=i+1
p0=p;

if (i>No)
{
printf("Method Failed after %d", No);
printf(" iterations");
}

}

}```
Posted
Updated 8-Feb-18 10:18am
v5
Vedat Ozan Oner 8-Feb-14 16:55pm
what do you expect if you say p=p0, then check for p-p0?
MalDrHoop 8-Feb-14 17:11pm
I just looked back at the algorithm, and it says to set p=g(p0). (compute pi.) so thats probablly part of where I messed up, I am just not sure exactly what I need to do for my g(p0).

Algorithm: (see question)
Vedat Ozan Oner 8-Feb-14 17:26pm
first find an equation (g) for the iteration from the function f. then iterate while x-g(x) doesn't fall into your tolerance as in the said in the algorithm.
MalDrHoop 8-Feb-14 17:34pm
I quess I dont quite grasp the concept. Ive missed some class due to illness. Is g(x) basically a rewritten form of the given equation? The equation I have used here is x^4-3x^2-3. so does that mean I need an element of x-(x^4-3x^2-3)? Im sorry, I am trying to figure it out.
Vedat Ozan Oner 8-Feb-14 17:44pm
ok. more help comes. to find a root: x^4-3*x^2-3=0 then x^4=3*x^2+3 then x = (3*x^2+3)^0.25 which is your g function. g(x) = (3*x^2+3)^0.25. for a given root of function f, g(x) must be equal to x. start with 0 for the x value than apply the algorithm.