Prev: find statistics of a matrices
Next: Thz signal generation using Cmos and linear superimposition
From: zhl zhl on 6 Aug 2010 02:57 Hi. I need some help to simulate some special self avoiding walks. Let's start with say 100 simultaneous runs. After 2 steps, say 75 of them were still alive and 25 went backwards and were "killed". Since 75<100, duplicate each one not killed twice (to get 150 runs). Continue and each time the number not killed get below 100, multiple by 2. I would like to find out at the end of 5 steps, how many runs will still be alive. Here is my code. I can simulate the first 2 steps. But when it comes to the duplication part, I've trouble. Any help is much appreciated. Thanks! walk_num = 100; step_num = 2; success_walk = 0; for walk = 1 : walk_num x = 0; y = 0; history = []; trajectory = zeros ( step_num + 1, 2 ); % the origin. trajectory(1,1:2) = [ x, y ]; step_length = step_num; for step = 1 : step_num history = [history;x y]; destination = [ x + 1, y; x - 1, y; x, y + 1; x, y - 1 ]; k = ceil ( 4 * rand() ); x = destination(k,1); y = destination(k,2); trajectory(step+1,1:2) = [ x, y ]; destination2 = setdiff ( trajectory(step+1,1:2), history, 'rows' ); [ nrows, ncols ] = size ( destination2 ); if ( nrows == 0 ) step_length = step - 1; break; end %end if end %end step for loop if (step_length == step_num) success_walk = success_walk + 1; end %end if end %end walk for loop
From: Darren Rowland on 6 Aug 2010 03:57 Formulate the problem using a population vector. This way you can generate the random movements for every member of the population at once, and proceed through time keeping the whole population in step. Duplicating the population will then be a trivial matter. Hth, Darren
From: zhl zhl on 10 Aug 2010 00:12 Thanks for the reply! Here's the modified program that is workable until the 3rd step. I still have problem saving the steps taken into the history variable. Any help is much appreciated. Thanks a lot! times = 0; walk_num = 100; step_num = 2; success_walk = 0; totalstep_num = 5; for walk = 1 : walk_num x = 0; y = 0; history = []; trajectory = zeros ( step_num + 1, 2 ); % the origin. trajectory(1,1:2) = [ x, y ]; step_length = step_num; for step = 1 : step_num history = [history;x, y]; destination = [ x + 1, y; x - 1, y; x, y + 1; x, y - 1 ]; k = ceil ( 4 * rand() ); x = destination(k,1); y = destination(k,2); trajectory(step+1,1:2) = [ x, y ]; destination2 = setdiff ( trajectory(step+1,1:2), history, 'rows' ); [ nrows, ncols ] = size ( destination2 ); if ( nrows == 0 ) step_length = step - 1; break; end %end if population(walk,step,1:2) = [ x, y ]; end %end step for loop if (step_length == step_num) success_walk = success_walk + 1; success(success_walk,1,1:2) = population(walk,1,1:2); success(success_walk,2,1:2) = population(walk,2,1:2); end %end if end %end walk for loop for totalstep = 3 : totalstep_num if (success_walk < 100) run_num = success_walk*2; success_walk = 0; times = times + 1; for run = 1 : run_num if (run <= 0.5*run_num) x = success(run, totalstep - 1, 1); y = success(run, totalstep - 1, 2); else x = success(run - 0.5*run_num, totalstep - 1, 1); y = success(run - 0.5*run_num, totalstep - 1, 2); end %end if-else if (run <= 0.5*run_num) %history = [success(run,1,1), success(run,1,2)]; else %history = [success(run - 0.5*run_num,1,1), success(run - 0.5*run_num,1,2)]; end %end if-else destination = [ x + 1, y; x - 1, y; x, y + 1; x, y - 1 ]; k = ceil ( 4 * rand() ); x = destination(k,1); y = destination(k,2); trajectory = [ x, y ]; destination2 = setdiff ( trajectory, history, 'rows' ); [ nrows, ncols ] = size ( destination2 ); if ( nrows ~= 0 ) success_walk = success_walk + 1; end %end if end %end for else run_num = success_walk; success_walk = 0; for run = 1 : run_num x = success(run, totalstep - 1, 1); y = success(run, totalstep - 1, 2); %history = [success(run,1,1), success(run,1,2)]; destination = [ x + 1, y; x - 1, y; x, y + 1; x, y - 1 ]; k = ceil ( 4 * rand() ); x = destination(k,1); y = destination(k,2); trajectory = [ x, y ]; destination2 = setdiff ( trajectory, history, 'rows' ); [ nrows, ncols ] = size ( destination2 ); if (nrows~=0) success_walk = success_walk + 1; end %end if end %end for end %end if-else end %end totalstep for loop %fprintf(1, 'total no. of steps = %d\n', total_step_num); %fprintf(1, 'no. of successful walks eventually = %d\n', success_walk); %fprintf(1, 'no. of times I multiply by 2 = %d\n', times); %estimate = success_walk / (100*2^times); %fprintf(1, 'estimate = %d\n', estimate);
|
Pages: 1 Prev: find statistics of a matrices Next: Thz signal generation using Cmos and linear superimposition |