国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python+matplotlib演示電偶極子實例代碼

2020-02-22 22:51:16
字體:
來源:轉載
供稿:網友

使用matplotlib.tri.CubicTriInterpolator.演示變化率計算:

完整實例:

from matplotlib.tri import (  Triangulation, UniformTriRefiner, CubicTriInterpolator)import matplotlib.pyplot as pltimport matplotlib.cm as cmimport numpy as np#-----------------------------------------------------------------------------# Electrical potential of a dipole#-----------------------------------------------------------------------------def dipole_potential(x, y):  """ The electric dipole potential V """  r_sq = x**2 + y**2  theta = np.arctan2(y, x)  z = np.cos(theta)/r_sq  return (np.max(z) - z) / (np.max(z) - np.min(z))#-----------------------------------------------------------------------------# Creating a Triangulation#-----------------------------------------------------------------------------# First create the x and y coordinates of the points.n_angles = 30n_radii = 10min_radius = 0.2radii = np.linspace(min_radius, 0.95, n_radii)angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)angles[:, 1::2] += np.pi / n_anglesx = (radii*np.cos(angles)).flatten()y = (radii*np.sin(angles)).flatten()V = dipole_potential(x, y)# Create the Triangulation; no triangles specified so Delaunay triangulation# created.triang = Triangulation(x, y)# Mask off unwanted triangles.triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),             y[triang.triangles].mean(axis=1))        < min_radius)#-----------------------------------------------------------------------------# Refine data - interpolates the electrical potential V#-----------------------------------------------------------------------------refiner = UniformTriRefiner(triang)tri_refi, z_test_refi = refiner.refine_field(V, subdiv=3)#-----------------------------------------------------------------------------# Computes the electrical field (Ex, Ey) as gradient of electrical potential#-----------------------------------------------------------------------------tci = CubicTriInterpolator(triang, -V)# Gradient requested here at the mesh nodes but could be anywhere else:(Ex, Ey) = tci.gradient(triang.x, triang.y)E_norm = np.sqrt(Ex**2 + Ey**2)#-----------------------------------------------------------------------------# Plot the triangulation, the potential iso-contours and the vector field#-----------------------------------------------------------------------------fig, ax = plt.subplots()ax.set_aspect('equal')# Enforce the margins, and enlarge them to give room for the vectors.ax.use_sticky_edges = Falseax.margins(0.07)ax.triplot(triang, color='0.8')levels = np.arange(0., 1., 0.01)cmap = cm.get_cmap(name='hot', lut=None)ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap=cmap,       linewidths=[2.0, 1.0, 1.0, 1.0])# Plots direction of the electrical vector fieldax.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,     units='xy', scale=10., zorder=3, color='blue',     width=0.007, headwidth=3., headlength=4.)ax.set_title('Gradient plot: an electrical dipole')plt.show()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青冈县| 清原| 定西市| 社会| 嘉祥县| 澄江县| 保德县| 都兰县| 阿鲁科尔沁旗| 侯马市| 巴东县| 雅江县| 惠安县| 青铜峡市| 大竹县| 中宁县| 泸水县| 九龙县| 崇文区| 石门县| 辛集市| 九龙坡区| 庆城县| 信宜市| 九龙县| 抚顺市| 鄂托克旗| 阳泉市| 政和县| 射洪县| 新民市| 永新县| 乌兰察布市| 寿宁县| 洛阳市| 新密市| 乳山市| 玉环县| 沛县| 昌都县| 怀仁县|