Evaluate Polynomial

Public Function Evaluate_Polynomial(RngExponents As Range, RngCoefficients As Range, RngX As Range) As Variant
Dim LngLoopCells As Long
Dim LngCountCells As Long
Dim DblSum As Double

‘====================================================================================================
‘Perform the necessary checks on the cells that contain the EXPONENTS
‘====================================================================================================
If TypeName(RngExponents) <> “Range” Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

If RngExponents Is Nothing Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

If RngExponents.Areas.Count <> 1 Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

‘====================================================================================================
‘Perform the necessary checks on the cells that contain the COEFFICIENTS
‘====================================================================================================
If TypeName(RngCoefficients) <> “Range” Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

If RngCoefficients Is Nothing Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

If RngCoefficients.Areas.Count <> 1 Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

‘====================================================================================================
‘Make sure that the number of COEFFICIENTS matches the number of EXPONENTS
‘====================================================================================================
If RngExponents.Cells.Count <> RngCoefficients.Cells.Count Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

If TypeName(RngX) <> “Range” Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

If RngX Is Nothing Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

‘====================================================================================================
‘Perform the necessary checks on the X-value
‘====================================================================================================
If RngX.Areas.Count <> 1 Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

If RngX.Cells.Count <> 1 Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

If Not IsNumeric(RngX) Then
——–Polynomial = “UNDEFINED”
——–Exit Function
End If

‘====================================================================================================
‘Determine the number of terms in the polynomial
‘====================================================================================================
LngCountCells = RngExponents.Cells.Count

‘====================================================================================================
‘Loop through the coefficient and exponent pairs and multiply accordingly summing up on each iteration
‘====================================================================================================
For LngLoopCells = 1 To LngCountCells
——–DblSum = DblSum + RngCoefficients.Cells(LngLoopCells).Value * (RngX.Value ^ RngExponents.Cells(LngLoopCells).Value)
Next LngLoopCells

‘====================================================================================================
‘Return the result
‘====================================================================================================
Evaluate_Polynomial = DblSum

End Function

  

Advertisement

~ by userdefinedfunctionsinvba on October 12, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.