I have been grappling with a physics calculation pertaining to thin film optics. I have been endeavouring to use transfer matrix calculations to determine optical reflectance. Attached below is the code I authored:
(*Angle of 30° for p polarisation*)
ClearAll["Global`*"]
θ1 = 30°;
θ2 = ArcSin[(n1*Sin[θ1])/n2];
δ1 = π/2*Cos[θ1];
δ2 = π/2*Cos[θ2];
γ1 = n1/Cos[θ1];
γ2 = n2/Cos[θ2];
matrixA = {{Cos[δ1],(ISin[δ1])/γ1},{Iγ1Sin[δ1],Cos[δ1]}};
matrixB = {{Cos[δ2],(ISin[δ2])/γ2},{Iγ2Sin[δ2],Cos[δ2]}};
matrixC = matrixA . matrixB;
Text["Here is Matrix BC:"];
matrixD = matrixC . {{1},{n-I*k}};
β = matrixD[[1,1]];
α = matrixD[[2,1]];
Y = α/β;
Text["Here is r - known as reflection coefficient"];
r = (1-Y)/(1+Y);
n = 300;
k = 300;
(This section calculates the reflectance)
R = FullSimplify[r * Conjugate[r], Assumptions-> n1>0 && n2>0];
Plot3D[R, {n1,0.01,500},{n2,0.01,500}]
Text["This is The minimum"]
NMinimize[{R, n1>0 && n2>0},{n1,n2}]
In the key phenomenon of interest, an electromagnetic wave beam hits a two-layer coating on a metal substrate.
- n1, refractive index of the first layer.
- n2, refractive index of the second layer.
- n-ik, complex index of the metal substarate.
Using transfer matrices, I want to determine the reflectance of the entire system. However, Mathematica returns complex values for R which, I have confirmed manually, should be a purely real number. Mathematica's results, conversely, still contains an imaginary part, so numerical minimization fails:
"The function value 0.993195 - 2.77556*10^-17 I is not a real number at {n1, n2} = {1.91862,1.66351}."
Addio, even if I define R to the result of my manual derivation, minimization of that expression produces a negative value for R, which is not physically meaningful.
Is there an alternative method for encoding the algorithm so Mathematica performs the computations successfully?

RasR = Re[r]^2 + Im[r]^2;. – JimB Jul 16 '23 at 18:59