akindofmagic
09-11-2010, 10:41 AM
Hello guys, I have a problem as a VBA beginner, I hope you can give me some hints.
I'm using Microsoft Excel 2003 with Microsoft Visual Basic 6.5.
I have to multiply an arbitrary number of polynomials, say P1*P2*P3*...*Pn.
P1 = an + a(n-1)*X^1 + a(n-2)*X^2 + ...+ a0*x^i
.
.
.
Pn = ...
I have managed to write the code for 3 polynomials, but I'm stuck for n polynomials.
I think the Sub needs to work with a matrix, where each row is the coefficients of each polynomial. The user will type the
maximum grades and the coefficients in the worksheet. The program will read the matrix and the calculus will begin.
Help me with this.
Thank you!
Here is what I've worked so far:
Sub Product3Pol()
Dim n As Integer, m As Integer, r As Integer, xo As Integer
Dim p1 As Integer, p2 As Integer
Dim aArray(20) As Double, bArray(20) As Double, cArray(20) As Double, qArray(60) As Double, rArray(60) As Double
' Colecting user data:
n = Application.InputBox("The maxim grade for the first polynomial is?")
m = Application.InputBox("The maxim grade for the second polynomial is?")
r = Application.InputBox("The maxim grade for the third polynomial is?")
xo = Application.InputBox("x0 value is?")
For i = 0 To n
aArray(i) = Application.InputBox("a" & i & "= ", "1st polynomial coefficients")
Next i
For i = 0 To m
bArray(i) = Application.InputBox("b" & i & "= ", "2nd polynomial coefficients")
Next i
For i = 0 To r
cArray(i) = Application.InputBox("c" & i & "= ", "3rd polynomial coefficients")
Next i
' Multiplying first two polynomials
p1 = n + m
For i = 0 To p1
For k = 0 To i
qArray(i) = qArray(i) + aArray(k) * bArray(i - k)
Next k
Next i
' Multiplying the result of the first two polynomials with the third one
p2 = p1 + r
For i = 0 To p2
For k = 0 To i
rArray(i) = rArray(i) + qArray(k) * cArray(i - k)
Next k
Next i
' Evaluating the resultant polynom for x0
i = 0
b = rArray(i)
Do
i = i + 1
b = b * xo + rArray(i)
Loop Until i = p2 + 1
' Output
Application.Worksheets("Sheet1").Cells(1, 1).Value = "Product polynomial maximum grade is:"
Application.Worksheets("Sheet1").Cells(1, 2).Value = p2
Application.Worksheets("Sheet1").Cells(2, 1).Value = "Product polynomial coefficients are:"
For i = 0 To p2
Application.Worksheets("Sheet1").Cells(2, 2 + i).Value = rArray(i)
Next i
Application.Worksheets("Sheet1").Cells(3, 1).Value = "Polynom value for x0 is:"
Application.Worksheets("Sheet1").Cells(3, 2).Value = b
End Sub
I'm using Microsoft Excel 2003 with Microsoft Visual Basic 6.5.
I have to multiply an arbitrary number of polynomials, say P1*P2*P3*...*Pn.
P1 = an + a(n-1)*X^1 + a(n-2)*X^2 + ...+ a0*x^i
.
.
.
Pn = ...
I have managed to write the code for 3 polynomials, but I'm stuck for n polynomials.
I think the Sub needs to work with a matrix, where each row is the coefficients of each polynomial. The user will type the
maximum grades and the coefficients in the worksheet. The program will read the matrix and the calculus will begin.
Help me with this.
Thank you!
Here is what I've worked so far:
Sub Product3Pol()
Dim n As Integer, m As Integer, r As Integer, xo As Integer
Dim p1 As Integer, p2 As Integer
Dim aArray(20) As Double, bArray(20) As Double, cArray(20) As Double, qArray(60) As Double, rArray(60) As Double
' Colecting user data:
n = Application.InputBox("The maxim grade for the first polynomial is?")
m = Application.InputBox("The maxim grade for the second polynomial is?")
r = Application.InputBox("The maxim grade for the third polynomial is?")
xo = Application.InputBox("x0 value is?")
For i = 0 To n
aArray(i) = Application.InputBox("a" & i & "= ", "1st polynomial coefficients")
Next i
For i = 0 To m
bArray(i) = Application.InputBox("b" & i & "= ", "2nd polynomial coefficients")
Next i
For i = 0 To r
cArray(i) = Application.InputBox("c" & i & "= ", "3rd polynomial coefficients")
Next i
' Multiplying first two polynomials
p1 = n + m
For i = 0 To p1
For k = 0 To i
qArray(i) = qArray(i) + aArray(k) * bArray(i - k)
Next k
Next i
' Multiplying the result of the first two polynomials with the third one
p2 = p1 + r
For i = 0 To p2
For k = 0 To i
rArray(i) = rArray(i) + qArray(k) * cArray(i - k)
Next k
Next i
' Evaluating the resultant polynom for x0
i = 0
b = rArray(i)
Do
i = i + 1
b = b * xo + rArray(i)
Loop Until i = p2 + 1
' Output
Application.Worksheets("Sheet1").Cells(1, 1).Value = "Product polynomial maximum grade is:"
Application.Worksheets("Sheet1").Cells(1, 2).Value = p2
Application.Worksheets("Sheet1").Cells(2, 1).Value = "Product polynomial coefficients are:"
For i = 0 To p2
Application.Worksheets("Sheet1").Cells(2, 2 + i).Value = rArray(i)
Next i
Application.Worksheets("Sheet1").Cells(3, 1).Value = "Polynom value for x0 is:"
Application.Worksheets("Sheet1").Cells(3, 2).Value = b
End Sub