Results 1 to 9 of 9

Thread: real-valued root of a polynomial_newton-raphson algorithmn_VBA code

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    real-valued root of a polynomial_newton-raphson algorithmn_VBA code

    Could anyone help me with the following problem:

    Write a VBA function that computes one real-valued root of the polynomial defined by

    P(x) = (summation mark from i=1 to n) coeffs.cells(i,1) x^(powers.cells(i,1))

    where n=coeffs.Rows.Count using Newton's Solver. For the input, coeffs is a Range with n rows (and 1
    column) of real-valued coefficientcients, and powers is a Range with n rows (and 1 column) of non-negative,
    integer powers.
    I guess ''Newton's Solver'' is actually the Newton-Raphson algorithmn.
    My solution so far is:
    Function polySolver(coeffs As Range, powers As Range) As Double
    Dim rowCount As Integer
    Dim i As Integer
    Dim xn As Double
    Dim xnm1 As Double
    Dim fx As Double
    fx = 0
    Dim fxprime As Double
    fxprime = 0
    xnm1 = 0.1
    rowCount = coeffs.Rows.count
    Do
    For i = 1 To rowCount
    fx = fx + coeffs.Cells(i, 1) * xnm1 ^ powers.Cells(i, 1)
    fxprime = fxprime + (powers.Cells(i, 1) * coeffs.Cells(i, 1)) * xnm1 ^ _
    (powers.Cells(i, 1) - 1)
    Next i
    xn = xnm1 - fx / fxprime
    xnm1 = xn
    Loop Until (Abs(fx) < 0.00001)

    polySolver = xn
    End Function
    When I choose different initial values of x0 (in the code denoted with xnm1), I get different solutions. This function probably shouldn't even contain the initial x0 (I suppose that the should't have an assigned value for xnm1). The algorithmn should be valid for any polynomial and the outcome should be one real-valued root of this polynomial.

    I would be most grateful if anyone could help me!
    Attached Files Attached Files

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: real-valued root of a polynomial_newton-raphson algorithmn_VBA code

    Your code looks correct, inasmuch as you've correctly computed the polynomial, its derivative, and the next next value of xnm1. You need to pick an initial value, which is often arbitrary. Hard-coding one is fine, unless the exercise is in choosing initial values. It doesn't sound like it is, though. (I don't have Word, so I can't run it.)

    It's entirely fine for different initial values to give different solutions. For instance, in the simple case of P(x) = x*(x-1), the initial value of x0 = 0 gives the root x=0, while the initial value of x0 = 1 gives the root x=1. In more complicated situations, which root you get (if there are multiple roots) depends upon which you're closest to (this is a lie, but it's close enough to the truth).

    Newton's method doesn't always converge. A nice example I'm lifting from Wikipedia is P(x) = x^3 - 2x + 2 with x0 = 0. The x values oscillate between 1 and 0. Still, polynomials are simple enough that you'll usually find a solution by just blindly guessing an initial value.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Question Re: real-valued root of a polynomial_newton-raphson algorithmn_VBA code

    Thank you very much for your reply!

    But there is one problem: what if there is no concrete polynomial (i.e. the code should be valid for just any polynomial you can come across)? That means that I don't have one single pre-defined derivative either. And I also can't guess the inital zero.

    Under such conditions, can I even write a proper VBA code (that gives the right result for all polynomials)?

    Or do I absolutely need one polynomial and then base the whole VBA code on it?

    I would appreciate if you could clarify these issues.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Re: real-valued root of a polynomial_newton-raphson algorithmn_VBA code

    vb Code:
    1. Option Explicit
    2. Option Base 1
    3.  
    4.  
    5. Function polySolver(coeffs As Range, powers As Range) As Double
    6.     Dim rowCount As Integer
    7.     Dim i As Integer
    8.     Dim xn As Double
    9.     Dim xnm1 As Double
    10.     Dim fx As Double
    11.     fx = 0
    12.     Dim fxprime As Double
    13.     fxprime = 0
    14.     xnm1 = 0.1
    15.     rowCount = coeffs.Rows.Count
    16.     Do
    17.         For i = 1 To rowCount
    18.             fx = fx + coeffs.Cells(i, 1) * xnm1 ^ powers.Cells(i, 1)
    19.             fxprime = fxprime + (powers.Cells(i, 1) * coeffs.Cells(i, 1)) * xnm1 ^ _
    20.             (powers.Cells(i, 1) - 1)
    21.         Next i
    22.     xn = xnm1 - fx / fxprime
    23.     xnm1 = xn
    24.     Loop Until (Abs(fx) < 0.00001)
    25.    
    26.     polySolver = xn
    27. End Function

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Re: real-valued root of a polynomial_newton-raphson algorithmn_VBA code

    So what should I do in my case:
    -I have only 2 arguments (vectors) and my random polynomial is generated from them
    -I don't have the x0 argument in the function (meaning that I can't change
    x0)

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Re: real-valued root of a polynomial_newton-raphson algorithmn_VBA code

    Here are the instructions:

    Write a VBA function that computes one real-valued root of the polynomial defined by

    P(x) = (summation mark from i=1 to n) coeffs.cell(i,1) x^(powers.cells(i,1))

    where n=coeffs.Rows.Count using Newton's Solver. For the input, coeffs is a Range with n rows (and 1 column) of real-valued coefficients, and powers is a Range with n rows (and 1 column) of non-negative, integer powers.

    My function MUST start like this:

    Function polySolver(coeffs As Range, powers As Range) As Double

    --- code ---

    End function

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: real-valued root of a polynomial_newton-raphson algorithmn_VBA code

    I noticed a pair of mistakes in your most recent code. You aren't resetting fx and fxprime each time through the loop.

    Code:
        Do
            For i = 1 To rowCount
    should be

    Code:
        Do
            fx = 0
            fxprime = 0
            For i = 1 To rowCount
    Again, picking x0 arbitrarily and hard coding it should not be a problem.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Re: real-valued root of a polynomial_newton-raphson algorithmn_VBA code

    Thanks a lot for your help!

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Re: real-valued root of a polynomial_newton-raphson algorithmn_VBA code

    hi can u please teach me to do this newton rhapson method in visual basic? newbie here in VBforums thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width