473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Polynommial calculator

Can someone please tell me IF IT´S POSSIBLE, to have a vba code for calculating real-valued root of the polynomial of n power in excel 2007, by using Newton method?
i tried to solve a simple P(x) such as X^10 + X^9+X^8+X^7+X^6+X^5+X^4+X^3+X^2+X-200 = 0 , by using solver from excel 2007, but it didn't work out.
Al old friend of mine, NEO, send me the following code:

Expand|Select|Wrap|Line Numbers
  1. Sub polySolver(coeffs As Range, powers As Range) As Double 
  2.     Dim rowCount As Integer 
  3.     Dim i 
  4.     Dim iii 
  5.     Dim xn As Double 
  6.     Dim xnm1 As Double 
  7.     Dim fx As Double 
  8.     fx = 0 
  9.     Dim fxprime As Double 
  10.     fxprime = 0 
  11.     xnm1 = 0.1 
  12.     Do 
  13.         For Each i In coeffs 
  14.             For Each iii In powers 
  15.                 fx = (fx + i) * xnm1 ^ iii 
  16.                 fxprime = fxprime + (iii * i) * xnm1 ^ (iii - 1) 
  17.  
  18.                 xn = xnm1 - fx / fxprime 
  19.                 xnm1 = xn 
  20.  
  21.             Next i 
  22.         Next iii 
  23.     Loop Until (Abs(fx) < 0.00001) 
  24.  
  25.     polySolver = xn 
  26. End Function 
but i haven't any sucess with it in trying to find root (s)of P(x) above ..
Can someone help fix it up??
I'm looking forward to receiving good news.
many thks
Dec 18 '13 #1
2 1324
This looks interesting. I'll see what I can do with it.

After a very quick look there are some serious problems with your code. Did it run at all? If so, I'm surprised.

1. PolySolver should be declared as a Function, not a Subroutine. Returning a value makes it a function.

2. Haphazard use of parentheses. Parentheses determine the order of execution of mathematical functions. Never be afraid to use too many parentheses, both to make life easier for the computer and for you. I've never seen the actual algorithm but it would be a miracle if the mathematical expressions return anything meaningful the way they are.

For instance in this line:

fxprime = fxprime + (iii * i) * xnm1 ^ (iii - 1)

Due to the precedence of mathematical operators the computer will do this, I think:

1. Raise xnm1 to the (iii - 1) power
2. multiply (iii * i)
3. multiply the result of 2. by the new value of xnml
4. Add the result to fxprime
5. Assign the result to fxprime.

