使用Python与Mayavi可视化复函数z1^n+z2^n=1无显示问题求助
Fixing the No-Display Issue When Visualizing the Complex Function $z_1^n + z_2^n = 1$ with Mayavi
Hey there, I've looked into your code and found a couple of key issues that are causing Mayavi to show nothing. Let's break down what's wrong and fix it step by step:
What's Going Wrong?
- 1D Grid Can't Generate a Surface: Your line
theta = np.mgrid[0:pi/2]creates a 1D array, butmlab.mesh()requires 2D arrays to render a surface. Without a 2D grid, there's no surface for Mayavi to draw. - Unnecessary Complexity in Complex Math: The expression
np.cos(theta*xi*1j)is mathematically equivalent tonp.cosh(theta*xi)(since $\cos(ix) = \cosh(x)$). Using the hyperbolic cosine directly avoids confusion and potential issues with complex exponentiation. - Unused Variables: You defined
z14andz24but didn't plot them—this isn't the main issue, but it's a waste of code.
Fixed Code
import numpy as np from mayavi import mlab pi = np.pi n = 2 xi = 3 # Create a 2D grid with 100 samples along each axis for a smooth surface theta, phi = np.mgrid[0:pi/2:100j, 0:pi/2:100j] k1 = np.array([2, 3]) k2 = np.array([0, 1]) # Precompute the complex phases to clean up the code phase_k1_0 = np.exp((2 * k1[0] * pi / n) * 1j) phase_k1_1 = np.exp((2 * k1[1] * pi / n) * 1j) phase_k2_0 = np.exp((2 * k2[0] * pi / n) * 1j) phase_k2_1 = np.exp((2 * k2[1] * pi / n) * 1j) # Simplify using hyperbolic functions instead of complex cosine/sine cosh_term = np.cosh(theta * xi) ** (2 / n) sinh_term = np.sinh(theta * xi) ** (2 / n) # Calculate coordinates for each of the four solution sets # Set 1 z11 = phase_k1_0 * cosh_term z21 = phase_k2_0 * sinh_term x11, x21, x31 = z11.real, z11.imag, z21.real # Set 2 z12 = phase_k1_0 * cosh_term z22 = phase_k2_1 * sinh_term x12, x22, x32 = z12.real, z12.imag, z22.real # Set 3 z13 = phase_k1_1 * cosh_term z23 = phase_k2_0 * sinh_term x13, x23, x33 = z13.real, z13.imag, z23.real # Set 4 z14 = phase_k1_1 * cosh_term z24 = phase_k2_1 * sinh_term x14, x24, x34 = z14.real, z14.imag, z24.real # Plot each surface with a unique color for clarity mlab.mesh(x11, x21, x31, color=(1, 0, 0)) mlab.mesh(x12, x22, x32, color=(0, 1, 0)) mlab.mesh(x13, x23, x33, color=(0, 0, 1)) mlab.mesh(x14, x24, x34, color=(1, 1, 0)) # Add axis labels to make the plot easier to interpret mlab.xlabel('Re(z₁)') mlab.ylabel('Im(z₁)') mlab.zlabel('Re(z₂)') mlab.show()
Key Fixes Explained
- 2D Grid: By using
np.mgrid[0:pi/2:100j, 0:pi/2:100j], we create a 2D grid of points. The100jtells NumPy to generate 100 evenly spaced points along each axis, which gives a smooth surface. - Simplified Hyperbolic Functions: Replacing complex cosine with
np.cosh()makes the code easier to read and avoids any unexpected behavior from complex number operations. - Color-Coded Surfaces: Assigning different colors to each surface helps you distinguish between the four different solution sets defined by your
k1andk2values. - Axis Labels: These make it clear which axis corresponds to the real/imaginary parts of your complex variables.
When you run this fixed code, you should see the four surfaces for $z_1^2 + z_2^2 = 1$ render correctly in Mayavi.
内容的提问来源于stack exchange,提问作者Silvio
相关产品推荐
相关产品推荐

