Created
August 5, 2025 17:39
-
-
Save louismeunier/c59eb88aaebfae0e69e72d266f97af45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Julia | |
| begin | |
| using Plots; plotly() | |
| # radius of the sphere | |
| r = 2 | |
| # point to find the tangent at | |
| x_0 = 1 | |
| y_0 = 1 | |
| # working with square roots is a little finicky... | |
| z_0 = -sqrt(2) | |
| # parametric equations of the sphere in terms of theta and phi | |
| x_sphere(theta, phi) = r*cos(theta) * sin(phi) | |
| y_sphere(theta, phi) = r*cos(theta) * cos(phi) | |
| z_sphere(theta, phi) = r*sin(theta) | |
| # the corresponding theta and phi to the point (x_0, y_0, z_0) | |
| theta_param = asin(z_0/r) | |
| phi_param = acos(y_0 / (r * cos(theta_param))) | |
| # range of theta and phi to plot the sphere | |
| theta_range =range(-2pi, stop=2pi, length=100) | |
| phi_range = range(-2pi, stop=2pi, length=100) | |
| # plot ranges for sphere | |
| x_range_sphere = [x_sphere(t, p) for t in theta_range, p in phi_range] | |
| y_range_sphere = [y_sphere(t, p) for t in theta_range, p in phi_range] | |
| z_range_sphere = [z_sphere(t, p) for t in theta_range, p in phi_range] | |
| # sphere plot | |
| surface( | |
| x_range_sphere, | |
| y_range_sphere, | |
| z_range_sphere, | |
| label="Sphere", | |
| color=RGBA(172/255, 39/255, 245/255, 1), | |
| legend=:none | |
| ) | |
| # derivatives the parametric equations of the sphere: | |
| # in terms of theta | |
| d_theta_x(theta, phi) = -r*sin(theta) * sin(phi) | |
| d_theta_y(theta, phi) = -r*sin(theta) * cos(phi) | |
| d_theta_z(theta, phi) = r*cos(theta) | |
| # in terms of phi | |
| d_phi_x(theta, phi) = r*cos(theta) * cos(phi) | |
| d_phi_y(theta, phi) = -r*cos(theta) * sin(phi) | |
| d_phi_z(theta, phi) = 0 | |
| # a vector of "d_theta" of the sphere at the point (x_0, y_0, z_0) | |
| d_theta_vec = [ | |
| d_theta_x(theta_param, phi_param), | |
| d_theta_y(theta_param, phi_param), | |
| d_theta_z(theta_param, phi_param) | |
| ] | |
| # a vector of "d_phi" of the sphere at the point (x_0, y_0, z_0) | |
| d_phi_vec = [ | |
| d_phi_x(theta_param, phi_param), | |
| d_phi_y(theta_param, phi_param), | |
| d_phi_z(theta_param, phi_param) | |
| ] | |
| # plotting the respective tangent vectors | |
| plot!( | |
| [x_0-d_theta_vec[1], x_0], | |
| [y_0-d_theta_vec[2], y_0], | |
| [-z_0+d_theta_vec[3], -z_0], | |
| label="Tangent Vector, theta", | |
| color=RGBA(0/255, 255, 0/255, 1) | |
| ) | |
| plot!( | |
| [x_0, x_0 + d_phi_vec[1]], | |
| [y_0, y_0 + d_phi_vec[2]], | |
| [-z_0, -z_0 + d_phi_vec[3]], | |
| label="Tangent Vector, phi", | |
| color=RGBA(255, 0/255, 0/255, 1) | |
| ) | |
| # the cross product of the two vectors above | |
| # this returns the line normal to the sphere at the point (x_0, y_0, z_0) | |
| d_cross = cross( d_theta_vec, d_phi_vec ) | |
| plot!( | |
| [x_0, x_0+d_cross[1]], | |
| [y_0, y_0+d_cross[2]], | |
| [-z_0, -z_0-d_cross[3]], | |
| label="Normal Vector", | |
| color=RGBA(0/255, 0/255, 255, 1) | |
| ) | |
| # the tangent plane: dot product of the cross product | |
| tangent_plane(x,y) = ( | |
| d_cross[1]*(x-x_0) + | |
| d_cross[2]*(y-y_0) - | |
| d_cross[3]*z_0 | |
| )/d_cross[3] | |
| # range of the tangent to plot | |
| x_range_tangent = range(-3, stop=3, length=100) | |
| y_range_tangent = range(-3, stop=3, length=100) | |
| z_range_tangent = [tangent_plane(a,b) for a in x_range_tangent, b in y_range_tangent] | |
| # plot tangent | |
| surface!( | |
| x_range_tangent, | |
| y_range_tangent, | |
| z_range_tangent, | |
| label="Tangent Plane", | |
| color=RGBA(0, 0, 0, 0.3) | |
| ) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment