Здравствуйте, хотел у Вас спросить по поводу кода реализации метода Релаксации
Я лишь добавил критерий остановки
Код:
A = {{10, 5, 0, 0}, {5, 10, -4, 0}, {0, -4, 8, -1}, {0, 0, -1, 5}};
B = {6, 25, -11, -11};
P = {0, 0, 0, 0};
SORmethod[A0_, B0_, P0_, \[Omega]_, max_, precision_] :=
Module[{A = N[A0], B = N[B0], i, j, k = 0, n = Length[P0], P = P0,
oldP = P0, Pvalue}, Pvalue[0] = P;
Print[Subscript["P", 0], " = ", P];
For [ i = 1, i <= n, i++,
\!\(\*SubscriptBox[\(P\), \(\(\[LeftDoubleBracket]\)\(i\)\(\
\[RightDoubleBracket]\)\)]\) = (1 - \[Omega])
\!\(\*SubscriptBox[\(oldP\), \(\(\[LeftDoubleBracket]\)\(i\)\(\
\[RightDoubleBracket]\)\)]\) + \[Omega]/
\!\(\*SubscriptBox[\(A\), \(\(\[LeftDoubleBracket]\)\(i,
i\)\(\[RightDoubleBracket]\)\)]\) (
\!\(\*SubscriptBox[\(B\), \(\(\[LeftDoubleBracket]\)\(i\)\(\
\[RightDoubleBracket]\)\)]\) - \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(j = 1\), \(i - 1\)]\(
\*SubscriptBox[\(A\), \(\([[\)\(i, j\)\(]]\)\)]\
\*SubscriptBox[\(P\), \(\([[\)\(j\)\(]]\)\)]\)\) - \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(j = i + 1\), \(n\)]\(
\*SubscriptBox[\(A\), \(\([[\)\(i, j\)\(]]\)\)]\
\*SubscriptBox[\(oldP\), \(\([[\)\(j\)\(]]\)\)]\)\)) ]
Pvalue[1] = P; k = 1;
While[
k < max &&
Max[Abs[Pvalue[k] - Pvalue[k - 1]]]/Max[Abs[Pvalue[k]]] >
precision,
For[ i = 1, i <= n, i++,
\!\(\*SubscriptBox[\(P\), \(\(\[LeftDoubleBracket]\)\(i\)\(\
\[RightDoubleBracket]\)\)]\) = (1 - \[Omega])
\!\(\*SubscriptBox[\(oldP\), \(\(\[LeftDoubleBracket]\)\(i\)\(\
\[RightDoubleBracket]\)\)]\) + \[Omega]/
\!\(\*SubscriptBox[\(A\), \(\(\[LeftDoubleBracket]\)\(i,
i\)\(\[RightDoubleBracket]\)\)]\) (
\!\(\*SubscriptBox[\(B\), \(\(\[LeftDoubleBracket]\)\(i\)\(\
\[RightDoubleBracket]\)\)]\) - \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(j = 1\), \(i - 1\)]\(
\*SubscriptBox[\(A\), \(\([[\)\(i, j\)\(]]\)\)]\
\*SubscriptBox[\(P\), \(\([[\)\(j\)\(]]\)\)]\)\) - \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(j = i + 1\), \(n\)]\(
\*SubscriptBox[\(A\), \(\([[\)\(i, j\)\(]]\)\)]\
\*SubscriptBox[\(oldP\), \(\([[\)\(j\)\(]]\)\)]\)\)) ];
Print[Subscript["P", k + 1], " = ", P];
oldP = P;
k = k + 1;
Pvalue[k] = P; ];
Return[P]; ];
На что программа выдаёт вот такую ошибку
Код:
SORmethod[A, B, P, 0.5, 100, 0.001]
Subscript[P, 0] = {0,0,0,0}
Set::write: Tag Times in Null Pvalue$717[1] is Protected. >>
Subscript[P, 2] = {0.3,1.175,-0.39375,-1.13938}
Out[32]={0.3, 1.175, -0.39375, -1.13938}
За основу был взят код
http://math.fullerton.edu/mathews/n2003/sormethod/SORmethodMod.nb