163
Appendix D: MATLAB
®
Simulation Function Files
This appendix contains the contents of the function or script le used in
MATALB® simulation.
A word of caution: These simulations are to help the reader visualize and understand
optical concept. They are not guaranteed to be error free, and they are not intended
to be used as a ray tracing or optical design software. These simulations are not a
replacement for commercially available software. Additionally, these simulations are
not a substitute for professional help for designing optical components and systems.
SIMULATION D.1: BLACKBODY RADIATION
Simulation le: Blackbody_radiation.m
Purpose: To become familiar with emission spectra of hot objects
Related chapter: 2–Light Sources
file coNteNt
% Blackbody_radiation (Normalized blackbody radiation emission)
% Copyright Araz Yacoubian 2013, 2014
clear
close(‘all’)
T = input(‘Input temperature in deg. Kelvin: ‘);
% temperature in degree Kelvin
h = 6.62606885E-34;% Plank’s Constant (J.s)
c = 3.00E+08; % Speed of light (m/s)
k = 1.38E-23; % Boltzman constant (J/K)
lambda_set1 = 100:10:2000;
lambda_set2 = 2100:100:1000000;
lambda_nm = [lambda_set1 lambda_set2];%Wavelength (nm)
lambda_m = lambda_nm*(1e-9); % Wavelength (meter)
lambda_um = lambda_nm*(1e-3); % Wavelength (micrometer)
Param1_m = ((2*h*(c^2))./(lambda_m.^5));
Param2_m = 1./((exp((h*c/(k*T))./lambda_m))-1);
I = Param1_m.*Param2_m;
I_relative = I./(max(I));
164 Appendix D: MATLAB® Simulation Function Files
[tmp,size_lambda] = size(lambda_nm);
[Y,lamda_max_index] = max(I_relative);
for m = lamda_max_index:size_lambda;
if I_relative(m) < 0.05;
max_index = m;
break
end
end
figure(‘Name’,’Blackbody Radiation’,’NumberTitle’,’off’)
clf
axes(‘Fontsize’,14)
plot(lambda_um,I_relative,’k-’,’LineWidth’,2)
ylabel(‘Normalized Exitance’,’Fontsize’,16)
xlabel(‘Wavelength (microns)’,’Fontsize’,16)
axis([0 lambda_um(max_index) 0 1.1]);
axis([0 lambda_um(max_index) 0 1.1]);
grid
SIMULATION D.2: REFRACTION
Simulation le: Refraction.m
Purpose: To become familiar with principle of refraction
Related chapter: 4–Manipulation of Light
file coNteNt
% Refraction
% Copyright Araz Yacoubian 2013, 2014
clear
close(‘all’)
n1 = input(‘Enter Refractive index in medium 1 (n1): ‘);
n2 = input(‘Enter Refractive index in medium 2 (n2): ‘);
q1_d = input(‘Input angle (deg.): ‘);
q1_r = (pi/180)*q1_d;
q2_r_out = asin((n1/n2)*sin(q1_r));
%%%%
if abs(imag(q2_r_out)) > 0;
q2_r = -q1_r;
x2 = -10;
else
q2_r = q2_r_out;
x2 = 10;
end
q2_d = (180/pi)*q2_r;
165Appendix D: MATLAB® Simulation Function Files
disp(‘ ‘);
disp([‘Output angle = ‘ num2str(q2_d) ‘ (deg.)’]);
%%%%
x1 = -10;
y1 = -x1*tan(q1_r);
x0 = 0;
y0 = 0;
y2 = -x2*tan(q2_r);
xin = [x1 x0];
yin = [y1 y0];
xout = [x0 x2];
yout = [y0 y2];
figure(‘Name’,’Refraction’,’NumberTitle’,’off’)
plot(xin,yin,xout,yout,’— ‘)
line([0,0],[-10 10],’Color’,[0 0 0])
line([-10 10],[0,0],’LineStyle’,’:’,’Color’,[0 0 0])
axis([-10 10 -10 10])
xlabel(‘x (mm)’)
ylabel(‘y (mm)’)
text(-6,-8,[‘n1 = ‘ num2str(n1)],’BackgroundColor’,[1 1 1],
’fontsize’,14)
text(6,-8,[‘n2 = ‘ num2str(n2)],’BackgroundColor’,[1 1 1],
’fontsize’,14)
SIMULATION D.3: REFRACTION (GUI)
Simulation le: Refraction_GUI.m
Purpose: To become familiar with principle of refraction; same as Refraction.m,
however uses graphic user interface (GUI)
Related chapter: 4–Manipulation of Light
file coNteNt
function Refraction_GUI
% Refraction GUI
% Copyright Araz Yacoubian 2013, 2014
clear
close(‘all’)
fh = figure(‘Visible’,’off’);
set(fh,’Color’,[0.925 0.914 0.847]);
set(fh,’Visible’,’on’,’Name’,’Refraction’,’NumberTitle’,’off’);
166 Appendix D: MATLAB® Simulation Function Files
%%%%%%%%%%%%%%
slider_value = 20;
angle_input = slider_value;
n1 = 1;
n2 = 1.5;
%%%%%%%%%%%%%%
CalcPlot1
angle_input_string = num2str(angle_input,’%2.1f’);
text_angle_input = uicontrol(‘Style’,’edit’,...
‘String’,angle_input_string,’Position’,[60,210,35,25],...
‘BackgroundColor’,[1 1 1]);%,...
%%%%%%%%%%%%%%%%%%%%%%%%%
sh = uicontrol(fh,’Style’,’slider’,...
‘Max’,90,’Min’,-90,’Value’,slider_value,...
‘SliderStep’,[0.05 0.2],...
‘Position’,[30 100 20 250],...
‘Callback’,{@slider1_Callback});
function slider1_Callback(source,eventdata)
slider_value = get(source, ‘Value’);
slider_value_dsp = num2str(slider_value,’%2.1f’);
set(textinputSliderValue, ‘String’, slider_value_dsp);
CalcPlot1
end
angle_input_string = num2str(angle_input,’%2.1f’);
textinputSliderValue = uicontrol(‘Style’,’edit’,...
‘Value’,angle_input,’Position’,[60,210,35,25],...%[140,
round(5.7*btn_pd_y),100,25],...
‘BackgroundColor’,[1 1 1],...
‘String’,angle_input_string,...
‘Callback’,{@anglein_Callback});
function anglein_Callback(source,eventdata)
angle_input = str2double(get(source, ‘String’));
angle_input
slider_value = angle_input;
sh = uicontrol(fh,’Style’,’slider’,...
‘Max’,90,’Min’,-90,’Value’,slider_value,...
‘SliderStep’,[0.05 0.2],...
‘Position’,[30 100 20 250],...
‘Callback’,{@slider1_Callback});
CalcPlot1
167Appendix D: MATLAB® Simulation Function Files
end
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
text_n1_mark = uicontrol(‘Style’,’text’,...
‘String’,’n1 = ‘,’Position’,[180,20,85,25],...
‘BackgroundColor’,[0.925 0.914 0.847]);
function textin_n1(source,eventdata)
n1 = str2double(get(source, ‘String’));
CalcPlot1
end
n1_string = num2str(n1,’%2.1f’);
text_n1 = uicontrol(‘Style’,’edit’,...
‘String’,n1_string,’Position’,[240,25,25,25],...
%[160,25,25,25],...
‘BackgroundColor’,[1 1 1],...
‘Value’,n1,’Callback’,{@textin_n1});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
text_n2_mark = uicontrol(‘Style’,’text’,...
‘String’,’n2 = ‘,’Position’,[330,20,85,25],...
‘BackgroundColor’,[0.925 0.914 0.847]);
function textin_n2(source,eventdata)
n2 = str2double(get(source, ‘String’));
CalcPlot1
end
n2_string = num2str(n2,’%2.1f’);
text_n2 = uicontrol(‘Style’,’edit’,...
‘String’,n2_string,’Position’,[390,25,25,25],...
%[360,25,25,25],...
‘BackgroundColor’,[1 1 1],...
‘Value’,n2,’Callback’,{@textin_n2});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%
text_angle_mark = uicontrol(‘Style’,’text’,...
‘String’,’q1’,...
‘FontName’,’Symbol’,’FontSize’,11,...
‘Position’,[65,235,25,25],...
‘BackgroundColor’,[0.925 0.914 0.847]);
%%%%%%%%%%%
%%%%%%%%%%%%%%%
function CalcPlot1
q1_d = slider_value;

Get Optics Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.