15,917,971 members

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

Could someone please help? I tried to follow the algorithm in the book, but I am still new to programming and not good at reading them. Thank you in advance.

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.

Here is the updated one:

Could someone please help? I tried to follow the algorithm in the book, but I am still new to programming and not good at reading them. Thank you in advance.

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

Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

Algorithm: (see question)

Desired Tolerance: .01

Maximum Iterations: 10

Iteration 1: Current value = 1.565085

Iteration 2: Current value = 1.793573

Iteration 3: Current value = 1.885944

Iteration 4: Current value = 1.922848

Iteration 5: Current value = 1.937508

Process returned 1072693280 (0x3FF00020) execution time : 7.555 s

Press any key to continue.