一元三次方程求解

原创 2007年09月11日 23:29:00

Sub SolveCubicEquations(ByVal CubicEquation As String, Optional ByVal x As String = "x", Optional ByRef result As String)
Dim a As Single, b As Single, c As Single, d As Single, temp As String, n As Byte
Dim f As Single, g As Single, h As Single, i As Single, j As Single, alpha As Single
CubicEquation = Replace(CubicEquation, " ", "")
result = Replace(CubicEquation, "-", "+-")
s = Split(Split(result, "=")(0), "+")
For n = 0 To UBound(s)
If s(n) Like "*" & x & "^3" Then temp = Trim(Split(s(n), x)(0)): a = IIf(temp = "-", -1, IIf(temp = "", 1, Val(temp)))
If s(n) Like "*" & x & "^2" Then temp = Trim(Split(s(n), x)(0)): b = IIf(temp = "-", -1, IIf(temp = "", 1, Val(temp)))
If s(n) Like "*" & x Then temp = Trim(Split(s(n), x)(0)): c = IIf(temp = "-", -1, IIf(temp = "", 0, Val(temp)))
If IsNumeric(s(n)) Then d = s(n)
Next
f = c / a - b * b / (3 * a * a)
g = 2 * b ^ 3 / (3 * a) ^ 3 - b * c / (3 * a * a) + d / a
h = g ^ 2 / 4 + f ^ 3 / 27
Select Case Sgn(h)
Case -1 'Roots Are Real
i = Sqr(g ^ 2 / 4 - h)
j = -g / (2 * i)
If j = 1 Then alpha = 0
If j <> 1 Then alpha = (Atn(-j / Sqr(1 - j ^ 2)) + 2 * Atn(1)) / 3
result = "Cubic Equations {" & CubicEquation & "} has 3 Real Roots:" & vbCrLf & String(50, "-")
result = result & vbCrLf & x & "1=" & Format(2 * i ^ (1 / 3) * Cos(alpha) - b / (3 * a), "0.0000")
result = result & vbCrLf & x & "2=" & Format(-i ^ (1 / 3) * (Cos(alpha) + (3 ^ 0.5) * Sin(alpha)) - b / (3 * a), "0.0000")
result = result & vbCrLf & x & "3=" & Format(-i ^ (1 / 3) * (Cos(alpha) - (3 ^ 0.5) * Sin(alpha)) - b / (3 * a), "0.0000")
Case 0 'All 3 Roots Are Real and Equal
result = "Cubic Equation {" & CubicEquation & "} has 3 Equal Real Roots:" & vbCrLf & String(50, "-")
result = result & vbCrLf & x & "1=" & Format(-(d / a) ^ (1 / 3), "0.0000")
result = result & vbCrLf & x & "2=" & Format(-(d / a) ^ (1 / 3), "0.0000")
result = result & vbCrLf & x & "3=" & Format(-(d / a) ^ (1 / 3), "0.0000")
Case 1 'Only 1 Root Is Real
i = (-g / 2 + h ^ 0.5) ^ (1 / 3)
j = -(g / 2 + h ^ 0.5) ^ (1 / 3)
result = "Cubic Equations {" & CubicEquation & "} has only 1 Real Roots:" & vbCrLf & String(50, "-")
result = result & vbCrLf & x & "1=" & Format(i + j - b / (3 * a), "0.0000")
result = result & vbCrLf & x & "2=" & Format(-(i + j) / 2 - b / (3 * a), "0.0000") & "+" & Format(Abs(i - j) * 3 ^ 0.5 / 2, "0.0000") & "*i"
result = result & vbCrLf & x & "3=" & Format(-(i + j) / 2 - b / (3 * a), "0.0000") & "-" & Format(Abs(i - j) * 3 ^ 0.5 / 2, "0.0000") & "*i"
End Select
result = Replace(result, "0.0000+", "")
result = Replace(result, "0.0000-", "")
result = Replace(result, "0.0000", 0)
result = Replace(result, ".0000", "")
result = result & vbCrLf
Debug.Print result

End Sub

Sub macro1()
SolveCubicEquations "2x^3-4x^2-22x+24=0"
SolveCubicEquations "x^3   + 6x^2   + 12x + 8 = 0"
SolveCubicEquations "y^3   + 7y -9 = 0", "y"
SolveCubicEquations "3z^3   + 5z  = 0", "z"
SolveCubicEquations "-2x^3   + 8x^2  = 0", "x"
End Sub

返回:

Cubic Equations {2x^3-4x^2-22x+24=0} has 3 Real Roots:
--------------------------------------------------
x1=4
x2=-3
x3=1

Cubic Equation {x^3+6x^2+12x+8=0} has 3 Equal Real Roots:
--------------------------------------------------
x1=-2
x2=-2
x3=-2

Cubic Equations {y^3+7y-9=0} has only 1 Real Roots:
--------------------------------------------------
y1=1.0971
y2=-0.5485+2.8112*i
y3=-0.5485-2.8112*i

Cubic Equations {3z^3+5z=0} has only 1 Real Roots:
--------------------------------------------------
z1=0
z2=1.2910*i
z3=1.2910*i

Cubic Equations {-2x^3+8x^2=0} has 3 Real Roots:
--------------------------------------------------
x1=4
x2=0
x3=0

 

【openjudge】一元三次方程求解(分治算法)

一元三次方程求解总时间限制: 1000ms 内存限制: 65536kB 描述 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程。给出该方程中各项的系数(a,b,c,d 均为实数),...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

C++求解一元三次方程

#include #include #include using namespace std; double fx(double nparam[3],double x) { return pow(x...

【Openjudge:Noi】7891:一元三次方程求解 c++

【Openjudge:Noi】7891:一元三次方程求解 总时间限制:  1000ms  内存限制:  65536kB 描述 有形如:ax3+bx2+cx+d=0  这样的一...

一元三次方程 (codevs 1038)题解

【问题描述】      有形如:ax3+bx2+cx+d=0这样的一个一元三次方程。给出该方程中各项的系数(a,b,c,d均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之间),...
  • PbTfcLx
  • PbTfcLx
  • 2016年01月10日 18:40
  • 792

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

vijos p1116(codevs 1038 )一元三次方程求解 题解

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 这道题是一道明显的二分题。 有题可知精度为0.01,且三个解均在-100...

一元三次方程求解c++实现

typedef  double Number; class CubicRealPolynomial { public:     static Number computeDiscriminan...
  • zg260
  • zg260
  • 2014年11月21日 15:36
  • 1376

某叶C语言学习上重大的一步——一元三次方程求解

目前某叶编的最难的程序了......感觉算是跨越吧,之前最难的是一元二次方程求解,虽然是最“难”的,只是因为最长,但是写起来还是很轻松的   不过一元三次方程可一点都不轻松,很累,因为没学过一元三次方...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:一元三次方程求解
举报原因:
原因补充:

(最多只允许输入30个字)