Polynomial Solver

MikeyJ

New Member
Joined
Jul 5, 2016
Messages
5
Hi, I'm currently working on a UDF (user-defined function) in MS excel, which enables the user to determine what values of x satisfy an equation. I've seen code like this previously on websites such as https://newtonexcelbach.wordpress.com/2011/01/13/solving-higher-order-polynomials/ but the variables used in this code aren't explanatory and it is very difficult to track their method. So far, I have created a function where the user selects a 4x1 range containing 3 co-efficients and a constant (cubic), and inputs a value that they wish to be solved for. I have completed the quadratic and linear code, but the cubic code is not yet completed and creates errors in the presence of more than one root. How could I improve this code to find all solutions of a cubic equation, and even high-order polynomials such as quartics etc.?
Code:
Public Function solvePOLY(ByRef rng As Range, ByVal NumX As Double) As Variant


Dim CoEffA As Double, CoEffB As Double, CoEffC As Double, ConstD As Double
Dim ERRORmsgA As VbMsgBoxResult, ERRORmsgB As VbMsgBoxResult, ERRORmsgC As VbMsgBoxResult, ERRORmsgD As VbMsgBoxResult
Dim deltaCHNG As Double, NoSol As String, xINT(1 To 2) As Double
Dim pCubic As Double, qCubic As Double, rCubic As Double, xintCUBIC As Double, xintPORTION As Double


Application.Volatile


    If VBA.IsNumeric(rng.Cells(1, 1).Value) = True Then
    
        CoEffA = rng.Cells(1, 1).Value
        
    Else
    
        ERRORmsgA = MsgBox("The x³ co-efficient must be numeric", vbApplicationModal + vbCritical + vbOKOnly, "Error")
        End
        
    End If


    
    If VBA.IsNumeric(rng.Cells(1, 2).Value) = True Then
    
        CoEffB = rng.Cells(1, 2).Value
        
    Else
    
        ERRORmsgB = MsgBox("The x² co-efficient entry must be numeric", vbApplicationModal + vbCritical + vbOKOnly, "Error")
        End
        
    End If
    
    
    If VBA.IsNumeric(rng.Cells(1, 3).Value) = True Then
    
        CoEffC = rng.Cells(1, 3).Value
        
    Else
    
        ERRORmsgC = MsgBox("The x co-efficient entry must be numeric", vbApplicationModal + vbCritical + vbOKOnly, "Error")
        End
        
    End If




    If VBA.IsNumeric(rng.Cells(1, 4).Value) = True Then
    
        ConstD = rng.Cells(1, 4).Value
        
    Else
    
        ERRORmsgD = MsgBox("The constant entry must be numeric", vbApplicationModal + vbCritical + vbOKOnly, "Error")
        End
        
    End If


deltaCHNG = (CoEffC ^ 2) - (4 * CoEffB * (ConstD - NumX))
NoSol = "No Solutions"


    If rng.Cells(1, 1).Value = 0 Then
    
        If rng.Cells(1, 2).Value = 0 Then
            
            solvePOLY = Application.Transpose(Array((NumX - ConstD) / CoEffC, " ", " "))
                
        Else
        
            On Error GoTo NoSolutions
        
            xINT(1) = (-CoEffC - Sqr(deltaCHNG)) / (2 * CoEffB)
            xINT(2) = (-CoEffC + Sqr(deltaCHNG)) / (2 * CoEffB)
        
NoSolutions: solvePOLY = Application.Transpose(Array(NoSol, " ", " "))
        
        
            If deltaCHNG = 0 Then
                
                solvePOLY = Application.Transpose(Array(xINT(1), " ", " "))
                
            End If
        
        
            If deltaCHNG > 0 Then
        
                solvePOLY = Application.Transpose(Array(xINT(1), xINT(2), " "))
                
            End If
        
        End If
    
    Else
    
        pCubic = (-CoEffB) / (3 * CoEffA)
        qCubic = (pCubic ^ 3) + ((CoEffB * CoEffC) - (3 * CoEffA * (ConstD - NumX))) / (6 * (CoEffA ^ 2))
        rCubic = CoEffC / (3 * CoEffA)


        xintPORTION = Sqr((qCubic ^ 2) + ((rCubic - (pCubic ^ 2)) ^ 3))
        xintCUBIC = ((qCubic + xintPORTION) ^ (1 / 3)) + ((qCubic - xintPORTION) ^ (1 / 3)) + pCubic
        
            solvePOLY = Application.Transpose(Array(xintCUBIC, "", ""))
        
         [B]'Solve Cubic needs continuation[/B]
    
    End If


End Function
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top