从cu中掉出来的点

2024-05-14 22:45:52 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我必须检查一个点是否在半轴a和b的椭圆内。我生成了一个元组列表(点),然后生成了另一个元组列表(点),其中我只保留椭圆内的那些点

但是,生成时,有些点会从椭圆中掉出来。这个误差是通过计算累积的吗?如果是的话,我该如何改进它,使点不会从曲线中掉出来

请注意,我对python有点生疏,有些东西对我来说并不明显。 提前谢谢

    dots=[(random.uniform(-a,a),random.uniform(-b,b)) for i in range(1000)]#;dots
    dotsin=[(x,y) for x,y in dots if (x**2 + y**2)<((a*cos(atan(y/x)))**2 + (b*sin(atan(y/x)))**2)]#;dotsin
    plt.scatter([x[0] for x in dotsin],[y[1] for y in dotsin])
    plt.grid()

Tags: in列表forpltrandomuniform曲线误差
1条回答
网友
1楼 · 发布于 2024-05-14 22:45:52

你的条件(x**2 + y**2)<((a*cos(atan(y/x)))**2 + (b*sin(atan(y/x)))**2)等价于(x**2 + y**2)**2 < (a*x) ** 2 + (b*y) ** 2。这不是椭圆方程。椭圆方程是(x/a)**2 + (y/b)**2 < 1

from numpy import random
import numpy as np
from math import sin, cos, atan
import matplotlib.pyplot as plt
%matplotlib inline
a = 3
b = 2
n = 1000
dots=[(random.uniform(-a,a),random.uniform(-b,b)) for i in range(n)]#;dots
dotsin=[(x,y) for x,y in dots if (x/a)**2 + (y/b)**2 < 1]#;dotsin
plt.scatter([x[0] for x in dotsin],[y[1] for y in dotsin])
phi = np.linspace(0, 2*np.pi, 200)
plt.plot(a*np.cos(phi), b*np.sin(phi))
plt.grid()

ellips

相关问题 更多 >

    热门问题