24小时热门版块排行榜    

查看: 998  |  回复: 7

duinichixin

金虫 (著名写手)

[求助] 小弟求教几个程序-关于迭代法的 已有2人参与

小弟编写了Table4.1SOR中的r=0.01及n=8的程序,现有如下几个疑问:1、我运行出it为8,cpu=0.042,w(exp)=1.02,为什么与作者不一样?2、作者终止条件是F范数,我的是2范数,应该怎么修改?3.当n=64时出现‘Out of memory. Type HELP MEMORY for your options.’请问是我程序太复杂了吗?
注:test为确定w(exp)程序,SOR为调用程序,test2为根据w(exp)计算it程序,各位大神不吝赐教。
test.m:
n=8;
r=0.01;
a=ones(n,1);
b=ones(n-1,1);
I=eye(n);
MM=2*diag(a)-diag(b,-1)-diag(b,1);
N=0.5*diag(b,-1)-0.5*diag(b,1);
A=MM+2*r*N+100*I/(n+1);
B=A;
X=rand(n,n);
F=A*X+X*B;
E=kron(I,A)+kron(B',I);
b=F(;
%x=E\b;
x0=b;
ww=0.01:0.01:1.99;
D=diag(diag(E));    %求E的对角矩阵
L=-tril(E,-1);      %求E的下三角矩阵
U=-triu(E,1);       %求E的上三角矩阵
for i=1:length(ww)
    w=ww(i);
    B=inv(D-L*w)*((1-w)*D+w*U);
p(i)=max(max(abs(eig(B))));
%plot(ww,p);
[x,n1]=SOR(E,b,x0,w);
mm(i)=n1;
end
plot(ww,mm);
Xe=reshape(x,n,n);
errror=norm((Xe-X),2)

SOR.m:
function [x,n1]=SOR(E,b,x0,w,eps,M)

if nargin==4
    eps=1.0e-6;
    M=2000;
elseif nargin<4
    error
    return
elseif nargin==5
    M=200;
end
if(w<=0 || w>=2)    %收敛条件要求
    error;
    return;
end
D=diag(diag(E));    %求E的对角矩阵
L=-tril(E,-1);      %求E的下三角矩阵
U=-triu(E,1);       %求E的上三角矩阵
B=inv(D-L*w)*((1-w)*D+w*U);
f=w*inv((D-L*w))*b;
x=B*x0+f;
n1=1;               %迭代次数
%迭代过程
while norm(x-x0)>=eps
    x0=x;
    x=B*x0+f;
    n1=n1+1;
    if(n1>=M)
        disp('Warining:迭代次数太多,可能不收敛!');
        return;
    end
end

test2.m
w=1.02
n=8;
r=0.01;
a=ones(n,1);
b=ones(n-1,1);
I=eye(n);
M=2*diag(a)-diag(b,-1)-diag(b,1);
N=0.5*diag(b,-1)-0.5*diag(b,1);
A=M+2*r*N+100*I/(n+1);
B=A;
X=rand(n,n);
F=A*X+X*B;
E=kron(I,A)+kron(B',I);
b=F(;
%x=E\b;
x0=b;
[x,n1]=SOR(E,b,x0,w)
Xe=reshape(x,n,n);
errror=norm((Xe-X),2)

小弟求教几个程序-关于迭代法的
1.png


小弟求教几个程序-关于迭代法的-1
2.png
回复此楼
辉煌在拼搏之后
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

duinichixin

金虫 (著名写手)

b=F(,打出来有点错误
辉煌在拼搏之后
2楼2015-01-11 18:35:51
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

duinichixin

金虫 (著名写手)

引用回帖:
2楼: Originally posted by duinichixin at 2015-01-11 18:35:51
b=F(,打出来有点错误

括号里面是冒号,谢谢,表示把矩阵拉长
辉煌在拼搏之后
3楼2015-01-11 18:37:21
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

mathstudy

金虫 (正式写手)

【答案】应助回帖

感谢参与,应助指数 +1
试试  稀疏矩阵的命令;比如sparse
4楼2015-01-11 18:42:26
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

duinichixin

金虫 (著名写手)

引用回帖:
4楼: Originally posted by mathstudy at 2015-01-11 18:42:26
试试  稀疏矩阵的命令;比如sparse

加上sparse有区别吗?
辉煌在拼搏之后
5楼2015-01-11 18:57:32
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zaq123321

专家顾问 (著名写手)

【答案】应助回帖

感谢参与,应助指数 +1
Chang your 2 norm to frobenius norm by replacing 2 with fro. Test it to make sure it works. U don't need to care about cpu time in the testing since your computer is different with his.  Your computer memory may be not big enough to run n=64 case. Otherwise check whether there is some place to adjust your memory allocation in your code. Good luck

[ 发自手机版 http://muchong.com/3g ]
小木虫给我温暖,给我希望,爱就要爱小木虫。
6楼2015-01-11 21:30:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

duinichixin

金虫 (著名写手)

引用回帖:
6楼: Originally posted by zaq123321 at 2015-01-11 21:30:40
Chang your 2 norm to frobenius norm by replacing 2 with fro. Test it to make sure it works. U don't need to care about cpu time in the testing since your computer is different with his.  Your compute ...

thank you,I will try it again
辉煌在拼搏之后
7楼2015-01-12 08:25:56
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

mathstudy

金虫 (正式写手)

引用回帖:
5楼: Originally posted by duinichixin at 2015-01-11 18:57:32
加上sparse有区别吗?...

你试试,有很大区别的
8楼2015-01-12 09:04:35
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 duinichixin 的主题更新
信息提示
请填处理意见