MATLAB仿真程序代码--模拟品牌传播的过程.docx
MATLAB仿真程序代码-模拟品牌传播的过程附录二MATLAB仿真程序代码function Et/Dt = BrandSpread(uO/params)%这个程序用来模拟品牌传播的过程% n研究的区域的大小,为n*n的点阵% t时刻% u(x,y,t) t时刻(x,y)点的品牌信心值% Q(x,y,t) t时刻(x,y)点的品牌信号值% c(x,y) (x,y)点的品牌传播速率% k(x,y) (x,y)点对品牌信号的敏感程度% Initializationif nargin<2params=strcut;end;if isfield(params/con') && -isempty(params.con) con = params.con;elsecon = 0;end;if isfield(params/maxf') && -isempty(params.maxf) maxf = params.maxf;elsemaxf = 10;end;if isfield(params/v') && -isempty(params.v)v = params.v;elsev = 1;end;if isfield(params/n') && -isempty(params.n)n = params.n;elsen = 10;end;if isfield(params/maxt') && -isempty(params.maxt)maxt = params.maxt;elsemaxt = 20;end;t=l;%循环次数tflag = l;%tflag = l时说明未达到平衡,需要做交换;tflag=0时则 达到平衡,区域内不会出现信号交换,停止信号交换u=u0;%随机生成L10上的均匀分布k(:z:)=round(9*rand(n) + l;%根据u和k计算每一个点上的Q Q(:,:)=u(:,:).*k(:,:);%顺序式传播,%先由点(ij)与其右边的点(ij+l)进行交换%再由点(ij)与其下方的点(i+Lj)进行交换 E(t)=sum(sum(u)/nA2; % D(t)=sum(sum(u-E(t).A2)/nA2;%随机获得一个需要观察的点(x_s,y_s) x_s=round(rand*n) y_s=round(rand*n) f=u(x_s,y_s);fprintf('The coordinate of the observe point isn');fprintf('( %d , %d )n',x_s,y_s);fprintf('itt mean valuet Variancetn');fprintf('%dt %,3et %,3et n',t,E,D);if t< = maxffigure;x=l:n;y=x;X,Y=meshgrid(x,y);mesh(X,Y/u(:/:);end;while (tflag = = l)t=t+l;tflag=O;% Phase 1for (i=l:n-l)for (j = l:n)deltaQ=(k(i+Lj)*Q(i,j)-k(i,j)*Q(i+Lj)/(k(i+Lj)+k(i,j);if deltaQ>=vdeltaQ=v;endif (deltaQ=0)tflag = l;endQ(ij)=Q(ij)-deltaQ-abs(deltaQ)*con;Q(i+1 J)=Q(i+lj)+deltaQ-abs(deltaQ)*con;u(iJ)=Q(i,j)/k(i,j);u(i+lj)=Q(i+lj)/k(i+lj);endend% Phase 2for (i = l:n)for (j=l:n-l)deltaQ =(k(ij + l)*Q(i,j)-k(iJ)*Q(iJ+l)/(k(iJ+l)+k(i,j);if deltaQ>=vdeltaQ=v;endif (deltaQ-=0)tflag = l;endQ(ij); Q(i,j)-deltaQ-abs(deltaQ)*con;Q(ij + l)=Q(ij + l)+deltaQ-abs(deltaQ)*con;u(ij)=Q(ij)/k(ij);u(ij+l)=Q(ij+l)/l<(ij+l);endendE(t)=sum(sum(u)/nA2;D(t)=sum(sum(u-E(t).A2)/nA2;f(t)=u(x_s/y_s);if t< = maxffigure;x=l:n;y=x;X,Y = meshgrid(x/y);mesh(X,Y,u(:,:);end;fprintf('%dt %.3et %.3et n)t,E(t),D(t);if (t> = maxt)break;end;if tflag =0break;end;end;figure;plot(l:t/E,'b-,/'linewidth,/l);xlabel('time');ylabel('mean value');legend('change of mean value');figure;plotaxD/r-'/linewidth'J);xlabel('time');ylabel('variance');legend('change of variance');figure;plot(l:t,f,g-'/'linewidth,fl);xlabel('time');ylabel('U');legend('change of U');