英文:
How to plot color plot or density plot for complex numbers?
问题
I can provide a translation of the code-related portions:
我有一个名为```res```的函数,它以某个```omega```变量的复杂值作为输入,并返回一个连分数残差的绝对值(也是复杂的)。
我想要测试```res```函数对各种omega值的反应,并查看哪些值产生最接近零的残差值。因此,我想要查看哪个omega对应于残差连分数的根。因此,我生成了以下omega列表:
```python
N = 400
omega = [complex(np.random.uniform(0.001, 15), np.random.uniform(0, 2)) for i in range(N)]
然后,我将每个omega[i]
值应用于res
函数。对于omega列表:
omega = [(8.89186304186803+1.8580361935398448j), (4.250554058118386+0.34692636983137204j),(13.77975011058525+0.4067498913305867j), (11.848491447761512+1.1353493824887473j), (5.368197310760776+1.603262808639006j), (7.953834937525846+0.31087387982821735j),
(12.589975089023985+1.9010925629632527j),(0.8533967396680111+0.9083830823882479j),
(11.966071152073017+1.7535358294965886j), (6.723597895069045+0.5943732982185757j)]
我得到了相应残差的绝对值:
list_res = [2.1663149855057022, 12.202467603015915, 13.105388388645999, 3.524702335556132, 5.007101216080126, 2.1989779961023457, 4.6753139694285775, 1.82871868615581, 3.8247209107872857, 2.977690850044372]
我想生成一个颜色图或密度图,其中包含omega值和相应残差的绝对值。我的目标是找到局部最小点(残差最接近零的点)。
我尝试使用spb
库的plot_complex
,但显然只能绘制符号函数。
我想制作一个类似下面的图。其中黑点是res
最小的值。
<details>
<summary>英文:</summary>
I have a ```res``` function that takes as input complex value of a certain ```omega``` variable and returns the absolute value of the residue (also complex) of a continued fraction.
I'd like to test the ```res``` function for various omega values and see which of those values yields a residual value closest to zero. So I would see which omega corresponds, which would be, in a sense, the root of the equation for the residue of the continued function. Therefore, I generated a list of omegas as follows:
N = 400
omega = [complex(np.random.uniform(0.001, 15), np.random.uniform(0, 2)) for i in range(N)]
And I imposed each ```omega[i]``` value on the ```res``` function. Truncating the lists, for the omega list
omega = [(8.89186304186803+1.8580361935398448j), (4.250554058118386+0.34692636983137204j),(13.77975011058525+0.4067498913305867j), (11.848491447761512+1.1353493824887473j), (5.368197310760776+1.603262808639006j), (7.953834937525846+0.31087387982821735j),
(12.589975089023985+1.9010925629632527j),(0.8533967396680111+0.9083830823882479j),
(11.966071152073017+1.7535358294965886j), (6.723597895069045+0.5943732982185757j)]
I got the following the absolute value of the corresponding residuals
list_res = [2.1663149855057022, 12.202467603015915, 13.105388388645999, 3.524702335556132, 5.007101216080126, 2.1989779961023457, 4.6753139694285775, 1.82871868615581, 3.8247209107872857, 2.977690850044372]
I want to generate a color plot or density plot with the omega values and the respective absolute values of the residual. The idea is to locate the local minimum points (the points where the residual is closest to zero)
I tried using ```plot_comples``` from the ```spb``` library. But apparently you can only plot symbolic functions.
I would like to make a plot similar to the plot below. Where the black dots are the values where ```res``` is minimum.
[Color plot][1]
[1]: https://i.stack.imgur.com/dDK3b.png
</details>
# 答案1
**得分**: 1
这是[`spb`](https://sympy-plot-backends.readthedocs.io/en/latest/)创建这种类型的可视化的方式:
1. 在复平面中创建一组角速度(omega)值的网格。
2. 在该复杂网格上评估函数。
3. 计算结果的幅值,其范围从0到(可能)无穷大。通常数值范围很大,因此绘制幅值不是一个好主意,因为会得到难以理解的图像...
4. 相反,我们使用以下公式计算亮度:`b = m / (m + 1)`,其中`m`是幅值。请注意,当`m=0`时,`b`为零,当`m`趋近于无穷大时,`b`趋近于一。
5. 绘制亮度。
这是一个示例:
```py
import numpy as np
import matplotlib.pyplot as plt
func = lambda w: np.sin(w**2)
n = 200j
re_w, im_w = np.mgrid[-3:3:n, -3:3:n]
w = re_w + 1j * im_w
res = func(w)
mag = np.absolute(res)
brightness = mag / (mag + 1)
fig, ax = plt.subplots()
img = ax.imshow(brightness, cmap="gray", vmin=brightness.min(), vmax=brightness.max())
cbar = fig.colorbar(img, label="brightness")
英文:
This is how spb
creates that kind of visualization:
- create a grid of omega values in the complex plane.
- evaluate the function over that complex grid.
- compute the magnitude of the result, which varies from 0 to (possibly) infinity. The range in values is usually large, so it's not a good idea to plot the magnitude because you'd end up with an image difficult to understand...
- Instead, we compute the brightness with this formula:
b = m / (m + 1)
wherem
is the magnitude. Note thatb
is zero whenm=0
andb
tends to one whenm
tends to infinity. - plot the brightness.
Here is an example:
import numpy as np
import matplotlib.pyplot as plt
func = lambda w: np.sin(w**2)
n = 200j
re_w, im_w = np.mgrid[-3:3:n, -3:3:n]
w = re_w + 1j * im_w
res = func(w)
mag = np.absolute(res)
brightness = mag / (mag + 1)
fig, ax = plt.subplots()
img = ax.imshow(brightness, cmap="gray", vmin=brightness.min(), vmax=brightness.max())
cbar = fig.colorbar(img, label="brightness")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论