以下公式用于对二维空间中的点进行分类:
f(x1,x2) = np.sign(x1^2+x2^2-.6)
字符串所有的点都在空间X = [-1,1] x [-1,1]中,并且每个x的概率都是一致的。现在我想想象一个等于:
X = [-1,1] x [-1,1]
0 = x1^2+x2^2-.6
型x1的值应在x轴上,x2的值应在y轴上。这一定是可能的,但我很难把方程转化为一个情节。
nc1teljy1#
您可以使用等值线图,如下所示(基于http://matplotlib.org/examples/pylab_examples/contour_demo.html的示例):
import numpy as npimport matplotlib.pyplot as pltx = np.linspace(-1.0, 1.0, 100)y = np.linspace(-1.0, 1.0, 100)X, Y = np.meshgrid(x,y)F = X**2 + Y**2 - 0.6plt.contour(X,Y,F,[0])plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1.0, 1.0, 100)
y = np.linspace(-1.0, 1.0, 100)
X, Y = np.meshgrid(x,y)
F = X**2 + Y**2 - 0.6
plt.contour(X,Y,F,[0])
plt.show()
字符串这将产生下图
的数据最后是一些一般性发言:
x^2
x**2
x1
x2
plt.gca().set_aspect('equal')
pgpifvop2#
@BasJansen的解决方案当然能让你达到目的,它要么效率很低(如果你使用很多网格点),要么不准确(如果你只使用很少的网格点)。你可以很容易地直接画出圆。给定0 = x1**2 + x**2 - 0.6,就可以得出x2 = sqrt(0.6 - x1**2)(正如Dux所说)。但你真正想做的是,把你的笛卡尔坐标,转换成极坐标。
0 = x1**2 + x**2 - 0.6
x2 = sqrt(0.6 - x1**2)
x1 = r*cos(theta)x2 = r*sin(theta)
x1 = r*cos(theta)
x2 = r*sin(theta)
字符串如果你在圆的等式中使用这些替换,你会看到r=sqrt(0.6)。所以现在你可以用它来绘制你的图:
r=sqrt(0.6)
import numpy as npimport matplotlib.pyplot as plt# theta goes from 0 to 2pitheta = np.linspace(0, 2*np.pi, 100)# the radius of the circler = np.sqrt(0.6)# compute x1 and x2x1 = r*np.cos(theta)x2 = r*np.sin(theta)# create the figurefig, ax = plt.subplots(1)ax.plot(x1, x2)ax.set_aspect(1)plt.show()
# theta goes from 0 to 2pi
theta = np.linspace(0, 2*np.pi, 100)
# the radius of the circle
r = np.sqrt(0.6)
# compute x1 and x2
x1 = r*np.cos(theta)
x2 = r*np.sin(theta)
# create the figure
fig, ax = plt.subplots(1)
ax.plot(x1, x2)
ax.set_aspect(1)
型测试结果:x1c 0d1x的数据
p1iqtdky3#
如何绘制x值并计算相应的y值?
import numpy as npimport matplotlib.pyplot as pltx = np.linspace(-1, 1, 100, endpoint=True)y = np.sqrt(-x**2. + 0.6)plt.plot(x, y)plt.plot(x, -y)
x = np.linspace(-1, 1, 100, endpoint=True)
y = np.sqrt(-x**2. + 0.6)
plt.plot(x, y)
plt.plot(x, -y)
字符串产生
的数据很明显,这可以做得更好,但这只是为了演示...
92dk7w1h4#
# x**2 + y**2 = r**2r = 6x = np.linspace(-r,r,1000)y = np.sqrt(-x**2+r**2)plt.plot(x, y,'b')plt.plot(x,-y,'b')plt.gca().set_aspect('equal')plt.show()
# x**2 + y**2 = r**2
r = 6
x = np.linspace(-r,r,1000)
y = np.sqrt(-x**2+r**2)
plt.plot(x, y,'b')
plt.plot(x,-y,'b')
字符串生产:
的数据
4dc9hkyq5#
思想:将一个点乘以复指数()会旋转圆上的点
import numpy as npimport matplotlib.pyplot as pltnum_pts=20 # number of points on the circleps = np.arange(num_pts+1)# j = np.sqrt(-1)pts = np.exp(2j*np.pi/num_pts *(ps))fig, ax = plt.subplots(1)ax.plot(pts.real, pts.imag , '-o')ax.set_aspect(1)plt.show()
num_pts=20 # number of points on the circle
ps = np.arange(num_pts+1)
# j = np.sqrt(-1)
pts = np.exp(2j*np.pi/num_pts *(ps))
ax.plot(pts.real, pts.imag , '-o')
字符串
的
5条答案
按热度按时间nc1teljy1#
您可以使用等值线图,如下所示(基于http://matplotlib.org/examples/pylab_examples/contour_demo.html的示例):
字符串
这将产生下图
的数据
最后是一些一般性发言:
x^2
并不意味着你认为它在python中是什么,你必须使用x**2
。x1
和x2
(对我来说)是非常误导的,特别是如果你说x2
必须在y轴上。1.(感谢Dux)您可以添加
plt.gca().set_aspect('equal')
,通过使轴相等,使图形实际上看起来是圆形的。pgpifvop2#
@BasJansen的解决方案当然能让你达到目的,它要么效率很低(如果你使用很多网格点),要么不准确(如果你只使用很少的网格点)。
你可以很容易地直接画出圆。给定
0 = x1**2 + x**2 - 0.6
,就可以得出x2 = sqrt(0.6 - x1**2)
(正如Dux所说)。但你真正想做的是,把你的笛卡尔坐标,转换成极坐标。
字符串
如果你在圆的等式中使用这些替换,你会看到
r=sqrt(0.6)
。所以现在你可以用它来绘制你的图:
型
测试结果:
x1c 0d1x的数据
p1iqtdky3#
如何绘制x值并计算相应的y值?
字符串
产生
的数据
很明显,这可以做得更好,但这只是为了演示...
92dk7w1h4#
字符串
生产:
的数据
4dc9hkyq5#
使用复数绘制圆
思想:将一个点乘以复指数(

)会旋转圆上的点
字符串
的