уже которую неделю бьюсь как реализовать и на VB и на QBasic'e но четно, может у кого есть в закромах исходничек или я могу кинуть код, может посмотрите?а?
система из 3-х уравнений
cooler писал(а):че-то припоминаю..слау, метод гаусса, крамера, мпи... на 1-м курсе было.. правда делали на фортране... лучше наверно кинь сюда код, если вспомню или найду те исходники - помогу
DEFINT I-J, N: DIM a(3, 4), x(3), x1(3)
CLS
PRINT "VVEDITE CHEREZ ZAPJATUJU 4ISLO URAVNENII n"
PRINT "I TO4NOST' epsilon"
INPUT N, E
PRINT "VVEDITE raswirennuju MARIX PO ODNOMY ELEMENTY"
PRINT "(STROKAM)"
FOR i = 1 TO N
FOR j = 1 TO N + 1
INPUT ; a(i, j): PRINT " ";
NEXT j: PRINT
NEXT i: GOSUB 520
FOR i = 1 TO N
IF a(i, i) <> 0 THEN 170
PRINT : PRINT "system REWAETCIA DRUGIM METHODOM"
GOSUB 580: GOTO 510
170
NEXT i
d2 = 0
FOR i = 1 TO N
s = a(i, i): a(i, i) = 0: a(i, N + 1) = a(i, N + 1) / s
x(i) = a(i, N + 1): x1(1) = 0
FOR j = 1 TO N
IF j = i THEN 270
a(i, j) = -a(i, j) / s: x1(i) = x1(i) + ABS(a(i, j))
d2 = d2 + ABS(a(i, j)) * ABS(a(i, j))
270
NEXT j
NEXT i
d2 = SQR(d2)
GOSUB 520
GOSUB 830: d = y
FOR i = 1 TO N
x1(i) = 0
FOR j = 1 TO N
IF j <> 1 THEN x1(i) = x1(i) + ABS(a(j, i))
NEXT j
NEXT i
GOSUB 830: d1 = y
PRINT : PRINT USING "NORMI ={#.##; #.##; #.##}"; d; d1; d2
IF d1 < d THEN d = d1
IF d2 < d THEN d = d2
PRINT "NAIMENWAJA NORMA ="; d
IF d < 1 THEN 470
GOSUB 490
GOSUB 580: GOTO 510
470
PRINT "USLOVIE SXODIMOSTI METODOM UDOVLETVORENO": GOSUB 580
E = d / (1 - d)
490
GOSUB 610: GOSUB 680: GOSUB 730: GOSUB 580
IF d * s > E THEN GOSUB 790: GOTO 490
510
END
520
CLS : FOR i = 1 TO N
FOR j = 1 TO N + 1
PRINT USING " ##.###^^^^"; a(i, j);
NEXT j: PRINT
NEXT i: GOSUB 580
RETURN
580
PRINT : PRINT "DLJA PRODOLGENIJA PROGRAMMI PRESS ANY KEY..."
590
IF INKEY$ = "" THEN 590
RETURN
610
FOR i = 1 TO N
x1(i) = a(i, N + 1)
FOR j = 1 TO N
IF j <> i THEN x1(i) = x1(i) + a(i, j) * x(j)
NEXT j
NEXT i
RETURN
680
d = ABS(x(1) - x1(1))
FOR i = 2 TO N
IF ABS(x(i) - x1(i)) > d THEN d = ABS(x(i) - x1(i))
NEXT i
RETURN
730
CLS
FOR i = 1 TO N
PRINT "x"; i; "="; x1(i)
NEXT i
PRINT "RASSTOJANIE MEJDU ITERACIJAMI PO m-NORME = "; d
RETURN
790
FOR i = 1 TO N
x(i) = x1(i)
NEXT i
RETURN
830
y = ABS(x1(1))
FOR i = 2 TO N
IF ABS(x1(i)) > y THEN y = ABS(x1(i))
NEXT i
RETURN
GSerg писал(а):В поиск.
На форуме выложено. Давно и много.
Const l = 9 '////////////TMP!!!! 9
Private Type MatriceA
MatriceA(1 To l, 0 To l + 1)
End Type
Private Type VectorX
VectorX(1 To l)
End Type
Dim i%, j%, n%
Dim d, d1, d2, e, s
Dim str As String
Dim A As MatriceA
Dim x As VectorX
Dim x1 As VectorX
'<sub-procedures.SetBufferUpdater code begin>
Function SetBufferUpdater(m, b As Boolean) As String
If b = True Then
str = str & " " & m
SetBufferUpdater = str
Else
str = ""
End If
End Function
Sub outputOfA()
For i = 1 To n: For j = 1 To n + 1
kl = SetBufferUpdater(A.MatriceA(i, j), True)
Next j
Debug.Print kl
kl = SetBufferUpdater("levin", False)
Next i
End Sub
'</sub-procedures.OutputOfA code begin>
Function outputOfVector()
For i = 1 To n
Debug.Print "x" & i & " = " & x1.VectorX(i)
Next i
Debug.Print "ðàññòîÿíèå ìåæäó èòåðàöèÿìè ïî m-íîðìå = " & d
End Function
Function changeXbyX1()
For i = 1 To n
x.VectorX(i) = x1.VectorX(i)
Next i
End Function
Function VectorOfInteration()
For i = 1 To n
x1.VectorX(i) = A(i, n + 1)
For j = 1 To n
If j <> 1 Then
x1.VectorX(i) = x1.VectorX(i) + A.MatriceA(i, j) * x.VectorX(j)
End If
End Function
Function normlm(abba As Boolean)
Dim y
If abba = True Then
y = Abs(x1.VectorX(1))
For i = 1 To n
If Abs(x1.VectorX(i)) > y Then
y = Abs(x1.VectorX(i))
End If
Next i
Else
y = Abs(x.VectorX(1))
For i = 1 To n
If Abs(x.VectorX(i)) > y Then
y = Abs(x.VectorX(i))
End If
Next i
End If
normlm = y
End Function
Private Sub Command2_Click()
k = Text1.Text
n = 9
A.MatriceA(1, 1) = Val(m11.Text)
A.MatriceA(1, 2) = Val(m12.Text)
A.MatriceA(1, 3) = Val(m13.Text)
A.MatriceA(1, 4) = Val(m14.Text)
A.MatriceA(1, 5) = Val(m15.Text)
A.MatriceA(1, 6) = Val(m16.Text)
A.MatriceA(1, 7) = Val(m17.Text)
A.MatriceA(1, 8) = Val(m18.Text)
A.MatriceA(1, 9) = Val(m19.Text)
A.MatriceA(1, 10) = Val(m110.Text)
A.MatriceA(2, 1) = Val(m21.Text)
A.MatriceA(2, 2) = Val(m22.Text)
A.MatriceA(2, 3) = Val(m23.Text)
A.MatriceA(2, 4) = Val(m24.Text)
A.MatriceA(2, 5) = Val(m25.Text)
A.MatriceA(2, 6) = Val(m26.Text)
A.MatriceA(2, 7) = Val(m27.Text)
A.MatriceA(2, 8) = Val(m28.Text)
A.MatriceA(2, 9) = Val(m29.Text)
A.MatriceA(2, 10) = Val(m210.Text)
A.MatriceA(3, 1) = Val(m31.Text)
A.MatriceA(3, 2) = Val(m32.Text)
A.MatriceA(3, 3) = Val(m33.Text)
A.MatriceA(3, 4) = Val(m34.Text)
A.MatriceA(3, 5) = Val(m35.Text)
A.MatriceA(3, 6) = Val(m36.Text)
A.MatriceA(3, 7) = Val(m37.Text)
A.MatriceA(3, 8) = Val(m38.Text)
A.MatriceA(3, 9) = Val(m39.Text)
A.MatriceA(3, 10) = Val(m310.Text)
A.MatriceA(4, 1) = Val(m41.Text)
A.MatriceA(4, 2) = Val(m42.Text)
A.MatriceA(4, 3) = Val(m43.Text)
A.MatriceA(4, 4) = Val(m44.Text)
A.MatriceA(4, 5) = Val(m45.Text)
A.MatriceA(4, 6) = Val(m46.Text)
A.MatriceA(4, 7) = Val(m47.Text)
A.MatriceA(4, 8) = Val(m48.Text)
A.MatriceA(4, 9) = Val(m49.Text)
A.MatriceA(4, 10) = Val(m410.Text)
A.MatriceA(5, 1) = Val(m51.Text)
A.MatriceA(5, 2) = Val(m52.Text)
A.MatriceA(5, 3) = Val(m53.Text)
A.MatriceA(5, 4) = Val(m54.Text)
A.MatriceA(5, 5) = Val(m55.Text)
A.MatriceA(5, 6) = Val(m56.Text)
A.MatriceA(5, 7) = Val(m57.Text)
A.MatriceA(5, 8) = Val(m58.Text)
A.MatriceA(5, 9) = Val(m59.Text)
A.MatriceA(5, 10) = Val(m510.Text)
A.MatriceA(6, 1) = Val(m61.Text)
A.MatriceA(6, 2) = Val(m62.Text)
A.MatriceA(6, 3) = Val(m63.Text)
A.MatriceA(6, 4) = Val(m64.Text)
A.MatriceA(6, 5) = Val(m65.Text)
A.MatriceA(6, 6) = Val(m66.Text)
A.MatriceA(6, 7) = Val(m67.Text)
A.MatriceA(6, 8) = Val(m68.Text)
A.MatriceA(6, 9) = Val(m69.Text)
A.MatriceA(6, 10) = Val(m610.Text)
A.MatriceA(7, 1) = Val(m71.Text)
A.MatriceA(7, 2) = Val(m72.Text)
A.MatriceA(7, 3) = Val(m73.Text)
A.MatriceA(7, 4) = Val(m74.Text)
A.MatriceA(7, 5) = Val(m75.Text)
A.MatriceA(7, 6) = Val(m76.Text)
A.MatriceA(7, 7) = Val(m77.Text)
A.MatriceA(7, 8) = Val(m78.Text)
A.MatriceA(7, 9) = Val(m79.Text)
A.MatriceA(7, 10) = Val(m710.Text)
A.MatriceA(8, 1) = Val(m81.Text)
A.MatriceA(8, 2) = Val(m82.Text)
A.MatriceA(8, 3) = Val(m83.Text)
A.MatriceA(8, 4) = Val(m84.Text)
A.MatriceA(8, 5) = Val(m85.Text)
A.MatriceA(8, 6) = Val(m86.Text)
A.MatriceA(8, 7) = Val(m87.Text)
A.MatriceA(8, 8) = Val(m88.Text)
A.MatriceA(8, 9) = Val(m89.Text)
A.MatriceA(8, 10) = Val(m810.Text)
A.MatriceA(9, 1) = Val(m91.Text)
A.MatriceA(9, 2) = Val(m92.Text)
A.MatriceA(9, 3) = Val(m93.Text)
A.MatriceA(9, 4) = Val(m94.Text)
A.MatriceA(9, 5) = Val(m95.Text)
A.MatriceA(9, 6) = Val(m96.Text)
A.MatriceA(9, 7) = Val(m97.Text)
A.MatriceA(9, 8) = Val(m98.Text)
A.MatriceA(9, 9) = Val(m99.Text)
A.MatriceA(9, 10) = Val(m910.Text)
'A.MatriceA(10, 1) = Val(m101.Text)
'A.MatriceA(10, 2) = Val(m102.Text)
'A.MatriceA(10, 3) = Val(m103.Text)
'A.MatriceA(10, 4) = Val(m104.Text)
'A.MatriceA(10, 5) = Val(m105.Text)
'A.MatriceA(10, 6) = Val(m106.Text)
'A.MatriceA(10, 7) = Val(m107.Text)
'A.MatriceA(10, 8) = Val(m108.Text)
'A.MatriceA(10, 9) = Val(m109.Text)
outputOfA
For i = 1 To n
If A.MatriceA(i, i) = 0 Then
Debug.Print "Ñèñòåìà ðåøàåòñÿ äðóãèì ìåòîäîì!"
End '////break!
End If
Next i
d2 = 0
For i = 1 To n
s = A.MatriceA(i, i)
A.MatriceA(i, i) = 0
x1.VectorX(i) = 0
A.MatriceA(i, n + 1) = A.MatriceA(i, n + 1) / s
x.VectorX(i) = A.MatriceA(i, n + 1)
For j = 1 To n
If j <> i Then
A.MatriceA(i, j) = -A.MatriceA(i, j) / s
x1.VectorX(i) = x1.VectorX(i) + Abs(A.MatriceA(i, j))
d2 = d2 + Abs(A.MatriceA(i, j)) * Abs(A.MatriceA(i, j))
End If
Next j
Next i
d2 = Sqr(d2)
outputOfA
d = normlm(True)
For i = 1 To n
x1.VectorX(i) = 0
For j = 0 To n
If j <> 0 Then
x1.VectorX(i) = x1.VectorX(i) + Abs(A.MatriceA(i, j))
End If
Next j
Next i
d1 = normlm(True)
Debug.Print "Íîðìû = { " & d & ";" & d1 & ";" & d2 & "}"
If d1 < d Then
d = d1
End If
If d2 < d Then
d = d2
End If
Debug.Print "Íàèìåíüøàÿ íîðìà: " & d
If d >= 1 Then
Debug.Print "Äîñòàòî÷íûå óñëîâèÿ ñõîäèìîñòè ìåòîäà èòåðàöèÿ íå óäîâëåòâîðÿþòñÿ"
End
End If
Debug.Print "Óñëîâèå ìåòîäà ñõîäèìîñòè óäîâëåòâîðåíî"
s = d / (1 - d)
Do
VectorOfInteration
For i = 1 To n
x.VectorX(i) = x1.VectorX(i) - x.VectorX(i)
d = normlm(False) '''X!
outputOfVector
changeXbyX1
Next i
Loop Until d * s < e
End Sub
Dim A(2, 2), X0(2), X1(2), b(2)
'все условия см. здесь: http://oldunesco.kemsu.ru/mps/html/node5_36.htm
Private Sub Command1_Click()
Open App.Path & "\in_A.txt" For Input As #1 ' читаем A
For i = 0 To 2
For j = 0 To 2
Input #1, A(i, j)
Next j
Next i
Close #1
Open App.Path & "\in_X0.txt" For Input As #1 ' читаем нач прибл
For j = 0 To 2
Input #1, X0(j)
Next j
Close #1
Open App.Path & "\in_B.txt" For Input As #1 ' читаем b
For j = 0 To 2
Input #1, b(j)
Next j
Close #1
For u = 0 To 3 ' количество итераций... это надо делать циклом др.типа... но я ограничелся этим - неохота делать проверку "погрешности"
For i = 0 To 2
For k = 0 To 2
For j = 0 To 2
' насчет i<>j точно не уверен
If k <> j Then X1(i) = X1(i) + A(k, j) * X0(i)
Next j
Next k
X1(i) = X1(i) - b(i)
X1(i) = X1(i) * (-1 / A(i, i))
Next i
For i = 0 To 2
X0(i) = X1(i)
X1(i) = 0
Next i
Next u
Open App.Path & "\out.txt" For Output As #1
For i = 0 To 2
Print #1, X0(i)
Next i
Close #1
End Sub
Сейчас этот форум просматривают: AhrefsBot, Google-бот, Yandex-бот и гости: 14