Matlab code for vector format of Snell law please
20 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Aby
2024-3-8
编辑:DGM
2024-3-8
% 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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!