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
