主题:[原创]一元高次方程求解(递归、降次、牛顿迭代)
老大徒伤悲
[专家分:29120] 发布于 2008-10-03 12:15:00
敬请大家多提宝贵意见。
回复列表 (共23个回复)
21 楼
老大徒伤悲 [专家分:29120] 发布于 2010-12-02 11:41:00
节点数 = 节点数 + 1
ReDim Preserve 节点(节点数)
节点(节点数) = 解
End If
Next i
End If
End Sub
Private Function 函数(系数() As Double, 阶数 As Integer, 自变量 As Double) As Double
Dim i As Integer
On Error GoTo 溢出
函数 = 1
For i = 阶数 - 1 To 0 Step -1
函数 = 函数 * 自变量 + 系数(i)
Next i
Exit Function
溢出:
MsgBox "你确定你的方程是有意义的?计算都溢出了!" & vbCrLf & "如果是,你就简化一下方程;" & vbCrLf & "如果不是,就别闹了。", vbOKOnly + vbExclamation, 标题 & "--系数溢出"
End
End Function
22 楼
老大徒伤悲 [专家分:29120] 发布于 2010-12-02 11:42:00
Private Sub Command1_Click()
Dim i As Integer, x0 As Double, 找到 As Boolean, y As Double, 小数位数 As Integer
Select Case Command1.Caption
Case "进入"
新方程
Case "确定"
On Error GoTo 溢出1
If CDbl(Text2(阶数)) = 0 Then
MsgBox "最高项的系数不得为零!" & vbCrLf & "要是为零了,方程的阶数就不是你说的数目了。", vbOKOnly + vbExclamation, 标题 & "--最高系数"
Exit Sub
End If
If Text1 = "" Then
MsgBox "你还没有确定允许误差呢!", vbOKOnly + vbExclamation, 标题 & "--误差丢失"
Exit Sub
End If
On Error GoTo 溢出2
允许误差 = CDbl(Text1)
If 允许误差 < 0.00000000000001 Then
MsgBox "你允许误差也太苛刻了吧!我不干。", vbOKOnly + vbExclamation, 标题 & "--误差过细"
Exit Sub
ElseIf 允许误差 > 1 Then
MsgBox "这么的大误差!你也太搞笑了。", vbOKOnly + vbExclamation, 标题 & "--误差太大"
Exit Sub
End If
If 正式方程 <> "" Then Exit Sub
Command1.Caption = "方程确认"
Case "方程确认"
Dim 解集() As Double, 解数 As Integer
For i = 0 To 阶数 - 1
系数(i) = 系数(i) / 系数(阶数)
Next i
系数(阶数) = 1
分段 系数(), 阶数, 解集(), 解数
Me.ForeColor = vbRed
If 解数 = -1 Then
CurrentX = 20 * 字号
Print "此方程无解!"
Else
小数位数 = -Int(Log(允许误差) / Log(10))
For i = 0 To 解数
CurrentX = 20 * 字号
Print "x";
FontSize = 字号 / 2
CurrentY = CurrentY + 10 * 字号
Print Trim(Str(i));
FontSize = 字号
CurrentY = CurrentY - 10 * 字号
Print "="; Round(解集(i), 小数位数)
Next i
End If
Command1.Enabled = False
Command2.Default = True
高 = CurrentY + 2200
Form_Resize
End Select
Exit Sub
溢出1:
MsgBox "你输入的系数超出了我的想象能力。" & vbCrLf & "我以为我最强呢,真没有想到……", vbOKOnly + vbExclamation, 标题 & "--系数溢出"
Text2(阶数) = ""
Text2(阶数).SetFocus
Exit Sub
溢出2:
MsgBox "你也真能瞎掰,这样的误差限能干啥?。" & vbCrLf & "好好填写!", vbOKOnly + vbExclamation, 标题 & "--误差溢出"
Text1 = ""
Text1.SetFocus
End Sub
23 楼
老大徒伤悲 [专家分:29120] 发布于 2010-12-02 11:42:00
Private Sub Command2_Click()
新方程
Command1.Enabled = True
Command1.Default = True
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Dim s As String
AutoRedraw = True
Me.Caption = 标题
Width = 宽
高 = 6000
Print
Print
FontSize = 字号 / 2 * 3
ForeColor = vbBlue
FontName = "楷体_gb2312"
s = "欢迎使用"
CurrentX = (Width - TextWidth(s)) / 2
Print s
ForeColor = vbRed
FontSize = 字号
s = "“" & 标题 & "”求解系统"
CurrentX = (Width - TextWidth(s)) / 2
Print s
Label4.Move 2000, 2000, 11200, 3000
Label4.FontSize = 字号 / 2
Label4 = "说明:"
Label4 = Label4 & vbCrLf & " 本程序用于求解一元任意次方程。"
Label4 = Label4 & vbCrLf & vbCrLf & "使用方法:"
Label4 = Label4 & vbCrLf & " 首先在对话框中输入方程未知数的最高次数,然后在方程各项的系数位置输入系数。确定后程序给出正式的方程式。"
Label4 = Label4 & vbCrLf & " 对正式方程进行核对,如有错误,可以返回更改;没有错误确认后,即可由程序自动完成求解,并给出结果。"
Text1.Visible = False
Text2(0).Visible = False
Text2(1).Visible = False
Label1.Visible = False
Label1 = "允 许 误 差" & vbCrLf & "(建议:>0.0000001)"
Text1 = "0.000000001"
Command1.Caption = "进入"
Command1.Default = True
Command2.Caption = "新方程"
Command2.Visible = False
Command3.Caption = "退出"
End Sub
Private Sub Form_Resize()
Dim 顶 As Single
Width = 宽
Height = 高
Move (Screen.Width - Width) / 2, (Screen.Height - Height) / 3
顶 = 高 - 1500
Label1.Move 500, 顶, 2000, 500
Text1.Move 2100, 顶, 1500, 300
Command1.Move 8000, 顶, 1500, 500
Command2.Move 9800, 顶, 1500, 500
Command3.Move 11600, 顶, 1500, 500
End Sub
Private Sub Text2_LostFocus(Index As Integer)
Dim d As Double
On Error GoTo 溢出:
d = Val(Text2(Index))
If Abs(d) > 1E+100 Then
MsgBox "系数嘛,不要太离谱。差不多就得了", vbOKOnly + vbExclamation, 标题
Text2(Index) = ""
Text2(Index).SetFocus
End If
Exit Sub
溢出:
MsgBox "你输入的系数超出了我的想象能力。" & vbCrLf & "我以为我最强呢,真没有想到……", vbOKOnly + vbExclamation, 标题 & "--系数溢出"
Text2(Index) = ""
Text2(Index).SetFocus
End Sub
我来回复