Thread: Newton raphson method - C

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Newton raphson method - C

    to be honest i am a bit confused .. i don't know where to start ..

    Code:
    program to find the root of the equation x^3+3x-1  by newton raphson method

    PHP Code:
    Computation of a root of f(x)= 0 by newton raphson method 
    PHP Code:
    Algorithm 
    step 0
    Code:
    define f(x) , f'(x)
    step 1
    Code:
    Input x0 , epsilon,maxit
    x0 is the initial guess of the root , epsilon is the desired accuracy of the root and maxit is the maximum number of iterations allowed
    step 2
    Code:
    set i =0
    step 3
    Code:
    set f0=f(x0)
    step 4
    Code:
    compute df0 = f'(x0)
    step 5
    Code:
    set x1 = x0-f0/df0
    step 6
    Code:
    set i = i +1
    step 7
    Code:
    check if |(x1-x0)|x1|< epsilon , then print "root is" , x1 and stop
    else if i<n, then set x0=x1 and go to step 3
    step 8
    Code:
    write "iterations do not converge"
    step 9
    Code:
    end

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Is it the problem you don't understand... or the solution?

    It may help to work the problem step by step on paper, detail every step required... that should provide a general map of the solution that you can then begin working into sourcecode...

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest you start by writing a C program.
    The steps you have should be comments in the program.
    Under each comment write the C code that does what you think the comment said it should do.

    Next, Compile the program and work on fixing the compile errors/warnings

    After that ask a question like what is the C code for step number x.

    Tim S.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    this is a program i have .. trying to understand this stuff is really hard .. fact

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #define f(x) (pow(x,3) + 3 * x -  1)
    #define g(x) (3* pow (x,2) + 3
    
    void main ()
    
    {
    
    int i ;
    
    float a , b , f [10],g[10],x[10];
    
    clrscr();
    
    a=0.001;
    
    read ;
    
    b = a +1 ;
    
    if ((f(a) * f(b)) > 0)
    
    {
    
    a=b;
    
    goto read ;
    }
    
    else
    
    x[0]=a;
    
    for (i = 0 ; i <  20 ; i++)
    {
    
    f[i]= f(x[i]);
    
    g[i]= g(x[i]);
    
    
    x [i +  1] = x [i] - (f[i] / g [i]);
    
    if (fabs(x[i + 1] - x [i]) < pow(10,-6))
    
    goto write ;
    }
    
    write :
    
    printf("\n smaller positive root is %f" , x [i]);
    
    getch ();
    }

    now what ?

    i am thinking too ...lol






    Quote Originally Posted by CommonTater View Post
    Is it the problem you don't understand... or the solution?

    It may help to work the problem step by step on paper, detail every step required... that should provide a general map of the solution that you can then begin working into sourcecode...

    nice question ..

    the problem is .. x^3+3x-1

    first i need to find the root of the equation by newton raphson method
    Last edited by arazki1yes; 04-04-2011 at 11:25 AM.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Newton method is
    Code:
      xn+1 = xn - f(xn)/f1(xn)
    I'd write a separate function say.
    Code:
    // function f to solve
    double f(double x) { ... }
    // derivative of function f
    double f1(double x) {  ... } 
    
    x = approx;           // our root be x
    while( 1 ) {
      x = x - f(x)/f1(x);
      if( convergence_met)
        break;   
      if( max_iter_hit)
        break;
    }
    print solution;
    Of course, there're far better ways.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by Bayint Naung View Post
    Newton method is
    Code:
      xn+1 = xn - f(xn)/f1(xn)
    I'd write a separate function say.
    Code:
    // function f to solve
    double f(double x) { ... }
    // derivative of function f
    double f1(double x) {  ... } 
    
    x = approx;           // our root be x
    while( 1 ) {
      x = x - f(x)/f1(x);
      if( convergence_met)
        break;   
      if( max_iter_hit)
        break;
    }
    print solution;
    Of course, there're far better ways.

    writing this stuff in a computer language ... ?

    that too especially first time ..

    is the hardest ........ i have ever come across


    the maths questions and answers .. to use newton raphson .. makes a bit of sense now ...

    but writing that as a computer language ..


    jeesus christ ...

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by arazki1yes View Post
    writing this stuff in a computer language ... ?

    that too especially first time ..

    is the hardest ........ i have ever come across


    the maths questions and answers .. to use newton raphson .. makes a bit of sense now ...

    but writing that as a computer language ..


    jeesus christ ...
    Well then you need to read up on C programming as well as math.

    Click this --- > Teach Yourself C in 21 Days -- Table of Contents

    Not the world's best C manual, but certainly a quick and easy introduction.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    first of all this program finds a root of a nonlinear equation

    if you carefully look at the functions invoked

    macrosf(x) ,FD(x) , library function fabs()


    and the variables used

    x0 - initial value of x

    xn - new value of %

    fx - function value at x

    fdx - value of function derivative at x

    count - number of iterations done

    Code:
    constants used
    EPS - error bound
    MAXIT -maximum number of iterations permitted



    so it becomes complicated

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of telling us how complicated you think it is, do something about it. I can tell you that in my opinion this is not complicated, but that won't make any difference in getting the job done.

    Have you tried working through the mathematics yet? If not, then that is one place to start since it will allow you to understand how to implement the algorithm.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newton raphson
    By kariisa in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2011, 08:13 AM
  2. using newton raphson method
    By sanskaar in forum C Programming
    Replies: 21
    Last Post: 09-27-2010, 12:02 AM
  3. Newton Raphson Method
    By zeb1d1ah in forum C Programming
    Replies: 2
    Last Post: 12-07-2009, 06:26 AM
  4. Newton Raphson method code
    By taebin in forum C Programming
    Replies: 2
    Last Post: 10-17-2004, 02:44 AM
  5. Newton Raphson method code
    By taebin in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2004, 03:07 PM