Pre-Engineering 220 Introduction to MatLab ® & Scientific Programming j kiefer



Download 8.69 Mb.
Page60/128
Date02.05.2018
Size8.69 Mb.
#47255
1   ...   56   57   58   59   60   61   62   63   ...   128

Firstly, select an initial guess (k = 0) .

Secondly, compute a new (k + 1 = 1).



Thirdly, test for convergence. . Notice that all the xi must pass the test.

If all the xi do not pass the test, then repeat until they do.




2. Gauss-Seidel Method

The Gauss-Seidel Method hopes to speed up the convergence by using newly computed values of xi at once, as soon as each is available. Thus, in computing xnew(12), for instance, the values of xnew(1), xnew(2), . . ., xnew(11) are used on the right hand side of the formula. We still need to keep separate sets of xnew and xold in order to perform the convergence tests.




% Script to implement Gauss-Seidel

b=[4 1 2;1 3 1;1 2 5];

c=[16;10;12];

xold=[1;1;1];

xnew=xold;

np=size(b);

n=np(1);

flag=1;


while flag > 0

for k=1:n

sum=0;

for l=1:n



if k~=l

sum=sum+b(k,l)*xnew(l);

end

end


xnew(k)=(c(k)-sum)/b(k,k);

end


for k=1:n

if abs((xnew(k)-xold(k))/xold(k)) > 0.0005

xold=xnew;

break


else

flag=0;


end

end


end

xnew





Download 8.69 Mb.

Share with your friends:
1   ...   56   57   58   59   60   61   62   63   ...   128




The database is protected by copyright ©ininet.org 2024
send message

    Main page