Borrar filtros
Borrar filtros

Matlab code for vector format of Snell law please

10 visualizaciones (últimos 30 días)
Or Peer
Or Peer el 14 de Mayo de 2019
Editada: DGM el 8 de Mzo. de 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.

Respuestas (1)

Aby
Aby el 8 de Mzo. de 2024
Editada: DGM el 8 de Mzo. de 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

Categorías

Más información sobre Fourier Analysis and Filtering en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by