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

 Risposta accettata

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 Commenti

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
Your code never initializes sum_1 or sum_2 so it is not possible to add values to them.
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
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.
Where is your
sum_1 = 0;
sum_2 = 0;
?
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
That code makes no reference to x1, x2, x3, x4, so we do not know.
thank you , how would you recommend adding those references into the code?
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.
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
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?
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
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.
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
You create T as length(b) by 6 in that last section, which is 249 x 6.
Thank you very much

Accedi per commentare.

Più risposte (1)

Torsten
Torsten il 4 Nov 2016
Modificato: Torsten il 4 Nov 2016
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 Commenti

it has an error that code, thanks though
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.

Accedi per commentare.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by