Fun
Follow


Puzzle challenge - distance to horizon

Image Analyst on 23 Mar 2024
Latest activity Reply by Image Analyst on 17 May 2024

In one line of MATLAB code, compute how far you can see at the seashore. In otherwords, how far away is the horizon from your eyes? You can assume you know your height and the diameter or radius of the earth.
BigZ
BigZ on 15 May 2024
This is ai answer:
radius = 6371; % radius of the Earth in kilometers
height = 1.7; % height of the observer in meters
distance = sqrt(2 * radius * height + height^2);
distance = distance / 1000; % convert distance to kilometers
disp(['The distance to the horizon is approximately ', num2str(distance), ' kilometers.']);
%The distance to the horizon is approximately 0.14719 kilometers.
Image Analyst
Image Analyst on 17 May 2024
That's a good example of why you can't just blindly trust AI. AI often gets answers wrong.
goc3
goc3 on 15 May 2024
The height and radius values in that AI answer are in different units...they cannot be inserted into the equation until one of them is converted to the units of the other.
Image Analyst
Image Analyst on 15 May 2024 (Edited on 15 May 2024)
Looks like no one else is going to post a response. This is the answer I got. Approximately 3 miles or 4.8 km.
D = 12742000; % Earth's diameter in meters.
R = D / 2; % Earth's radius in meters.
h = 1.8; % Your height in meters
% (R+h)^2 = d^2 + R^2
% d^2 = (R+h)^2 - R^2
distanceInMeters = sqrt((R+h)^2 - R^2)
dInMiles = distanceInMeters / 1609.34
distanceInMeters =
4789.1
dInMiles =
2.9758
Plug the numbers in directly to get the whole answer in one line of code.
goc3
goc3 on 15 May 2024
Using the constants provided in your answer, as well as the simplification mentioned by Christian, I wrote the following single-line anonymous function:
distance_in_miles = @(h) sqrt(h.*(h+12742000))./1609.34;
Since element-wise operators are used, it can take vector input and yield vector output, like the following:
height_in_meters = [0.3, 1.0, 1.8, 10, 30];
distance_in_miles(height_in_meters) =
1.2149 2.2180 2.9758 7.0141 12.1488
So, an infant sitting on the beach would be able to see more than a mile, a 1m-tall child over two miles, and an adult about three miles. On the other hand, looking out from a hotel room, perhaps on the third floor, would yield a view of about seven miles while a room on a much higher floor, perhaps the ninth, might result in a view of about 12 miles.
Christian Schröder
Christian Schröder on 23 Mar 2024
Assuming Earth is a perfect sphere, it should be (h*(h + d))^(1/2) unless I'm completely mistaken.
Image Analyst
Image Analyst on 23 Mar 2024 (Edited on 23 Mar 2024)
Can you give the distance in miles or kilometers? And sorry, I named variables after you answered so that people will all use the same letters.
Christian Schröder
Christian Schröder on 23 Mar 2024
The distance is already in miles or kilometers, so long as both d and h are; the units carry through. :)

Tags

No tags entered yet.