Gauss_Seidel_Method

Gauss_Seidel_Method -...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
function Gauss_Seidel_Method(A,b,es,lam) %Problem 12.1 %Uses the gauss seidel method to solve the passed system %until the percent relative error falls below es. %A=coefficient matrix, 3x3 %b=column vector of knowns %es=tolerance specified %lam=weighting factor x2=0; x3=0; x1_old=0; x2_old=0; x3_old=0; i=0; [m,n]=size(A); error('---->>Coefficient matrix must be a 3x3'); elseif nargin<3, error('At least 3 inputs are needed, coefficient matrix & column vector of knowns & error'); elseif nargin==3||isempty(lam),lam=0; end while(1) x1=(b(1,1)-A(1,2)*x2-A(1,3)*x3)/A(1,1);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: x2=(b(2,1)-A(2,1)*x1-A(2,3)*x3)/A(2,2); x3=(b(3,1)-A(3,1)*x1-A(3,2)*x2)/A(3,3); if lam&gt;1 &amp;&amp; lam&lt;2 %really want to use a for loop, is there any way? %method for overrelaxation, which is designed to accelerate the %convergent system. x1=(1-lam)*x1_old+lam*x1; x2=(1-lam)*x2_old+lam*x2; x3=(1-lam)*x3_old+lam*x3; end ea=abs((x1-x1_old)/x1)*100; x1_old=x1; x2_old=x2; x3_old=x3; i=i+1; if ea&lt;=es fprintf('\nAfter %d iterations, x1=%7.4f, x2=%7.4f, x3=%7.4f, maximum error=%4.3f percent\n\n',i,x1,x2,x3,ea); break end end...
View Full Document

Page1 / 2

Gauss_Seidel_Method -...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online