|
楼主 |
发表于 2011-5-6 09:50
|
显示全部楼层
回复 2 # sunminmin 的帖子
SquareDiamond.m文件- function [data]=SquareDiamond(N,initvalue,c)
- %输入N为:方阵的维数2^N+1,在方阵的四个角的顶点上放置的初值,
- %这里定为四个初值一样
- % Example:
- % tic;
- % d=SquareDiamond(8,5,0.5);
- % colormap(gray); %设置渲染的颜色
- % surf(d); %画出三维曲面
- % shading flat; %各小曲面之间不要网格
- % toc %测试运行时间
- n=2^N;
- data=zeros(n+1);
- data(1,1)=initvalue;
- data(1,n+1)=initvalue;
- data(n+1,1)=initvalue;
- data(n+1,n+1)=initvalue;
- data=mytry(data,n,c,n);
- function [x]=rnd(absvalue)
- %扩展的随机函数生成器,产生绝对值小于absvalue的随机实数
- x=(rand(1)-0.5)*2*absvalue;
- function [data]=mytry(data,m,r,n)
- %square=========================================
- %x-------x
- %---------
- %-- 0 -- 由四个x定中间的0
- %---------
- %x-------x
- for i=1:m:n
- for j=1:m:n
- data((i+i+m)/2,(j+j+m)/2)=(data(i,j)+data(i,j+m)+data(i+m,j)+data(i+m,j+m))/4+rnd(r);
- end
- end
- %diamond========================================
- %----x--
- %-----
- %x- 0 -x 由四个x定中间的0
- %--------
- %----x---
- %钻石步骤的横向部分
- %line No.1
- for j=1+m/2:m:n
- data(1,j)=(data(1,j+m/2)+data(1+m/2,j)+data(1,j-m/2)+data(n+1-m/2,j))/4+rnd(r);
- end
- %middle
- for i=1+m:m:n
- for j=1+m/2:m:n
- data(i,j)=(data(i,j+m/2)+data(i+m/2,j)+data(i,j-m/2)+data(i-m/2,j))/4+rnd(r);
- end
- end
- %line last
- for j=1+m/2:m:n
- data(n+1,j)=(data(n+1,j+m/2)+data(1+m/2,j)+data(n+1,j-m/2)+data(n+1-m/2,j))/4+rnd(r);
- end
- %钻石步骤的纵向部分
- %line No.1
- for i=1+m/2:m:n
- data(i,1)=(data(i,1+m/2)+data(i+m/2,1)+data(i,n+1-m/2)+data(i-m/2,1))/4+rnd(r);
- end
- %middle
- for i=1+m/2:m:n
- for j=1+m:m:n
- data(i,j)=(data(i,j+m/2)+data(i+m/2,j)+data(i,j-m/2)+data(i-m/2,j))/4+rnd(r);
- end
- end
- % line last
- for i=1+m/2:m:n
- data(i,n+1)=(data(i,1+m/2)+data(i+m/2,n+1)+data(i,n+1-m/2)+data(i-m/2,n+1))/4+rnd(r);
- end
- if (m>2)
- data=mytry(data,m/2,r/2,n); %递归
- end
复制代码 |
|