0

我有一个 matlab 代码,我最终想用它来查找随机放置在网格内的 50 个源的输出并求和;目前我可以让它为一个来源工作;代码是这样的;

%required constants
ro = 5*10^-6;
po = 40;
a = 5.9336*10^-6;
D = 2*10^-9;
f = 2.6835*10^-7;
mult = a/(4*f*D);
rc = 6.0260e-05
pop = 5:1:495;


%initialise 500 x 500 array to zero
pp = zeros(500,500);



i = 1;


while i < 2

    x(i) = randsample(pop,1);
    y(i) = randsample(pop,1);

%randomly selections an x,y point on grid - the below if loop sets a boundary of %+/70     %microns from the point to examine. The min lower point in x and y is 1, max 500;    

    if x(i) - 70 > 0 && x(i) + 70 <= 500 && y(i) - 70 > 0 && y(i) + 70 <= 500

        xb(i) = x(i) - 70;
        xu(i) = x(i) + 70;

        yb(i) = y(i) - 70;
        yu(i) = y(i) + 70;

    elseif x(i) - 70 < 0 && x(i) + 70 <= 500 && y(i) - 70 > 0 && y(i) + 70 <= 500

        xb(i) = 1;
        xu(i) = x(i) + 70;

        yb(i) = y(i) - 70;
        yu(i) = y(i) + 70;

    elseif x(i) - 70 > 0 && x(i) + 70 > 500 && y(i) - 70 > 0 && y(i) + 70 <= 500

        xb(i) = x(i) - 70;
        xu(i) = 500;

        yb(i) = y(i) - 70;
        yu(i) = y(i) + 70;

    elseif x(i) - 70 > 0 && x(i) + 70 <= 500 && y(i) - 70 < 0 && y(i) + 70 <= 500

        xb(i) = x(i) - 70;
        xu(i) = x(i) + 70;

        yb(i) = 1;
        yu(i) = y(i) + 70;

    elseif x(i) - 70 > 0 && x(i) + 70 <= 500 && y(i) - 70 > 0 && y(i) + 70 > 500

        xb(i) = x(i) - 70;
        xu(i) = x(i) + 70;

        yb(i) = 1;
        yu(i) = 500

    end


    %now test boundaries established, we use our equation for the source....   

    for xm = xb:xu
        for ym = yb:yu
            H(xm,ym) = (sqrt((xm - x).^2 + (ym - y).^2))*10^-6;
            %H is a distance; if beyond rc, source is zero
            if  H(xm,ym) > rc 
                pp(xm,ym) = 0;
            elseif  H(xm,ym) < ro
                pp(xm,ym) = po;
            else
                pp(xm,ym) = po + mult.*(H(xm,ym).^2 - ro^2 - 2.*rc^2.*log(H(xm,ym)./ro));
            end
        end
    end
    i = i + 1
end

这仅适用于一个来源;代码原样完美地产生单一来源。但是我在尝试向上推广到 while i < 51 时遇到了麻烦;基本上,我想修改代码,以便在每次运行 while 循环时创建一个 500 x 500 矩阵 pp,而不是覆盖以前的,而是将这些 pps 相加,以便获取所有源。

我尝试将 H 和 pp 等修改为 i、xm、ym 等的函数,但这似乎对我不起作用 - 关于我如何做到这一点的任何建议/示例?谢谢!

4

1 回答 1

1

我面前没有matlab,ATM,但我认为这应该可以。我没有费心检查是否xmym可能是负数、零或大于 500。

要创建 500 x 500 X 51 pp,只需...

这将保持一个运行总和。

pp = zeros([500 500 51]);
% removed for brevity

for i=1:51 %instead of while, which is less efficient
    % removed for brevity
    for xm = xb:xu
        for ym = yb:yu
            H(xm,ym) = (sqrt((xm - x).^2 + (ym - y).^2))*10^-6;
            %H is a distance; if beyond rc, source is zero
            if  H(xm,ym) > rc 
                pp(xm,ym,i) =  0;
            elseif  H(xm,ym) < ro
                pp(xm,ym,i) = po;
            else
                pp(xm,ym,i) =mult.*(H(xm,ym).^2 - ro^2 - 2.*rc^2.*log(H(xm,ym)./ro));
            end
        end
    end
end

这为您提供了 500 x 500 x 51 矩阵 pp。任何给定的 500x500 都可以使用pp(:,:,index). 总和可以通过sum(pp,3)

于 2013-05-02T17:49:37.650 回答