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.

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

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.

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)

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

→ The Comprehensive Guide to Cloud Computing
A complete overview of Cloud Computing focused on what you need to know, from selecting a platform to choosing a cloud vendor.