Skip to content

Instantly share code, notes, and snippets.

@louismeunier
Last active August 5, 2025 17:38
Show Gist options
  • Select an option

  • Save louismeunier/b69e77565b140c8d5516f0b8f50a1312 to your computer and use it in GitHub Desktop.

Select an option

Save louismeunier/b69e77565b140c8d5516f0b8f50a1312 to your computer and use it in GitHub Desktop.
# julia
# MATH150 Assignment 5, Problem 9a
begin
using Plots; plotly()
# general "settings"
t_range = range(0.95, stop=1.05, length = 100)
P = [2,2,1]
# F, a curve defined parametrically
x(t) = 2(t^3+2)/3
y(t) = 2t^2
z(t) = 3t-2
F_x_range = x.(t_range)
F_y_range = y.(t_range)
F_z_range = z.(t_range)
plot(F_x_range, F_y_range, F_z_range, color=RGBA(0,0,255,0.5), linewidth=3, label="F", legend=false)
# G
g(x,y) = sqrt((15-x^2-2y^2)/3)
G_x_range = range(1.9, stop=2.1, length = 100)
G_y_range = range(1.9, stop=2.1, length = 100)
# G_z_range = g.(G_x_range, G_y_range)
surface!(G_x_range, G_y_range, g, color=RGBA(255,0,0,0.5), linewidth=3, label="G", legend=true)
# plotting the point of interest
plot!(
[2],[2],[1],
marker = (:circle, 1, 0.8, :black),
label = "P(2,2,1)"
)
# I multiply each vector by a scalar to make them fit within the bounds of the functions
scalar = 0.01
# plot the gradient of G
grad_G_x(t) = 2t^2
grad_G_y(t) = 4t
grad_G_z(t) = 3
# note that t = 1 at P = (2,2,1)
plot!(
[P[1], P[1]+scalar*grad_G_x(1)],
[P[2], P[2]+scalar*grad_G_y(1)],
[P[3], P[3]+scalar*grad_G_z(1)],
color=:green,
linewidth=6,
label="grad G(2,2,1)"
)
# plot the gradient of F
grad_F_x(x,y,z) = 2x
grad_F_y(x,y,z) = 4y
grad_F_z(x,y,z) = 6z
plot!(
[2, 2+scalar*grad_F_x(P[1],P[2],P[3])],
[2, 2+scalar*grad_F_y(P[1],P[2],P[3])],
[1, 1+scalar*grad_F_z(P[1],P[2],P[3])],
color=:orange,
linewidth=6,
label="grad F(2,2,1)"
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment