Lagrange Example - 3d Surface with a constraint curve and lifted constraint curve
5 min read
Description
This Python script uses matplotlib to visualize a 3D surface (a paraboloid) with a squiggled constraint curve. The curve is lifted onto the surface, with two points highlighted: one at the maximum and one at the minimum of the curve. Below is an explanation of the main steps:
Image
Code
Explanation Step By Step
Imports, using micropip for the virtual env stuff
Function f(x, y): This function defines the surface of a paraboloid. It calculates ( z = x^2 + y^2 ) for given ( x ) and ( y ) values.
Grid Setup: Here, a grid of points is created in the ( x )- and ( y )-directions using np.linspace. The corresponding ( z )-values for each ( (x, y) ) pair are calculated using the paraboloid function f(x, y).
Surface Plot: A 3D plot of the paraboloid is created using ax.plot_surface(). The rstride and cstride parameters control the resolution of the mesh, and alpha sets the transparency.
Squiggled Constraint Curve: The function squiggled_constraint generates a modified version of the constraint curve by adjusting the radius ( r ) based on a sinusoidal term ( r = r_0 + A \sin(f \theta) ). This creates the squiggling effect.
amplitude controls how much the curve moves in or out.
frequency determines how many squiggles appear around the curve.
Generate Squiggled Curve: This line computes the new ( x )- and ( y )-coordinates for the squiggled curve using the squiggled_constraint function.
Lift the Curve to the Surface: The ( z )-coordinates for the squiggled curve are calculated by passing the squiggled ( x )- and ( y )-coordinates into the surface function ( z = f(x, y) ).
Find Maximum and Minimum Points:
np.argmax(z_squiggled) finds the index of the point on the squiggled curve with the highest ( z )-value (maximum).
np.argmin(z_squiggled) finds the index of the point with the lowest ( z )-value (minimum).
Plot Maximum Point: The maximum point is plotted with a green dot (ax.scatter) and labeled “Maximum” slightly above the point (ax.text).
Plot Minimum Point: Similarly, the minimum point is plotted with a blue dot and labeled “Minimum.”
Labels and Titles: Standard axis labels and a plot title are added for clarity.
View Angle: The ax.view_init function adjusts the viewing angle of the 3D plot to give a good perspective of both the surface and the squiggled curve.