Why am I getting such a large number on the initial iteration?

5 views (last 30 days)
I am trying to make a program that shows how many reflections off of a wall a sound wave will make given first a certain arrangement of sound panels, and then random arrangements. It seems to be working, except that I iterate that whole process a number of times to verify that it isn't a statistical quirk. My problem: The first time this program iterates, the variable 'absorb' is much higher than in the following iterations. What I expect is that my arrangement, the 'wall0' matrix, should indeed give a higher 'absorb' value, however, on each successive attempt this is not the case.
I am not sure why this is happening, and I was hoping someone could look at the code to help out. I suspect it has something to do with the random number generation, however, I am not sure at what point I went wrong. The code is as follows:
If you notice, the very first run of the k-while loop (using the wall0 configuration) results in an absorb = 7, but all first iterations of the k-while loop are much lower. I have run this many times and have gotten similar results of having the first run be much higher than the rest.
function AuSorb()
clc; clear variables; close;
matrix wall0 is a representation of a wall that sound vectors are coming 'in contact' with. Cells represent probability of sound being absorbed. a and b values represent audio foam panels that offer a larger probability, 0 denotes drywall, zero probability.
a = 0.3;
b = 0.5;
n = 100;
wall0 = [0 0 0 0 0 0 0 0 0 0;...
0 0 0 0 0 0 0 0 0 0;...
0 a 0 b 0 a 0 0 0 0;...
0 0 a 0 b 0 a 0 0 0;...
0 0 0 a 0 a 0 0 0 0;...
0 a 0 b 0 a 0 0 0 0;...
0 0 a 0 b 0 a 0 0 0;...
0 0 0 a 0 a 0 0 0 0;...
0 b 0 0 b 0 b 0 b 0;...
0 0 0 0 0 0 0 0 0 0];
d01 is starting distance of source from wall, h0 and w0 are the starting positions relating to indexes of the matrix
d01 = 4;
h0 = 4;
w0 = 5;
Outer while loop just runs the algorithm multiple times and graphs each run against each other. k is just loop variable
k = 0;
while k < 5
J and A are placeholder matrices that will hold loop variables ahead. j is a loop variable for the next while loop.
J = []; A = [];
j = 0; wall = wall0;
resets variable wall to initial configuration at each iteration.
while j < 100
absorb = 0; %%counts the number of sound waves absorbed
Inner while loop runs the program first using the initial wall0 configuration, and then 99 more times using random configurations of the wall matrix
u = [];
v = [];
rnjesus = rand(1, n);
creates a probability for each sound wave to be absorbed, to be compared against the probability of the cell it lands on in wall matrix
theta = (pi/4) * 2*(rand(1, n) - 0.5);
phi = (pi/4) * 2 *(rand(1, n) - 0.5);
Creates random azimuthal and longitudinal angles between -pi/4 and pi/4
dH = int8(d01.*tan(theta));
dW = int8(d01.*tan(phi));
calculates the horizontal and vertical distances that each sound wave traverses based on initial distance from wall and random phi and theta
for i = 1:length(dH)
loops through entire dH and dW vectors
u = [u, (dH(i) + h0) + 1];
v = [v, (dW(i) + w0) + 1];
calculates landing position of each sound vector on the wall matrix, and and adds it to u(row) and v(column) vectors
if rnjesus(i) < wall(u(i) , v(i))
based on landing position, calculates if sound wave is absorbed or not, then adds this to the absorbtion total
u(i) = 100;
v(i) = 100; %%probably unneccessary
absorb = absorb + 1;
end
end
after first configuration, all further j-while loop iterations are performed by randomly rearranging the rows of the wall0 matrix (below)and stored in the wall matrix variable (w1 is leftover from early code and probably is unnecessary or could be replaced.)
w1 = wall0;
[m,n] = size(w1) ;
wall = w1 ;
for i=1:m
idx = randperm(n) ;
wall(i,idx) = w1(i,:);
end
for each iteration of the j-while loop, absorb numbers are added to the A vector, and same with J, just to have an x-variable to graph
A = [A, absorb]
j = j + 1;
J = [J, j];
end
plots each k run of the program
plot(J, A)
hold on
k = k + 1;
end end
  2 Comments
John D'Errico
John D'Errico on 26 Nov 2017
UGH. Totally undocumented code that does something only vaguely described. Variable names that have no meaning at all.
I hope you don't expect a serious answer quickly.
David Mueller
David Mueller on 26 Nov 2017
Edited: David Mueller on 26 Nov 2017
I apologize, and you are absolutely correct. I have edited the code and the question. Hopefully they are both more clear now. Posted this really late and wasn't thinking clearly.

Sign in to comment.

Answers (1)

Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan on 26 Nov 2017
The problem is that you are overwriting the variable n inside the loop, so it changes value from 100 to 10.
Changes:
[m,n] = size(w1); to [m,cols] = size(w1);
idx = randperm(n); to idx = randperm(cols);
This comes back to the comment above that your variable names have no meaning. If you'd taken care to use meaningful names, you wouldn't have had to make me debug this for you. Another suggestion: Put your code in one continuous block so it is easy to copy and run. You can add description as comments in your code.
  1 Comment
David Mueller
David Mueller on 26 Nov 2017
thank you very much for your response. This worked! I will try to be better about this in the future.

Sign in to comment.

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!