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 件のコメント
Shubham Khatri
2021 年 5 月 3 日
編集済み: Shubham Khatri
2021 年 5 月 3 日
Hello,
Can you please elaborate on exactly what issue you are facing?
Thanks
回答 (1 件)
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!
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!