I am running this code 100 times, generating multiple random numbers and using those random numbers to determine if a particle has leaked left(NL) leaked right(NL) abdorbed(NA). If scattering happens, then I want the code to return to recalculate mu.

1 次查看(过去 30 天)
%Prob2,Case 1
clc;
clear all;
tic
sigmatot=1;
sigmascat=0.5;
L=1;
x1=0.1;
x2=0.3;
N=100;
NL=0;
NR=0;
NA=0;
for i=1:N
k=0;
x=x1+(x2-x1)*rand()
k=1;%I want to re-start here if eta>c
mu=2*rand()-1;
deltax=-(mu/sigmatot)*(log(rand()));
r=x+deltax
if r<0
NL=NL+1;
end
if r>L
NR=NR+1;
else
c=sigmascat/sigmatot;
eta=rand();
if eta <= c
k=1;
end
if eta > c
NA=NA+1;
end
while k==1
%At this point, if eta>c I want the code to restart calculating
%mu
end
end
end

回答(1 个)

Sanju
Sanju 2024-2-19
To restart the calculation of ‘mu‘ whenever scattering occurs , your code can be updated as follows,
while k == 1
mu = 2 * rand() - 1; % recalculating mu
deltax = -(mu / sigmatot) * (log(rand()));% recalculating deltax
r = x + deltax;% recalculating r
if r < 0
NL = NL + 1;
k = 0; % Exit the while loop
end
if r > L
NR = NR + 1;
k = 0; % Exit the while loop
else
eta = rand();
if eta <= c
k = 1;
end
if eta > c
NA = NA + 1;
k = 0; % Exit the while loop
end
end
end
This code runs a simulation 100 times, tracking particles and their interactions with a medium. If a particle scatters, the code recalculates the scattering angle and resumes the simulation from the scattering point.
You can also refer to the below documentation links if required,
Hope this Helps!

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by