Loop / indexing query in MATLAB -
i have matlab code want use find output 50 sources randomly placed inside grid , summed; @ moment can work 1 source; code this;
%required constants ro = 5*10^-6; po = 40; = 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 0 pp = zeros(500,500); = 1; while < 2 x(i) = randsample(pop,1); y(i) = randsample(pop,1); %randomly selections x,y point on grid - below if loop sets boundary of %+/70 %microns point examine. min lower point in x , y 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, use our equation source.... xm = xb:xu ym = yb:yu h(xm,ym) = (sqrt((xm - x).^2 + (ym - y).^2))*10^-6; %h distance; if beyond rc, source 0 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 = + 1 end
this works 1 source; code produces single source perfectly. i'm running trouble trying generalise upwards while < 51 ; basically, want modify code 500 x 500 matrix pp created each run of while loop, , instead of over-writing previous ones, these pps summed sources.
i tried modifying h , pp etc functions of i,xm,ym etc didn't seem work me - suggestions / examples of how might this? thanks!
i don't have matlab in front of me, atm, think should work. didn't bother checking if xm
or ym
negative or 0 or greater 500.
for creating 500 x 500 x 51 pp, do...
this maintain running sum.
pp = zeros([500 500 51]); % removed brevity i=1:51 %instead of while, less efficient % removed brevity xm = xb:xu ym = yb:yu h(xm,ym) = (sqrt((xm - x).^2 + (ym - y).^2))*10^-6; %h distance; if beyond rc, source 0 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
this gives 500 x 500 x 51 matrix pp. given 500x500 can extracted using pp(:,:,index)
. sum can obtained sum(pp,3)
Comments
Post a Comment