Private Sub cmdcalculate_Click() Dim x(100), y(100), yprime(100), step Call getarray(x, y, yprime, length, step) Call derivative(x, y, yprime, length, step) Call writearray(x, y, yprime, length) 'used just to test if derivate sub is working xval = Val(txtx.Text) j = 0 For i = 0 To length - 1 If xval = x(i) Then lbloutputyprime.Caption = Format(yprime(i), "0.0000") lbloutputy.Caption = Format(y(i), "0.0000") End If j = j + 1 Next i If j = length Then Call interpolate(x, y, yprime, length, step, xval) End Sub Private Sub cmdquit_Click() End End Sub Sub getarray(x, y, yprime, length, step) CommonDialog1.ShowOpen file = CommonDialog1.FileName Open file For Input As #1 length = 0 Do While Not EOF(1) Input #1, x(length), y(length) length = length + 1 Loop step = (x(length - 1) - x(0)) / (length - 1) Close #1 End Sub Sub derivative(x, y, yprime, length, step) For i = 0 To length - 1 If i < 2 Then yprime(i) = (-y(i + 2) + 4 * y(i + 1) - 3 * y(i)) / (2 * step) ElseIf i > 1 And i < (length - 2) Then yprime(i) = (-y(i + 2) + 8 * y(i + 1) - 8 * y(i - 1) + y(i - 2)) / (12 * step) ElseIf i > (length - 3) Then yprime(i) = (3 * y(i) - 4 * y(i - 1) + y(i - 2)) / (2 * step) End If Next i End Sub Sub interpolate(x, y, yprime, length, step, xval) For i = 0 To length - 3 If xval > x(i) And xval < x(i + 1) Then p = (xval - x(i)) / step yinterpolate = y(i) + p * (y(i + 1) - y(i)) + (p * (p - 1) / 2) * (y(i + 2) - 2 * y(i + 1) + y(i)) yprimeinterpolate = yprime(i) + p * (yprime(i + 1) - yprime(i)) + (p * (p - 1) / 2) * (yprime(i + 2) - 2 * yprime(i + 1) + yprime(i)) lbloutputyprime.Caption = Format(yprimeinterpolate, "0.0000") lbloutputy.Caption = Format(yinterpolate, "0.0000") End If Next i For i = length - 2 To length - 1 If xval > x(i) And xval < x(i + 1) Then p = (xval - x(i)) / step yinterpolate = y(i) + p * (y(i) - y(i - 1)) + (p * (p + 1) / 2) * (y(i) - 2 * y(i - 1) + y(i - 2)) yprimeinterpolate = yprime(i) + p * (yprime(i) - yprime(i - 1)) + (p * (p + 1) / 2) * (yprime(i) - 2 * yprime(i - 1) + yprime(i - 2)) lbloutputyprime.Caption = Format(yprimeinterpolate, "0.0000") lbloutputy.Caption = Format(yinterpolate, "0.0000") End If Next i End Sub Sub writearray(x, y, yprime, length) CommonDialog1.ShowSave file = CommonDialog1.FileName Open file For Output As #1 For i = 0 To length - 1 Print #1, x(i), y(i), yprime(i) Next i Close #1 End Sub