查看单个帖子
旧 2008-03-31, 10:44   #7
xiaotie
初级会员
 
注册日期: 2008-03-28
年龄: 46
帖子: 19
声望力: 18
xiaotie 正向着好的方向发展
默认

Warning: MATLAB Toolbox Path Cache is out of date and is not being used.
Type 'help toolbox_path_cache' for more info.

To get started, select MATLAB Help or Demos from the Help menu.

>> function [y,n]=jacobi(A,b,x0,eps)
if nargin==3
eps=1.0e-6;
elseif nargin<3
error
return
end
D=diag(diag(A)); %求A的对角矩阵
L=-tril(A,-1); %求A的下三角阵
U=-triu(A,1); %求A的上三角阵
B=D\(L+U);
f=D\b;
y=B*x0+f;
n=1; %迭代次数
while norm(y-x0)>=eps
x0=y;
y=B*x0+f;
n=n+1;
end
??? function [y,n]=jacobi(A,b,x0,eps)
|
Error: Function definitions are not permitted at the prompt or in scripts.

>> A=[10,-1,0;-1,10,-2;0,-2,10]

A =

10 -1 0
-1 10 -2
0 -2 10

>> b=[9,7,6]'

b =

9
7
6

>> [x,n]=jacobi(A,b,[0,0,0]',1.0e-6)
??? Undefined command/function 'jacobi'.

>> A=[10,-1,0;-1,10,-2;0,-2,10];
b=[9,7,6]';
[x,n]=jacobi(A,b,[0,0,0]',1.0e-6)
??? Undefined command/function 'jacobi'.

>>
xiaotie 当前离线   回复时引用此帖