Is that what you want it to do?
Apr 7 '14 #2
OK. I got it to compile with no errors. It produces 0s, but it does it very quickly.[

I converted it to VB 6 because I haven't used VBA for a decade. If you have any questions about the changes I made, just ask.

Here's the modified code.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. '---------------------------------------------------------
  4. ' Procedure : cmdRun_Click
  5. ' Author    : Rory Starkweather
  6. ' Date      : 04/07/2014
  7. ' Purpose   : Fill two arrays that replace the Range type
  8. '               variables that Excel VBA uses. intCoeffs is
  9. '               filled with 10 1s. intPowers is filled donward
  10. '               from 10 to 1. This matches up with the example
  11. '               in the OP.
  12. '---------------------------------------------------------
  13. Private Sub cmdRun_Click()
  14.  
  15.     Dim intCoeffs(1 To 10) As Integer
  16.     Dim intPowers(1 To 10) As Integer
  17.     Dim intIndex As Integer
  18.     Dim dblResult As Double
  19.  
  20.     On Error GoTo cmdRun_Click_Error
  21.       Dim errstrErrorMessage As String
  22.       Dim errlngRetVal As Long
  23.  
  24.     For intIndex = 10 To 1 Step -1
  25.         intCoeffs(intIndex) = 1
  26.         intPowers(intIndex) = intIndex
  27.     Next ' intIndex
  28.  
  29.     dblResult = rsPolySolver(intCoeffs(), intPowers())
  30.     frmMain.lblRoot.Caption = CStr(dblResult)
  31.  
  32.     '*** On Error GoTo 0
  33.     Exit Sub
  34.  
  35. cmdRun_Click_Error:
  36.  
  37.     errstrErrorMessage = "Error " & Err.Number & _
  38.                          " (" & Err.Description & _
  39.                          ") in procedure cmdRun_Click" & _
  40.                          " of Form frmMain"
  41.     errlngRetVal = MsgBox(errstrErrorMessage, _    vbExclamation + vbOKOnly)
  42.  
  43. End Sub
  44.  
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. '///Public Function PolySolver(coeffs As Range, powers As Range) As Double
  4. '---------------------------------------------------------------------------------------
  5. ' Procedure : rsPolySolver
  6. ' Author    : Rory Starkweather
  7. ' Date      : 04/07/2014
  8. ' Purpose   : The good stuff. Almost. Only the looping code was changed. The
  9. '              math is exactly like the original.
  10. '---------------------------------------------------------------------------------------
  11. Public Function rsPolySolver(ByRef intCoeffs() As Integer, _
  12.                             ByRef intPowers() As Integer) As Double
  13.  
  14.     'Dim rowCount As Integer    *Not used
  15.     Dim i As Integer
  16.     Dim iii As Integer
  17.     Dim xn As Double
  18.     Dim xnm1 As Double
  19.     Dim fx As Double
  20.     Dim fxprime As Double
  21.  
  22.     Dim intInnerIndex As Integer
  23.     Dim intOuterIndex As Integer
  24.  
  25.     On Error GoTo PolySolver_Error
  26.       Dim errstrErrorMessage As String
  27.       Dim errlngRetVal As Long
  28.  
  29.     fx = 0
  30.     fxprime = 0
  31.     xnm1 = 0.1
  32.  
  33.  
  34.     Do
  35.         '///For Each i In coeffs
  36.         For intOuterIndex = 1 To 10
  37.             i = intCoeffs(intOuterIndex)
  38.             '///For Each iii In powers
  39.             For intInnerIndex = 1 To 10
  40.                 iii = intPowers(intInnerIndex)
  41.                 fx = (fx + i) * xnm1 ^ iii
  42.                 fxprime = fxprime + (iii * i) * xnm1 ^ (iii - 1)
  43.  
  44.                 xn = xnm1 - fx / fxprime
  45.                 xnm1 = xn
  46.  
  47.             '///Next i
  48.             Next ' intInnerIndex
  49.         '///Next iii
  50.         Next ' intOuterIndex
  51.  
  52.     Loop Until (Abs(fx) < 0.00001)
  53.  
  54.     rsPolySolver = xn
  55.  
  56.     '*** On Error GoTo 0
  57.     Exit Function
  58.  
  59. PolySolver_Error:
  60.  
  61.     errstrErrorMessage = "Error " & Err.Number & _
  62.                          " (" & Err.Description & _
  63.                          ") in procedure PolySolver" & _
  64.                          " of Module mdlProcs"
  65.     errlngRetVal = MsgBox(errstrErrorMessage, vbExclamation + vbOKOnly)
  66.  
  67.  
  68. End Function
  69.  
  70.  
It exits the Loop the first time Abs(fx)is tested because fx is always 0.
Apr 7 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
3
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
3
by: Art | last post by:
Hi, In part of my application the user may need to do a simple arithmetic calculation in order to get the value to put in a text box. I was thinking that it would be good if I could display the...
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
5
Deathwing
by: Deathwing | last post by:
Hi everyone one I'm playing around with trying to make an expense calculator. I would like it so that the user can keep enter expenses until they have no more expenses. Then I would like for the...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
0
Curtis Rutland
by: Curtis Rutland | last post by:
Have any of you ever used a Reverse Polish Notation calculator? I did in high school. It was easily the best calculator ever (the HP=32SII). RPN is great, because you don't have to use parenthesis....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.