# POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

• 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 goes 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: POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

Thanks a lot for your help!

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 be grateful if you could provide me with some more advice!

• Re: POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

So what should I do in this 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)

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 coefficientcients, 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

• Re: POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

Here's the code:

• Re: POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

this part:

function polysolver (coeffs as range, powers as range) as double

should not be changed

• Re: POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

Yep I'll have a look

what is the work sheet lay out?

if maybe better to have fixed ranges rather that use input ranges

in the mean time have a look at this example forum.ozgrid.com/index.php?attachment/43335/

• Re: POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

What do you mean by ''worksheet layout''?
Anyway, thanks for your help!

• Re: POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

Number of powers and coefficients goes from 1 to n, so the number of powers is n.

• Re: POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

try....

• Re: POLYNOMIALS vba code newton-raphson algorithmn real-valued root of a polynomial

I think next i and next iii should be reversed. But the function still isn't working properly. I tried it with several examples.
Thanks for helping me!

## Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!