VBA

Moderators: PavlinII
Number of threads: 1673
Number of posts: 3078

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
real-valued root of a polynomial_newton-raphson algorithmn_VBA code Posted by blankablanka on 21 Jan 2012 at 8:29 AM
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 will be extremely thankful for any advice you can offer me!




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.