数值分析报告 (李庆扬版)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《数值分析》作业
学院:机械学院
专业:机械工程
姓名:赵博
学号:2014520024
日期:2015年6月29日
第二章作业
问:用线性插值及二次插值计算ln0.54的近似值。
答:VB程序如下:
Option Explicit
Sub czfl(ByRef x() As Single, y() As Single, n As Integer, x1 As Double, f As Double) Dim i, j As Integer
Dim p As Single
Dim appexcel As Object
Dim wbmybook As Object
Dim wsmysheet As Object
Set appexcel = CreateObject("excel.application")
Set wbmybook = appexcel.workbooks.Add
Set wsmysheet = appexcel.worksheets.Add
f = 0
For i = 0 To n
p = 1
For j = 0 To n
If i <> j Then
p = p * (x1 - x(j)) / (x(i) - x(j))
End If
Next j
wsmysheet.cells(i + 1, 1) = Str(p)
wsmysheet.cells(i + 1, 2) = Str(p * y(i))
f = f + p * y(i)
Next i
wsmysheet.cells(n + 1, 3) = "最终结果" + Str(f)
appexcel.Visible = True
End Sub
Private Sub Command1_Click(Index As Integer)
Dim x() As Single
Dim y() As Single
Dim n As Integer
Dim k As Integer
Dim f As Double
k = 0
n = V al(Text1.Text)
ReDim x(n)
ReDim y(n)
Open "czfl.txt" For Output As #1
Print #1, Text3.Text
Close #1
Open "czfl.txt" For Input As #1
Do While Not EOF(1)
Input #1, x(k), y(k)
'Print x(k), y(k)
k = k + 1
Loop
Close #1
Select Case Index
Case 0
Call czfl(x(), y(), Text1.Text, Text2.Text, f)
Case 1
Call czfnt(x(), y(), Text1.Text, Text2.Text, f)
End Select
End Sub
Sub czfnt(ByRef x() As Single, y() As Single, n As Integer, x1 As Double, f As Double)
Dim i, j As Integer
Dim k, l As Integer
Dim s As Double
Dim appexcel As Object
Dim wbmybook As Object
Dim wsmysheet As Object
Set appexcel = CreateObject("excel.application")
Set wbmybook = appexcel.workbooks.Add
Set wsmysheet = appexcel.worksheets.Add
f = y(0)
s = 1
For j = 1 To n
For i = n To j Step -1
y(i) = (y(i) - y(i - 1)) / (x(i) - x(i - j))
wsmysheet.cells(i, j) = Str(y(i))
Next i
Next j
For k = 1 To n
s = s * (x1 - x(l))
f = f + s * y(k)
Next k
wsmysheet.cells(n, n + 1) = "最终结果" + Str(f)
appexcel.Visible = True
End Sub
结果:
1.线性插值
0.6 -0.41589
0.4 -0.20433 最终结果-.620218614056672 2.二次插值
-0.12 0.109955
0.84 -0.58224
0.28 -0.14303 最终结果-.615319814966043
第三章作业
求运动方程。
答:vb程序如下:(窗体见工程文件)Option Explicit
Option Base 1
Sub lufj(n As Integer, a() As Single, b() As Single) Dim i As Integer, j As Integer, k As Integer
Dim s As Single
For i = 2 To n
a(i, 1) = a(i, 1) / a(1, 1)
Next i
For i = 2 To n
For j = i To n
s = 0
For k = 1 To i - 1
s = s + a(i, k) * a(k, j)
Next k
a(i, j) = a(i, j) - s
Next j
For j = i + 1 To n
s = 0
For k = 1 To i - 1
s = s + a(j, k) * a(k, i)
Next k
a(j, i) = (a(j, i) - s) / a(i, i)
Next j
Next i
For i = 1 To n
s = 0
For j = 1 To i - 1
s = s + a(i, j) * b(j)
Next j
b(i) = b(i) - s
Next i
For i = n To 1 Step -1
s = 0
For j = i + 1 To n