Plot a 3D figure with different colors of a System of three Equations using SageMath

asked 2024-02-19 16:26:34 +0100

MKS gravatar image

updated 2024-02-19 16:53:43 +0100

Variable Definition:

Three variables x, y, and z are defined symbolically using the var function. These variables represent the coordinates in three-dimensional space. Equation Definition:

Three equations representing planes are defined: eq1: x + 2y + 4z = 7 eq2: 3x+7y+2z=-11 eq3: 2x+3y+3z=1 Plotting the Planes:

Each equation is plotted in a different color with the implicit_plot3d function. The color parameter specifies the color of the plane, and the opacity parameter sets the transparency level. In this case, each plane is set to have a transparency of 0.5, making them partially see-through. Labeling the Planes:

Each plane is labeled with its equation using the text3d function. The labels are positioned at specific coordinates in the three-dimensional space to avoid overlap. Adding Arrows:

Arrows representing the direction of the normal vectors to each plane are added. The direction of each arrow corresponds to the coefficients of the normal vectors derived from the equations. Displaying the Plot:

All the components (planes, labels, and arrows) are combined using the + operator and displayed using the show function. Overall, the code generates a 3D plot showing three planes with transparency applied to each plane separately, along with labels indicating their equations and arrows indicating the direction of their normal vectors.

# Define variables
x, y, z = var('x,y,z')

# Define equations
eq1 = x + 2*y + 4*z == 7
eq2 = 3*x + 7*y + 2*z == -11
eq3 = 2*x + 3*y + 3*z == 1

# Plot each plane in a different color with transparency and label them
plane1 = implicit_plot3d(eq1, (x, -10, 10), (y, -10, 10), (z, -10, 10), color='blue', opacity=0.5)
plane2 = implicit_plot3d(eq2, (x, -10, 10), (y, -10, 10), (z, -10, 10), color='green', opacity=0.5)
plane3 = implicit_plot3d(eq3, (x, -10, 10), (y, -10, 10), (z, -10, 10), color='red', opacity=0.5)

# Label each plane with its equation using text3d
label1 = text3d("x + 2*y + 4*z == 7", (8, 5, 8), color='blue')
label2 = text3d("3*x + 7*y + 2*z == -11", (-8, -5, 8), color='green')
label3 = text3d("2*x + 3*y + 3*z == 1", (-8, 5, -8), color='red')

# Add arrows indicating the direction of the normal vectors
arrow1 = arrow3d((8, 5, 8), (8 + 1, 5 + 2, 8 + 4), color='blue')
arrow2 = arrow3d((-8, -5, 8), (-8 + 3, -5 + 7, 8 + 2), color='green')
arrow3 = arrow3d((-8, 5, -8), (-8 + 2, 5 + 3, -8 + 3), color='red')

# Show the plots
show(plane1 + plane2 + plane3 + label1 + label2 + label3 + arrow1 + arrow2 + arrow3)

Output: image description

edit retag flag offensive close merge delete