derivative and newton raphson

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DDCane
    New Member
    • Oct 2007
    • 16

    derivative and newton raphson

    i have made a code for finding a derivative and now im trying to use it to help me with a code for the newton raphson method:
    [CODE=python]
    def derivative (f,x,h):
    import math
    return float(1/(2*h)) * (f(x+h) - f(x-h))

    def solve (f,x0,h):
    delta= f(x(n))/fp(x(n)
    for x(n+1) in solve():
    x(n)-delta[/CODE]

    the first def works fine but i cant get the second def to work can anyone see what im doing wrong?
    Last edited by bartonc; Oct 30 '07, 12:36 AM. Reason: Added [CODE=python][/CODE] tags.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by DDCane
    i have made a code for finding a derivative and now im trying to use it to help me with a code for the newton raphson method:
    [code=Python]
    def derivative (f,x,h):
    import math
    return float(1/(2*h)) * (f(x+h) - f(x-h))

    def solve (f,x0,h):
    delta= f(x(n))/fp(x(n)
    for x(n+1) in solve():
    x(n)-delta[/code]

    the first def works fine but i cant get the second def to work can anyone see what im doing wrong?
    In function solve(), you have passed arguments 'f', 'x0' and 'h'. Where are 'x', 'fp', and 'n' defined? You are calling function 'solve()' recursively, but you are not passing any arguments to it. 'solve()' requires three arguments. The format of a 'for' loop is:
    [code=Python]for item in iterable:
    ......code..... .[/code]
    'item' cannot be an expression.[code=Python]>>> for x+1 in range(10):
    ... print x
    Traceback (SyntaxError: can't assign to operator
    >>> [/code]

    Comment

    • DDCane
      New Member
      • Oct 2007
      • 16

      #3
      how am im supposed to fix it? fp in solve is the fisrt function. u use the first function to get the derivative of f in the first function. do u know how i fix this?

      Originally posted by bvdet
      In function solve(), you have passed arguments 'f', 'x0' and 'h'. Where are 'x', 'fp', and 'n' defined? You are calling function 'solve()' recursively, but you are not passing any arguments to it. 'solve()' requires three arguments. The format of a 'for' loop is:
      [code=Python]for item in iterable:
      ......code..... .[/code]
      'item' cannot be an expression.[code=Python]>>> for x+1 in range(10):
      ... print x
      Traceback (SyntaxError: can't assign to operator
      >>> [/code]

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by DDCane
        how am im supposed to fix it? fp in solve is the fisrt function. u use the first function to get the derivative of f in the first function. do u know how i fix this?
        I am not familiar with the calculation you are attempting. I know that it will not work if 'fp()' is not defined inside of 'solve()' or globally to your module.

        Comment

        • DDCane
          New Member
          • Oct 2007
          • 16

          #5
          globally in the module?

          Originally posted by bvdet
          I am not familiar with the calculation you are attempting. I know that it will not work if 'fp()' is not defined inside of 'solve()' or globally to your module.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by DDCane
            globally in the module?
            Let's say your code is in a file called function.py.[code=Python]# code.py
            def fp():
            ....code....

            def derivative (f,x,h):
            import math
            return float(1/(2*h)) * (f(x+h) - f(x-h))

            def solve (f,x0,h):
            delta= f(x(n))/fp(x(n)
            for x(n+1) in solve():
            x(n)-delta

            if __name__ == __main__:
            ....call your functions...[/code]
            Function 'fp()' is global to module 'function'.

            Comment

            • DDCane
              New Member
              • Oct 2007
              • 16

              #7
              ok. is there a simpler way to write the newton raphsons method in python? ive looked at the discussions in the forums here and havent found anything that could help m efurther than what i already know. do u have any clue on how to write the method?

              Originally posted by bvdet
              Let's say your code is in a file called function.py.[code=Python]# code.py
              def fp():
              ....code....

              def derivative (f,x,h):
              import math
              return float(1/(2*h)) * (f(x+h) - f(x-h))

              def solve (f,x0,h):
              delta= f(x(n))/fp(x(n)
              for x(n+1) in solve():
              x(n)-delta

              if __name__ == __main__:
              ....call your functions...[/code]
              Function 'fp()' is global to module 'function'.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Originally posted by DDCane
                ok. is there a simpler way to write the newton raphsons method in python? ive looked at the discussions in the forums here and havent found anything that could help m efurther than what i already know. do u have any clue on how to write the method?
                The Newton Raphson method uses an iterative process to approximate the root of a function. That's all I know about the subject, and I had to look that up.

                Comment

                • KaezarRex
                  New Member
                  • Sep 2007
                  • 52

                  #9
                  Originally posted by DDCane
                  ok. is there a simpler way to write the newton raphsons method in python? ive looked at the discussions in the forums here and havent found anything that could help m efurther than what i already know. do u have any clue on how to write the method?
                  Here's what I have come up with:
                  import math
                  [CODE=python]def derivative (f, x, h):
                  return float((f(x + h) - f(x))) / h

                  def solve(f, x0, h, depth):
                  if depth > 0:
                  delta = f(x0) / derivative(f, x0, h)
                  return solve(f, x0 - delta, h, depth - 1)
                  else:
                  return x0[/CODE]
                  I changed the formulas in your function to ones I'm more familiar with, and I had to add the parameter "depth" to tell the solve function when to stop. That's necessary because when working with the Newton-Raphson Method, you have to choose how many times you use it.
                  To use the solve function, you can either do something like this:
                  [CODE=python]def function(x):
                  return math.cos(x) - x**3

                  solve(function, 0.5, 0.001, 6)[/CODE]
                  or:
                  [CODE=python]function = lambda x: math.cos(x) - x**3
                  solve(function, 0.5, 0.001, 6)[/CODE]
                  Either way in that example your approximating the root of "cos(3) - x**3" by applying the Newton-Raphson Method 6 times with a starting guess of "0.5". The "0.001" is the "h" value used to approximate your derivative. The lower it is the more accurate the derivative will be.

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Originally posted by KaezarRex
                    Here's what I have come up with:
                    import math
                    [CODE=python]def derivative (f, x, h):
                    return float((f(x + h) - f(x))) / h

                    def solve(f, x0, h, depth):
                    if depth > 0:
                    delta = f(x0) / derivative(f, x0, h)
                    return solve(f, x0 - delta, h, depth - 1)
                    else:
                    return x0[/CODE]
                    I changed the formulas in your function to ones I'm more familiar with, and I had to add the parameter "depth" to tell the solve function when to stop. That's necessary because when working with the Newton-Raphson Method, you have to choose how many times you use it.
                    To use the solve function, you can either do something like this:
                    [CODE=python]def function(x):
                    return math.cos(x) - x**3

                    solve(function, 0.5, 0.001, 6)[/CODE]
                    or:
                    [CODE=python]function = lambda x: math.cos(x) - x**3
                    solve(function, 0.5, 0.001, 6)[/CODE]
                    Either way in that example your approximating the root of "cos(3) - x**3" by applying the Newton-Raphson Method 6 times with a starting guess of "0.5". The "0.001" is the "h" value used to approximate your derivative. The lower it is the more accurate the derivative will be.
                    That's great KaezarRex - nice solution to an interesting problem.
                    BV

                    Comment

                    • Viktor Sundelin
                      New Member
                      • Sep 2010
                      • 2

                      #11
                      Need help with derivative + solve

                      Hello!
                      (Newton-Raphson Method)
                      Can anyone describe how this code work?
                      What is depth, and how is solve working?
                      The function derivative only give us the derivative of a function:
                      for example:
                      Code:
                      >>> derivative(math.sin, math.pi, 0.0001)
                      -0.9999999983354435
                      But how is solve work?
                      solve use derivative function.
                      Code:
                      def derivative (f, x, h):
                          return float((f(x + h) - f(x))) / h
                       
                      def solve(f, x0, h, depth):
                          if depth > 0:
                              delta = f(x0) / derivative(f, x0, h)
                              return solve(f, x0 - delta, h, depth - 1)
                          else:
                              return x0

                      Comment

                      Working...