📄 gausseid.mht
字号:
From: <Saved by Windows Internet Explorer 7>
Subject:
Date: Tue, 12 May 2009 09:48:45 -0700
MIME-Version: 1.0
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.mece.ualberta.ca/Courses/mec390/390code/gausseid.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=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.6000.16825" name=3DGENERATOR></HEAD>
<BODY><PRE>% Illustration of Gauss-Seidel iteration with relaxation
% The goal is to determine a value of x1 and x2 that simultaneously
% satisfy the following equations...
%
% f1(x1,x2) =3D x1^2 + x2^2 + exp(x1) - 7.7183*x1 =3D 0
% f2(x1,x2) =3D x2 + exp(x2) + x1^3 - 10.389 =3D 0
% we will take an x1 out of f1(x1,x2) and an x2 out of f2(x1,x2)
% thus we will have
%
% x1 =3D g1(x1,x2) =3D (x1^2 + x2^2 + exp(x1))/7.7183 and
% x2 =3D g2(x1,x2) =3D 10.389 - exp(x2) - x1^3
% in file 'g1.m', we would have:
%
% function y =3D g1(x1,x2)
% y =3D (x1^2 + x2^2 + exp(x1))/7.7183;
% in file 'g2.m', we would have
%
% function y =3D g2(x1,x2)
% y =3D 10.389 - exp(x2) - x1^3;
fprintf('\nSolution of Non-Linear Equations using Gauss-Seidel =
iteration\n');
x1 =3D input('Enter x1 estimate: ');
x2 =3D input('Enter x2 extimate: ');
w =3D input('Enter relaxation factor: ');
maxerror =3D input('Enter max relative error (percent): ');
maxit =3D input('Enter max # of iterations: ');
count =3D 0;
error =3D 1;
fprintf('\niteration x1 x2 error\n');
while (error > maxerror) & (count < maxit)
count =3D count + 1;
x1new =3D g1(x1,x2);
error1 =3D abs((x1new - x1)/x1new*100); % determine error
x1 =3D x1 + w * (x1new - x1);
=20
x2new =3D g2(x1,x2);
error2 =3D abs((x2new - x2)/x2new*100); % Note: this could all be =
vectorized!
x2 =3D x2 + w * (x2new - x2);
error =3D max(error1,error2); % find maximum error
=20
fprintf('%6g %10g %10g %10g\n',count,x1,x2,error);
end
</PRE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -