Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


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
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 will be extremely thankful for any advice you can offer me!

Sign In or Register to comment.