Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:31:57+00:00 2026-06-14T07:31:57+00:00

I have implemented the newton raphson algorithm for finding roots in C. I want

  • 0

I have implemented the newton raphson algorithm for finding roots in C. I want to print out the most accurate approximation of the root as possible without going into nan land. My strategy for this is while (!(isnan(x0)) { dostuff(); } But this continues to print out the result multiple times. Ideally I would like to setup a range so that the difference between each computed x intercept approximation would stop when the previous – current is less than some range .000001 in my case. I have a possible implementation below. When I input 2.999 It takes only one step, but when I input 3.0 it takes 20 steps, this seems incorrect to me.

(When I input 3.0)

λ newton_raphson 3
2.500000
2.250000
2.125000
2.062500
2.031250
2.015625
2.007812
2.003906
2.001953
2.000977
2.000488
2.000244
2.000122
2.000061
2.000031
2.000015
2.000008
2.000004
2.000002
2.000001
Took 20 operation(s) to approximate a proper root of 2.000002
within a range of 0.000001

(When I input 2.999)

λ newton_raphson 2.999
Took 1 operation(s) to approximate a proper root of 2.000000
within a range of 0.000001

My code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define RANGE 0.000001


double absolute(double number)
{
    if (number < 0) return -number;
    else return number;
}

 double newton_raphson(double (*func)(double), double (*derivative)(double), double x0){
    int count;
    double temp;
    count = 0;
    while (!isnan(x0)) {
            temp = x0;
            x0 = (x0 - (func(x0)/derivative(x0)));
            if (!isnan(x0))
                printf("%f\n", x0);
            count++;
            if (absolute(temp - x0) < RANGE && count > 1)
                break;
    }
    printf("Took %d operation(s) to approximate a proper root of %6f\nwithin a range of 0.000001\n", count, temp);
    return x0;
 }


/* (x-2)^2 */
 double func(double x){ return pow(x-2.0, 2.0); }
/* 2x-4 */
 double derivative(double x){ return 2.0*x - 4.0; }

 int main(int argc, char ** argv)
 {
   double x0 = atof(argv[1]);
   double (*funcPtr)(double) = &func; /* this is a user defined function */
   double (*derivativePtr)(double) = &derivative; /* this is the derivative of that function */

   double result = newton_raphson(funcPtr, derivativePtr, x0);
   return 0;
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:31:59+00:00Added an answer on June 14, 2026 at 7:31 am

    You call trunc(x0) which turns 2.999 into 2.0. Naturally, when you start at the right answer, no iteration is needed! In other words, although you intended to use 2.999 as your starting value, you actually used 2.0.

    Simply remove the call to trunc().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have implemented BlockUI (element blocking) for a large survey.. On most pages we
I have implemented the algorithm, but what I am facing is how can I
I am trying to implement a root finding algorithm. I am using the hybrid
I have implemented correctly bump's api, and added this code: - (void) configureBump {
I have implemented pagination to my data, but the problem is I only have
I have implemented a table view with multiple threads. Currently when a user taps
I have implemented a test method with Jersey to run on my Google AppEngine
I have implemented Facebook into my app but now I find that whenever I
I have implemented clean URLs using the following in my .htaccess RewriteEngine on RewriteCond
I have implemented a very basic sign up using email address+name, although I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.