Jacoby

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
public double [,] _Amatrix = new double[,]
{
{ 2, 2, 10 },
{ 10, 1, 1 },
{ 2, 10, 1 }
};
public double [] _Bmatrix = new double[] { 14, 12, 13 };
public double[] _B;
public double[] _C;
public double[] _A;
public double E = 0.01;
public void prepare()
{
for (int i = _Amatrix.GetLength(0) - 1; i >= 0; i--)
{
for (int j = _Amatrix.GetLength(1) - 1; j >= 0; j--)
{
double sum = 0;
if (_Amatrix[i, i] != _Amatrix[i, j])
{
sum += _Amatrix[i, j];
}
if (_Amatrix[i, i] < sum)
{
for (int k = _Amatrix.GetLength(1) - 1; k >= 0; k--)
{
if (_Amatrix[i, i] < _Amatrix[k, i])
{
double[] res = new double[_Amatrix.GetLength(0)];
for (int s = 0; s < _Amatrix.GetLength(0); s++)
{
res[s] = _Amatrix[k, s];
_Amatrix[k, s] = _Amatrix[i, s];
_Amatrix[i, s] = res[s];
res[s] = _Bmatrix[k];
_Bmatrix[k] = _Bmatrix[i];
_Bmatrix[i] = res[s];
}
}
}
}
}
}
}
public void getB()
{
_B = new double[_Amatrix.GetLength(0)];
for (int i = 0; i < _Bmatrix.Length; i++)
{
_B[i] = _Bmatrix[i];
}
}
public void zeroing()
{
for (int i = 0; i < _Amatrix.GetLength(0); i++)
{
double data = _Amatrix[i, i];
for (int j = 0; j < _Amatrix.GetLength(1); j++)
{
if (_Amatrix[i, j] == _Amatrix[i, i])
{
_Amatrix[i, j] = 0;
}
else
{
_Amatrix[i, j] /= -data;
}
}
_Bmatrix[i] /= data;
}
}
public void Iteration()
{
_C = new double[_Amatrix.GetLength(0)];
_A = new double[_Amatrix.GetLength(0)];
for (int i = 0; i < _Bmatrix.Length; i++)
{
_A[i] = _Bmatrix[i];
}
for (int i = 0; i < _Amatrix.GetLength(0); i++)
{
double sum = 0;
for (int j = 0; j < _Amatrix.GetLength(1); j++)
{
sum += _Amatrix[i, j] * _Bmatrix[j];
}
_C[i] = sum + _B[i];
}
for(int i = 0; i < _Bmatrix.Length; i++)
{
_Bmatrix[i] = _C[i];
}
}
public void showAmatrix()
{
Console.WriteLine();
for(int i = 0; i < _Amatrix.GetLength(0); i++)
{
for(int j =0 ; j < _Amatrix.GetLength(1); j++)
{
Console.Write("{0}\t", _Amatrix[i, j]);
}
Console.WriteLine("|\t{0}", _Bmatrix[i]);
}
Console.WriteLine();
}
public double convergence()
{
return Math.Abs(_A[_Bmatrix.Length - 1] - _C[_Bmatrix.Length - 1]);
}
public void showC()
{
Console.WriteLine("C:");
for (int i = 0; i < _C.Length; i++)
{
Console.Write("{0}\t", _C[i]);
}
Console.WriteLine();
}
public void showB()
{
Console.WriteLine("B:");
for (int i = 0; i < _B.Length; i++)
{
Console.Write("{0}\t", _B[i]);
}
Console.WriteLine();
}
public void showA()
{
Console.WriteLine("A:");
for (int i = 0; i < _A.Length; i++)
{
Console.Write("{0}\t", _A[i]);
}
Console.WriteLine();
}