Решение дифференциального уравнения и построение графиков - VB
Формулировка задачи:
Здраствуйте, помогите пожалуста выявить в чем проблема. Программа запускается но выдает ошибку run-time error '9'; Subscript out of range. Выделяет 20 строку.
Решение задачи: «Решение дифференциального уравнения и построение графиков»
textual
Листинг программы
Dim x() As Single Dim y() As Single Dim g() As Single Dim t() As Single Private n, i As Integer Private xk, x0, kx, ky, h As Single Private k, k1, k2, k3, k4 As Single Private max, min, y0 As Single Private Function f(x, y As Single) As Single f = (x - y) / x End Function Private Function v(x As Single) As Single v = x0 * (c - Log(x0)) End Function Private Sub EilerMod() ReDim x(n), y(n) y(0) = y0 For i = 0 To n - 1 x(i) = x0 + h * i y(i + 1) = y(i) + h * f(x(i) + h / 2, y(i) + h / 2 * f(x(i), y(i))) MSFlexGrid1.TextMatrix(1, 0) = Str(x0) MSFlexGrid1.TextMatrix(i + 2, 0) = Str(x(i + 1)) MSFlexGrid1.TextMatrix(1, 1) = Str(y0) MSFlexGrid1.TextMatrix(i + 2, 1) = Str(y(i + 1)) Next i End Sub Private Sub Rynge_kytt() ReDim g(n) g(0) = y0 For i = 0 To n - 1 k1 = h * f(x(i), g(i)) k2 = h * f(x(i) + h / 2, g(i) + k1 / 2) k3 = h * f(x(i) + h / 2, g(i) + k2 / 2) k4 = h * f(x(i) + h, g(i) + k3) k = (k1 + 2 * k2 + 2 * k3 + k4) / 6 g(i + 1) = g(i) + k MSFlexGrid1.TextMatrix(i + 2, 2) = Str(g(i + 1)) MSFlexGrid1.TextMatrix(1, 2) = Str(g(0)) Next i End Sub Private Sub obshee() ReDim t(n) c = y0 / x0 + Log(x0) For i = 0 To n t(0) = y0 x(i) = x0 + h * i t(i) = v(x(i)) MSFlexGrid1.TextMatrix(i + 1, 3) = Str(t(i)) Next i End Sub Private Sub Command1_Click() x0 = Val(Text1.Text) xk = Val(Text2.Text) h = Val(Text3.Text) y0 = Val(Text4.Text) n = (xk - x0) / h MSFlexGrid1.Rows = n + 2 MSFlexGrid1.Cols = 4 MSFlexGrid1.TextMatrix(0, 0) = "x" MSFlexGrid1.TextMatrix(0, 1) = "EilerMod" MSFlexGrid1.TextMatrix(0, 2) = "Рунге-Кутт" MSFlexGrid1.TextMatrix(0, 3) = "obshee" Label5.Caption = Str(x0) Label6.Caption = Str(xk) EilerMod Rynge_kytt obshee max = y0 min = y0 For i = 0 To n If y(i) > max Then max = y(i) End If If y(i) < min Then min = y(i) End If If g(i) > max Then max = g(i) End If If g(i) < min Then min = g(i) End If If t(i) > max Then max = t(i) End If If t(i) < min Then min = t(i) End If Next i Label7.Caption = Str(min) Label8.Caption = Str(max) kx = (Picture1.Width - 1095) / (xk - x0) ky = (Picture1.Height - 1320) / (max - min) Picture1.Cls For i = 0 To n - 1 X1 = 1320 + Round(kx * (x(i - 1) - x0)) X2 = 1320 + Round(kx * (x(i) - x0)) Y1 = 6255 + Round(ky * (y(i - 1) - min)) Y2 = 6255 + Round(ky * (y(i) - min)) Picture1.Line (X1, Y1)-(X2, Y2), RGB(100, 200, 200) X1 = 1320 + Round(kx * (x(i - 1) - x0)) X2 = 1320 + Round(kx * (x(i) - x0)) Y1 = 6255 + Round(ky * (g(i - 1) - min)) Y2 = 6255 + Round(ky * (g(i) - min)) Picture1.Line (X1, Y1)-(X2, Y2), RGB(400, 300, 300) X1 = 1320 + Round(kx * (x(i - 1) - x0)) X2 = 1320 + Round(kx * (x(i) - x0)) Y1 = 6255 + Round(ky * (t(i - 1) - min)) Y2 = 6255 + Round(ky * (t(i) - min)) Picture1.Line (X1, Y1)-(X2, Y2), RGB(500, 100, 100) Next i End Sub
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д