Matlab code for vector format of Snell law please

28 views (last 30 days)
Or Peer
Or Peer on 14 May 2019
Edited: DGM on 8 Mar 2024
Given a unit vector(in air for example) and a given unit Normal vector(normal of flat water)
Can I get please a code for calculating the vector after refraction(Snell law).
All in 3D
Help would be very welcome.

Answers (1)

Aby
Aby on 8 Mar 2024
Edited: DGM on 8 Mar 2024
% Define input parameters
unit_vector = [1, 0, 0]; % Example incident unit vector
normal_vector = [0, 0, 1]; % Example normal vector
n1 = 1; % Refractive index of air
n2 = 1.5; % Refractive index of water
% Call the function to calculate the refracted vector
refracted_vector = snells_law(unit_vector, normal_vector, n1, n2);
% Display the result
disp('Refracted Vector:');
disp(refracted_vector);
function refracted_vector = snells_law(unit_vector, normal_vector, n1, n2)
% n1: refractive index of the initial medium
% n2: refractive index of the medium being entered into
% Ensure that the input vectors are unit vectors
unit_vector = unit_vector / norm(unit_vector);
normal_vector = normal_vector / norm(normal_vector);
% Calculate the dot product of the unit vector and the normal vector
cos_theta1 = dot(-unit_vector, normal_vector);
% Calculate the sine of the angle of incidence
sin_theta1 = sqrt(1 - cos_theta1^2);
% Calculate the sine of the angle of refraction using Snell's law
sin_theta2 = (n1 / n2) * sin_theta1;
% If the value inside sqrt is negative, total internal reflection occurs
if sin_theta2 > 1
fprintf('Total internal reflection occurs.\n');
refracted_vector = NaN;
else
% Calculate the cosine of the angle of refraction
cos_theta2 = sqrt(1 - sin_theta2^2);
% Calculate the refracted vector using Snell's law
refracted_vector = (n1 / n2) * unit_vector + ((n1 / n2) * cos_theta1 - cos_theta2) * normal_vector;
% Normalize the refracted vector to ensure it remains a unit vector
refracted_vector = refracted_vector / norm(refracted_vector);
end
end

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!