Demo entry 6345809

test

   

Submitted by eddie on Feb 09, 2017 at 06:07
Language: Matlab. Code size: 1.7 kB.

function [ output_args ] = main()
% ray plotter
%   r: ray coordinates (y-position, theta)
%	x: x-distance


% initialize
counter = 1;

r{counter}(:,1) = [0 .001]';
r{counter}(:,2) = [0 0]';
r{counter}(:,3) = [0 -.001]';

x{counter} = 0;

% element 1
counter = counter + 1;
for i = 1:size(r{1}, 2)
	[r{counter}(:,i), x{counter}] = comp_1(r{counter-1}(:,i), x{counter-1}, 10);
end

% element 2
counter = counter + 1;
for i = 1:size(r{1}, 2)
	[r{counter}(:,i), x{counter}] = comp_4(r{counter-1}(:,i), x{counter-1}, 5);
end

% element 3
counter = counter + 1;
for i = 1:size(r{1}, 2)
	[r{counter}(:,i), x{counter}] = comp_1(r{counter-1}(:,i), x{counter-1}, 10);
end

% plot
figure(1); clf;
for i = 1:(length(x) - 1)
	if (x{i+1} - x{i}) > 0
		x0 = linspace(x{i}, x{i+1}, 100);
		for j = 1:size(r{1}, 2)
			m = (r{i+1}(1,j) - r{i}(1,j)) / (x{i+1} - x{i});
			for k = 1:length(x0)
				y0 = m*(x0 - x{i}) + r{i}(1,j);
			end
			plot(x0, y0);
			hold on
		end
	end
end

assignin('base', 'r', r);
assignin('base', 'x', x);

%-------------------------------------------------------------------%
function [out1, out2] = comp_1(in1, in2, d)
% free-space propagation
	out1 = [1 d; 0 1] * in1;
	out2 = in2 + d;
end


%-------------------------------------------------------------------%
function [out1, out2] = comp_2(in1, in2, n1, n2)
% planar refractive index interface
	out1 = [1 0; 0 n1/n2] * in1;
	out2 = in2;
end

%-------------------------------------------------------------------%
function [out1, out2] = comp_4(in1, in2, f)
% thin lens with focal length f
	out1 = [1 0; -(1/f) 1] * in1;
	out2 = in2;
end

end

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).