Thread: Newton's Method for Approximating Roots. What is the error?

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    16

    Newton's Method for Approximating Roots. What is the error?

    /* This program will take an initial guess for the root of the equation (x^3)-3=0 and will use Newton's Method to approximate an approximate root after 500 iterations, or once the margin of error is within 0.000001. The result will the be printed to the standard output. */

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    #define E 0.000001
    
    
    double Fx(double x1)
    {
        return pow(x1,3) - 3;
    }
        
    double df (double x2)
    {
        return 3*pow(x2,2);
    }
    
    
    int main() {
    
    
    double x, F, Fd;
    int count = 0; 
    
    
    
    
    
    
    printf("Input Initial approximation, x: ");
    scanf("%f",&x); // Address specifier
    
    
    if (x != 0) {
    
    
       
    /*do
      {
      
      F = Fx(x)/df(x);
      Fd = x-F;
      
      count+=1;
      
      if (fabs(F) < E) {
      
      printf("The root is: %f\n", x);
      printf("The number of iterations is: %d",count);  
      }
      x = Fd;
    */
    while(count < 1) {
        if(df(x)>0)
            F = Fx(x)/df(x);
        Fd = x-F;
        count+=1;
        if ((count == 1) || (fabs(F)<=E)) {
        printf("The root is: %f\n", x);
        printf("The number of iterations is: %d",count); 
        } 
        x = Fd;
    }
    
    
    
    
    /*printf("The root is: %f\n", x);
    printf("The number of iterations is: %d",count);    //pow(x,3); 
    */ 
    }
    else 
        printf("That is not a valid input!");
        
        return 0; 
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    while(count < 1)
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newton's Method Program
    By BP8xC3 in forum C Programming
    Replies: 6
    Last Post: 09-08-2011, 08:55 PM
  2. Newton raphson method - C
    By arazki1yes in forum C Programming
    Replies: 8
    Last Post: 04-04-2011, 01:11 PM
  3. Newton's Method
    By hottiefee in forum C++ Programming
    Replies: 33
    Last Post: 02-19-2011, 09:57 AM
  4. Newton method to find roots
    By ICool in forum C Programming
    Replies: 17
    Last Post: 11-18-2007, 11:46 AM
  5. Newton's method
    By Cmuppet in forum C Programming
    Replies: 5
    Last Post: 10-19-2004, 11:02 AM

Tags for this Thread