Newton Raphson Method

This is a discussion on Newton Raphson Method within the C Programming forums, part of the General Programming Boards category; Hello I am trying to programme c to use the newton raphson method to evaluate roots of an equation. My ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    8

    Newton Raphson Method

    Hello I am trying to programme c to use the newton raphson method to evaluate roots of an equation.

    My code is as follows:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    double function(double );
    double function2(double );
    int main()
    {
        double x[100];
        double fx[100];
        double gradx[100];
        int n1=0;  
        x[0]=0;
        n1=-1;
    
        do  
        {
            n1++;
            fx[n1]=function(x[n1]);
            gradx[n1]=function2(x[n1]);
            x[n1+1]=x[n1]-(fx[n1]/gradx[n1]);
        }
        while(fx[n1+1]<1e-8||(fx[n1+1]>-1e-8));
    
        for(n1=0;n1<100;n1++)  
        {
            printf("%g\t %g\t %g\n",x[n1],fx[n1],gradx[n1]);
        }
    
        return 0;
    }
    
    double function(double argument1)
        {
            double result;
            double a;
            a=0.4+(8039.0/25000);
            result = sin(pow(argument1,a)-pow(argument1,(1/a))+(a*argument1));
            return result;
        }
    
    double function2(double argument1)
        {
            double result2;
            double aa;
            aa=0.4+(8039.0/25000);
            result2=((aa*pow(argument1, aa-1))-(1/aa*pow(argument1, (1/aa)-1))+aa)*sin(pow(argument1,aa)-pow(argument1,(1/aa))+(aa*argument1));
            return result2;
        }
    I'm getting an answer out but don't think it is the correct one. Any advice on where I am going wrong would be appreciated.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by zeb1d1ah View Post
    Hello I am trying to programme c to use the newton raphson method to evaluate roots of an equation.

    My code is as follows:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    double function(double );
    double function2(double );
    int main()
    {
        double x[100];
        double fx[100];
        double gradx[100];
        int n1=0;  
        x[0]=0;
        n1=-1;
    
        do  
        {
            n1++;
            fx[n1]=function(x[n1]);
            gradx[n1]=function2(x[n1]);
            x[n1+1]=x[n1]-(fx[n1]/gradx[n1]);
        }
        while(fx[n1+1]<1e-8&&(fx[n1+1]>-1e-8));
    
        for(n1=0;n1<100;n1++)  
        {
            printf("%g\t %g\t %g\n",x[n1],fx[n1],gradx[n1]);
        }
    
        return 0;
    }
    
    double function(double argument1)
        {
            double result;
            double a;
            a=0.4+(8039.0/25000);
            result = sin(pow(argument1,a)-pow(argument1,(1/a))+(a*argument1));
            return result;
        }
    
    double function2(double argument1)
        {
            double result2;
            double aa;
            aa=0.4+(8039.0/25000);
            result2=((aa*pow(argument1, aa-1))-(1/aa*pow(argument1, (1/aa)-1))+aa)*sin(pow(argument1,aa)-pow(argument1,(1/aa))+(aa*argument1));
            return result2;
        }
    I'm getting an answer out but don't think it is the correct one. Any advice on where I am going wrong would be appreciated.
    I think there is no need to explain it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    Thanks for that. My main problem is that I want to take my calculated values of x, fx, and gradx and use it to calculate their next values. The value of x should converge using this method, however at the minute they are just all over the place. I think it's a problem with one of my loops, possibly the printf statement, just don't really have the experience to analyse it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 08:51 AM
  3. Replies: 2
    Last Post: 01-22-2008, 04:22 PM
  4. Newton Raphson method code
    By taebin in forum C Programming
    Replies: 2
    Last Post: 10-17-2004, 03:44 AM
  5. Newton Raphson method code
    By taebin in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2004, 04:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21