Please help with the code for Gauss Seidel Method

Please help with the code , I do not know what I am doing wrong. If you can please give me an advice. the first approximation should be x1=-0.2; x2=0.156 and x3=-.508. I have attached the code.
Please follow the format that I have already , because I will use it to do a bigger matrix 33 by 9

 採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 4 日

0 投票

Your assigment to T should be within for loops where both sibscripts are changing
It is uncommon to call norm() passing in a single value. Also those subscripts imply the assigmnent to norm2 should be inside for loops.

16 件のコメント

alberto ortiz
alberto ortiz 2016 年 11 月 4 日
I tried to put them on the loop but still is saying undying function or variable sum_1, can you please show me how to do it, Thank you
Walter Roberson
Walter Roberson 2016 年 11 月 4 日
Your code never initializes sum_1 or sum_2 so it is not possible to add values to them.
alberto ortiz
alberto ortiz 2016 年 11 月 5 日
yes how should you recommend me solving that, I am trying to figure out the code to use the Gauss Seidel on a larger matrix for heat transfer, any advice will help and thank you
alberto ortiz
alberto ortiz 2016 年 11 月 5 日
you did help me in the previous step with my code, which I have completed and got the matrix now I am just trying to do the Gauss Seidel method and if you can please help me. I attached the whole file, and thank you for your contributions.
Walter Roberson
Walter Roberson 2016 年 11 月 5 日
Where is your
sum_1 = 0;
sum_2 = 0;
?
alberto ortiz
alberto ortiz 2016 年 11 月 5 日
oh yes now is running and I got the correct answer thank you very much, the only thing is that I was only able to get the refined answer, not the x1,x2,x3,x4.... how can I stop the loop to give me those answers? I have attached the latest for the trial
Walter Roberson
Walter Roberson 2016 年 11 月 5 日
That code makes no reference to x1, x2, x3, x4, so we do not know.
alberto ortiz
alberto ortiz 2016 年 11 月 5 日
thank you , how would you recommend adding those references into the code?
Walter Roberson
Walter Roberson 2016 年 11 月 5 日
Nothing in any of your code versions has referenced x1, x2, x3, x4. You indicated approximate values for x1, x2, and x3, but you made no connection between those names and anything you compute.
If we are to infer that x1, x2, x3, x4 correspond to x(1), x(2), x(3), x(4) from your code posted at http://www.mathworks.com/matlabcentral/answers/310573-please-help-with-the-code-for-gauss-seidel-method#comment_404069
then notice that what you assign to x is
x= linspace(0,9E-3,9);
so you could just do that and skip everything else.
alberto ortiz
alberto ortiz 2016 年 11 月 7 日
the x1 and x2 are given by the k value at the start of the Gauss seidel portion, I just told you x1 and x2 because thats usually how we do it by hand but I put it on the code as k because without that information it could not be completed
Walter Roberson
Walter Roberson 2016 年 11 月 7 日
Your code has
for k=2:6
If x1 and x2 are given by the k value then that would imply that x1 = 2, x2 = 3, and so on. Is there a point in saving those values?
alberto ortiz
alberto ortiz 2016 年 11 月 7 日
yes because we see how the temperature gradient gets more refined with each iteration , and you will see a variance in the nodal temperature field , thank you
Walter Roberson
Walter Roberson 2016 年 11 月 7 日
編集済み: Walter Roberson 2016 年 11 月 7 日
Very well.
%Trial
A=[5 -2 3;-3 9 1;2 -1 -7]
B=[-1;2;3]
T=inv(A)*B
n=length(B)
sum_1=0
sum_2=0
k_vals = 2 : 6;
for kidx = 1 : length(kvals);
k = k_vals(kidx);
for i= 1:n
for j=1:n
if j<i
sum_1=sum_1+A(i,j)/(A(i,i))*T(j,k)
end
if j>i
sum_2=sum_2+A(i,j)/(A(i,i))*T(j,k-1)
end
end
T(i,k)=B(i)/(A(i,i))-sum_1-sum_2
end
sum_3=0
norm_1=(norm(T(i,k))-norm(T(i,k-1)))/(norm(T(i,k)))
T_er=abs(T(i,k)-T(i,k-1))
T_er<0.05
end
k_vals_cell = num2cell(k_vals);
[x1, x2, x3, x4, x5] = k_vals_cell{:};
Personally, I would say that -0.2, 0.156 and -.508 are really bad approximations of 2, 3, and 4, but perhaps that is good enough for your purposes.
alberto ortiz
alberto ortiz 2016 年 11 月 8 日
yes I put everything and the code is running , but for some reason is not plotting the new T value , any recommendations? with the surf pot
Walter Roberson
Walter Roberson 2016 年 11 月 8 日
You create T as length(b) by 6 in that last section, which is 249 x 6.
alberto ortiz
alberto ortiz 2016 年 11 月 8 日
Thank you very much

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2016 年 11 月 4 日
編集済み: Torsten 2016 年 11 月 4 日

0 投票

L = [5 0 0; -3 9 0 ; 2 -1 -7];
U = [0 -2 3; 0 0 1 ; 0 0 0];
b = [-1 2 3];
epsilon = 1;
xold = [0 0 0];
while epsilon > 1e-5
xnew = L\(b-U*xold)
xnew
epsilon = norm((xnew-xold)./xold,1);
xold = xnew;
end
Best wishes
Torsten.

2 件のコメント

alberto ortiz
alberto ortiz 2016 年 11 月 4 日
it has an error that code, thanks though
Torsten
Torsten 2016 年 11 月 7 日
Try
L = [5 0 0; -3 9 0 ; 2 -1 -7];
U = [0 -2 3; 0 0 1 ; 0 0 0];
b = [-1;2;3];
epsilon = 1;
xold = [1;0;0];
while epsilon > 1e-5
xnew = L\(b-U*xold)
xnew
epsilon = norm((xnew-xold)./xold,1);
xold = xnew;
end
Best wishes
Torsten.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by