📄 gs.mht
字号:
From: <Saved by Windows Internet Explorer 7>
Subject:
Date: Tue, 12 May 2009 09:47:26 -0700
MIME-Version: 1.0
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: 7bit
Content-Location: http://www.mece.ualberta.ca/Courses/mec390/390code/gs.m
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.6000.16825" name=GENERATOR></HEAD>
<BODY><PRE>% Illustration of Gauss Seidel iteration
%
% The goal is to determine a set of values of x1...x4 that
% satisfy the following equations...
% f1(x1,x2,x3,x4) = -2*x1 + x2 + 1.5 = 0
% f2(x1,x2,x3,x4) = x1 - 2*x2 + x3 - 0.5 = 0
% f3(x1,x2,x3,x4) = x2 - 2*x3 + x4 - 0.5 = 0
% f4(x1,x2,x3,x4) = x3 - 2*x4 + 0.5 = 0
%
% We will take an x1 out of f1 and an x2 out of f2 and so on.
% x1 = g1(x1,x2,x3,x4) = (x2 + 1.5) / 2
% x2 = g2(x1,x2,x3,x4) = (x1 + x3 - 0.5) / 2
% x3 = g3(x1,x2,x3,x4) = (x2 + x4 - 0.5) / 2
% x4 = g4(x1,x2,x3,x4) = (x3 + 0.5) / 2
% These are defined in files 'lin_g1.m' through 'lin_g4.m'
fprintf('\nSolution of System of Equations using GS iteration\n');
x1 = input('Enter x1 estimate : ');
x2 = input('Enter x2 extimate : ');
x3 = input('Enter x3 estimate : ');
x4 = input('Enter x4 extimate : ');
maxerror = input('Enter max approx error : ');
maxit = input('Enter max # of iterations: ');
count = 0;
error = 100;
fprintf('\niteration x1 x2 x3 x4\n');
while (error > maxerror) & (count < maxit)
count = count + 1;
x1new = lin_g1(x1,x2,x3,x4);
x2new = lin_g2(x1new,x2,x3,x4);
x3new = lin_g3(x1new,x2new,x3,x4);
x4new = lin_g4(x1new,x2new,x3new,x4);
error1 = abs(x1new - x1);
error2 = abs(x2new - x2);
error3 = abs(x3new - x3);
error4 = abs(x4new - x4);
error = max([error1,error2,error3,error4]);
fprintf('%6i %10.6f %10.6f %10.6f %10.6f\n',...
count,x1new,x2new,x3new,x4new);
x1 = x1new; % assign new values
x2 = x2new;
x3 = x3new;
x4 = x4new;
end
</PRE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -