How do I check a each element of a 2 x n matrix for values to remove?
Show older comments
I am trying to simulate a fluorescing vertical 1-D sample, emitting light from -pi/2 to pi/2, passing through a single thin lens, and then striking a detector. I have gotten this to work when the angle is zero (light is traveling horizontally and parallel to other light rays), but when I try to add the angle, it gets bad. To statistically model the process, I have made random number generation loop that creates a random position on the sample (r) and a random angle (r'), and vector v=(r,r'), with r = zero at the center and the sample going up h/2 and down -h/2.
I tried setting up a loop that would check where the ray is located (r) and direction it is aiming (r'), and then check if it is ouside of the lens. For example, diameter lens = Diam= 10, object distance = s_o=100, v=(2,-0.8), the condition would be: v((s_o*tan(v(2,j)) > (Diam/2)+(v(1,j))))=[ ]. I am doing this because it is necesary to omit values that are outside of the lens focal length ratio and diameter, f/.
This is not working. I have tried multiple ways including changing the for loop and if loops to multiple individual loops and even made them switch-case for a while.
function [I,S] = RayTraceAngle(s_i,s_o,f,Diam,L_sample,L_detector,n_pix,n)
%This function receives the optical setup information and determins
%statistical probability of the distribution of photons on a sensor.
% s_i is the distance from lens to image plane
% s_o is the distance from lens to object plane
% f is the focal length of the lens
% L_sample is the sample height
% L_detector is the detector height
% n_pix is the number of vertical pixels.
% Diam is the lens diameter
% n is the number of random values or iterations. Reccomend at least
% 125,000
format long
L=[1-s_i/f s_o*(1-s_i/f)+s_i;-1/f 1-s_o/f];
r1 = zeros(2,n); %allocate space for initial vector
r2 = zeros(2,n);%allocate space for final vector
h_pix=L_detector/n_pix;%height of pixel
for i = 1:n
%%%%%%%%%%%%%begin random position generation%%%%%%%%%%%%%
a=randi([-1,1],1,1); %generating a integer between -1 and 1
while a ==0 %excluding zero
b=randi([-1,1],1,1);%generating another number if a is zero
a=a+b; %adding new random number to a
end
c=rand(1)*a; %multiplying random number between 0 and 1 by + or -
rad_samp = L_sample/2; %distance from optical axis to edge of sample
r=rad_samp*c; %gives position from optical axis to point on sample
%%%%%%%%%%%%%end random position generation%%%%%%%%%%%%%
%%%%%%%%%%%%%begin random angle generation%%%%%%%%%%%%%
d=randi([-1,1],1,1); %generating a integer between -1 and 1
while d ==0 %excluding zero
e=randi([-1,1],1,1); %generating another number if a is zero
d=d+e; %adding new random number to d
end
r_prime=rand(1)*d*pi/2; %multiplying random number between 0 and 1 by + or -
v=[r;r_prime]; %the initial vector
[row,col]= size(v);
for j=1:1:length(col)
if (v(1,j) > 0) && (v(2,j)>0)
v((s_o*tan(v(2,j)) > (Diam/2)-(v(1,j))))=[];
end
end
[row,col]= size(v);
for j=1:1:length(col)
if (v(1,j) < 0) && (v(2,j)>0)
v((s_o*tan(v(2,j)) > (Diam/2)-(v(1,j))))=[];
end
end
[row,col]= size(v);
for j=1:1:length(col)
if (v(1,j) > 0) && (v(2,j)<0)
v((s_o*tan(v(2,j)) > (Diam/2)+(v(1,j))))=[];
end
end
[row,col]= size(v);
for j=1:1:length(col)
if (v(1,j) < 0) && (v(2,j)<0)
v((s_o*tan(v(2,j)) > (Diam/2)+(v(1,j))))=[];
end
end
end
%%%%%%%%%%%%%end random angle generation%%%%%%%%%%%%
[row,col]= size(v);
for i = 1:1:length(col)
r1(:,i)=v; %initial vector
r2(:,i)=L*v; %the found vector
I = r2;
S=r1;
edges = -(L_detector/2):h_pix:(L_detector/2); %creates the edges for the histogram bins with pixel height
histogram(I(1,:),edges) %creates histogram which includes Left bin edge but not right
end
Accepted Answer
More Answers (0)
Categories
Find more on Biological Physics 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!