Results 1 to 9 of 9

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

Threaded View

  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

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