内容发布更新时间 : 2024/12/27 23:21:28星期一 下面是文章的全部内容请认真阅读。
《数值分析》作业
学 院:专 业:姓 名:学 号:日 期:
机械学院 机械工程 赵 博 2014520024 2015年6月29日1
第二章作业
P48_2给出f(x)?lnx的数值表如下: x lnx 0.4 -0.916291 0.5 -0.693147 0.6 -0.510826 0.7 -0.356675 0.8 -0.223144 问:用线性插值及二次插值计算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(\ 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) = \最终结果\ appexcel.Visible = True End Sub
Private Sub Command1_Click(Index As Integer) Dim x() As Single
2
Dim y() As Single Dim n As Integer Dim k As Integer Dim f As Double k = 0
n = Val(Text1.Text) ReDim x(n) ReDim y(n)
Open \ Print #1, Text3.Text Close #1
Open \ 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
3