% Last Update: 28.10.2008
% *************************************************************************
% Matlab tutorial - "Technische Optik" - TO2
% *************************************************************************
% This tutorial shows how to use Matlab in context of ABCD ray-matrices.
% We expoit the symbolic toolbox
%
% by Alexander Gößling, Photonics Lab, University of Applied Sciences
% *************************************************************************
%
% Execute a program:    press F5
% Get Help:             press F1
%
% If no ';' is placed behind a command, the result is printed to the
% command window
%
% *************************************************************************


% *************************************************************************
% I. Basic Matlab (if you are already familiar with Matlab skip I)
% *************************************************************************

close all;
clear all;
clc;

% Define a variable
l=sym('l');
% or
syms f g b;

% Define a matrix Ml [1st row; 2nd row] and a vector
Ml=[1 0; 0 l];
vector=[1; 1]

% Multiply matrices
Ml*vector

% Solve Equations - solve(eqn,...,eqnN,var1,...,varN)
% every eqn is set to zero!
A=solve(1/g+1/b-1/f,b+g-500,f-100,b,g,f);
% Print solutions 
A.g
A.b
A.f
% Print numeric solutions with 4 digis
digits(4)
vpa(A.g)
vpa(A.b)
vpa(A.f)


% Differentiate
syms x funct;
funct=x^4;
difffunct=diff(funct);

% Plot
subplot(1,2,1)
ezplot(funct);
subplot(1,2,2)
ezplot(difffunct);


% *************************************************************************


% *************************************************************************
% II. ABCD Formalism
% *************************************************************************

% ABCD Matrices (Please Check!)

clear all;

% free propagation
% s     - distance

syms s
Ap=[1 s; 0 1];

% thin lens
% fj     - focal length of lens j

syms f1 f2
Af1=[1 0; -1/f1 1];
Af2=[1 0; -1/f2 1];

% thick lens in air
% f     - focal lenth
% h1,2  - distance lens apex to focus
% s     - thickness
% n     - Index of refraction (glas=1.5)

syms f n h1 h2 A B C D
Ad=[1+h2/f s/n; -1/f 1-h1/f];

% generate latex code for export
% latex(Ad)

% Example:  two thin lenses in a distance of s
% Task:     Determine the effective focal length, h1, and h2

ABCD=Af1*Ap*Af2;

A=ABCD(1,1)-Ad(1,1);
B=ABCD(1,2)-Ad(1,2);
C=ABCD(2,1)-Ad(2,1);
D=ABCD(2,2)-Ad(2,2);






