# Matrix multiplication, matrix with variables

4 views (last 30 days)
GH on 4 Jun 2020
Edited: Rik on 4 Jun 2020
Hi everyone,
how can I multiply several matrices, if in them there are at least one but maximum 3 variables. (The matrices are actually only two specifc kinds, a tranfer and a refractive matrix one after ithe other.
(I have 2017a version so the symbolic toolbox doesn't work yet)

#### 1 Comment

Rik on 4 Jun 2020
Can you provide a bit more detailed explanation? What is your input? What is your desired output?
And what have you tried so far?

Rik on 4 Jun 2020
Edited: Rik on 4 Jun 2020
If this code works, why not use this?
Fin=T_vit*R_lens_post*T_lens*R_lens_ant*T_aqueous*R_cor_post*T_cornea*R_cor_ant;
You also don't need to hard-code all your values:
%Accommodation in Diopters
Acc = 0;
cornea_antR = 7.8*10^-3;
cornea_postR = 6.5*10^-3;
lens_ant = 12.0*10^-3 + 0.4*Acc;
lens_post = -5.22*10^-3 + 0.2*Acc;
retina = -13.4*10^-3;
R = table(cornea_antR, cornea_postR, lens_ant, lens_post, retina);
%Index of Refraction
n_cornea = 1.377;
n_aqueous = 1.377;
n_lens = 1.42 + 0.0026*Acc - 0.00022 * Acc^2;
n_vitreous = 1.336;
n_air = 1; %you forgot this one here
N = table(n_cornea, n_aqueous, n_lens, n_vitreous);
%Thickness in mm - distance to the next surface
d_cornea = 0.55*10^-3;
d_aqueous = 2.97*10^-3 - 0.04 * Acc;
d_lens = 3.77*10^-3 + 0.04 * Acc;
d_vitreous = 16.713*10^-3;
D = table(d_cornea, d_aqueous, d_lens, d_vitreous);
%The system:(Arizona eye model ray matrices)
%Free space -> Ref_Cornea_Ant -> Trans_Cornea -> Ref_Cornea_Post ->
%Trans_Aq -> Ref_Lens_ant -> Trans_Lens -> Ref_Lens_Post -> Trans_vitreous
% % (in general)the Transfer matrix indexes [A B; C D]
% A = 1;
% B = d;
% C = 0;
% D = 1;
% % (in general)the Refractive matrix indexes [A B; C D]
% A = 1;
% B = 0;
% C = - (n_2 - n_1)/ (n_2 * R);
% D = (n_1/ n_2);
% T = [1 D; 0 1];
% R = [1 0; - (n_2 - n_1)/ (n_2 * R) (n_1/ n_2) ];
% d = d_vitreous = 16.713*10^-3 m
T_vit = [1 d_vitreous; 0 1];
% n_1 = n_lens = 1.42; n_2 = n_vitreous = 1.336 ; R = lens_post = -5.22*10^-3 m
% - (n_2 - n_1)/ (n_2 * R) = -(1.336 - 1.42 / (1.336*-5.22*10^-3))
% (n_1/ n_2) = 1.42 / 1.336
R_lens_post = [1 0; (-(n_vitreous - n_lens / (n_vitreous*lens_post))) (n_lens / n_vitreous) ];
% d = d_lens = 3.77*10^-3 m
T_lens = [1 d_lens; 0 1];
% n_1 = n_aqueous = 1.377; n_2 = n_lens = 1.42 ; R = lens_ant = 12.0*10^-3m
% - (n_2 - n_1)/ (n_2 * R) = -(1.42 - 1.377 / (1.42 *12.0*10^-3))
% (n_1/ n_2) = (1.377/ 1.42)
R_lens_ant = [1 0; (-(n_lens - n_aqueous / (n_lens *lens_ant))) (n_aqueous/ n_lens) ];
% d = d_aqueous = 2.97*10^-3 m
T_aqueous = [1 d_aqueous; 0 1];
% n_1 = n_cornea = 1.377; ; n_2 = n_aqueous = 1.377; R = cornea_postR = 6.5*10^-3
% - (n_2 - n_1)/ (n_2 * R) = -(1.377 - 1.377 / (1.377 *6.5*10^-3))
% (n_1/ n_2) = (1.377/ 1.377)
R_cor_post = [1 0; (-(n_aqueous - n_cornea / (n_aqueous *cornea_postR))) (n_cornea/n_aqueous) ];
% d = d_aqueous = 2.97*10^-3 m
T_cornea = [1 d_aqueous ; 0 1];
% n_1 = n_air = 1; ; n_2 = n_cornea= 1.377; R = cornea_antR = 7.8*10^-3
% - (n_2 - n_1)/ (n_2 * R) = -(1.377 - 1 / (1.377 *6.5*10^-3))
% (n_1/ n_2) = (1/ 1.377)
R_cor_ant = [1 0; (-(n_cornea - n_air / (n_cornea *cornea_antR))) (n_air/n_cornea)];
Fin=T_vit*R_lens_post*T_lens*R_lens_ant*T_aqueous*R_cor_post*T_cornea*R_cor_ant;

GH on 4 Jun 2020
You are absolutly right, thank you very very much.
Rik on 4 Jun 2020
It would probably help with readability if you used a few anonymous functions:
%Accommodation in Diopters
Acc = 0;
cornea_antR = 7.8*10^-3;
cornea_postR = 6.5*10^-3;
lens_ant = 12.0*10^-3 + 0.4*Acc;
lens_post = -5.22*10^-3 + 0.2*Acc;
retina = -13.4*10^-3; %unused variable
%Index of Refraction
n_cornea = 1.377;
n_aqueous = 1.377;
n_lens = 1.42 + 0.0026*Acc - 0.00022 * Acc^2;
n_vitreous = 1.336;
n_air = 1; %you forgot this one here
%Thickness in mm - distance to the next surface
d_cornea = 0.55*10^-3; %unused variable
d_aqueous = 2.97*10^-3 - 0.04 * Acc;
d_lens = 3.77*10^-3 + 0.04 * Acc;
d_vitreous = 16.713*10^-3;
%The system:(Arizona eye model ray matrices)
%Free space -> Ref_Cornea_Ant -> Trans_Cornea -> Ref_Cornea_Post ->
%Trans_Aq -> Ref_Lens_ant -> Trans_Lens -> Ref_Lens_Post -> Trans_vitreous
% (in general)the Transfer matrix indexes
% (in general)the Refractive matrix indexes
T = @(D) [1 D; 0 1];
R = @(n_1,n_2,r) [1 0; - (n_2 - n_1)/ (n_2 * r) (n_1/ n_2) ];
Fin=T(d_vitreous)*R(n_aqueous,n_vitreous,lens_post)* ...
T(d_lens)*R(n_aqueous,n_lens,lens_ant)* ...
T(d_aqueous)*R(n_cornea,n_aqueous,cornea_postR)* ...
T(d_aqueous)*R(n_air,n_cornea,cornea_antR);
end