PDA

View Full Version : REAL VALUED ROOT OF A POLYNOMIAL_newton raphson algorithmn_VBA



blanka
01-21-2012, 09:13 AM
Could anyone help me with thefollowing problem:



Write a VBA function thatcomputes 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.Countusing Newton's Solver. For the input, coeffs is a Range with n rows (and1

column) of real-valuedcoefficientcients, and powers is a Range with n rows (and1 column) of non-negative,

integer powers.

I guess ''Newton's Solver'' is actually theNewton-Raphson algorithmn.

My solution so far is:

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

Dim rowCount AsInteger

Dim i As Integer

Dim xn As Double

Dim xnm1 As Double

Dim fx As Double

fx = 0

Dim fxprime AsDouble

fxprime = 0

xnm1 = 0.1

rowCount =coeffs.Rows.count

Do

For i = 1 TorowCount

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 codedenoted with xnm1), I get different solutions. This function probably shouldn'teven contain the initial x0 (I suppose that the should't have an assigned valuefor xnm1). The algorithmn should be valid for any polynomial and the outcomeshould be one real-valued root of this polynomial.
I would really appreciate your help!