# Need help to draw lines.

2 views (last 30 days)
Axelina Lindgren on 1 May 2021
Answered: Prabhan Purwar on 7 May 2021
I need some help plotting lines going through the lens shape from (x, y)=(-10 0) to (0, 10) and I don't know how to do it even if I've tried several times I only get errors. I need a line from the starting point to the edge of the lens for different y:s. Then I need a line from that point straight over to the oposite side of the lens. Then i need a line from that point to the end point. I have tried different ways but I don't know how I am supposed to get the right x value for a certain y. Grateful for some help!
The code down below is for creating the lens shape.
clear
n1 = 1; %Refractive index air
n2 = 1.5; %Refractive index lens
D = 10; %Lens diameter
d = 10; %Focal length
lopt = n1.*sqrt(d.^2 + R.^2); %Optical way when the ray does not diverge through the lens
Y = linspace(0, R, 1000);
X = linspace(0, d, 1000);
for ii = 1:length(Y)
LoptTemp = zeros(length(Y), length(Y));
L1(:,ii) = sqrt( (d-X).^2 + Y(ii).^2)*n1;
L2(:,ii) = X*n2;
LoptTemp(:,ii) = L1(:,ii) + L2(:,ii);
diffii(:,ii) = abs(LoptTemp(:,ii) - lopt);
[difference minIndex(ii)] = min(diffii(:,ii));
x_fit(ii) = X(minIndex(ii));
end
figure
plot(x_fit, Y, 'black');
hold on
plot(x_fit.*-1, Y, 'black');
hold on
plot(x_fit, -Y, 'black');
hold on
plot(x_fit.*-1, -Y, 'black');
hold on

Prabhan Purwar on 7 May 2021
Hi,
Kindly have a look at the following link for plotting lines.
File Exchange repositories such as Optometrika can act as a good starting point to learn about Optometric and it's implementation using MATLAB.
Hope it helps!!
Thanks