python画椭圆的逻辑_在Python中绘制椭圆轨道(使用numpy,matplotlib)

作为x的函数,y可以解决该问题

问题是每个有效x有2个y值,而椭圆跨度x范围外没有(或想象)y解

下面是3.5代码,sympy 1.0应该可以打印,list comps可能不会向后兼容到2.x.

from numpy import linalg

from numpy import linspace

import numpy as np

from numpy import meshgrid

import random

import matplotlib.pyplot as plt

from scipy import optimize

from sympy import *

xs = [1.02, 0.95, 0.87, 0.77, 0.67, 0.56, 0.44, 0.30, 0.16, 0.01]

ys = [0.39, 0.32, 0.27, 0.22, 0.18, 0.15, 0.13, 0.12, 0.12, 0.15]

b = [i ** 2 for i in xs] # That is the list that contains the results that are given as x^2 from the equation.

def fxn(x, y): # That is the function that solves the given equation to find each parameter.

my_list = [] #It is the main list.

for z in range(len(x)):

w = [0] * 5

w[0] = y[z] ** 2

w[1] = x[z] * y[z]

w[2] = x[z]

w[3] = y[z]

w[4] = 1

my_list.append(w)

return my_list

t = linalg.lstsq(fxn(xs, ys), b)

def ysolv(coeffs):

x,y,a,b,c,d,e = symbols('x y a b c d e')

ellipse = a*y**2 + b*x*y + c*x + d*y + e - x**2

y_sols = solve(ellipse, y)

print(*y_sols, sep='

')

num_coefs = [(a, f) for a, f in (zip([a,b,c,d,e], coeffs))]

y_solsf0 = y_sols[0].subs(num_coefs)

y_solsf1 = y_sols[1].subs(num_coefs)

f0 = lambdify([x], y_solsf0)

f1 = lambdify([x], y_solsf1)

return f0, f1

f0, f1 = ysolv(t[0])

y0 = [f0(x) for x in xs]

y1 = [f1(x) for x in xs]

plt.scatter(xs, ys)

plt.scatter(xs, y0, s=100, color = 'red', marker='+')

plt.scatter(xs, y1, s=100, color = 'green', marker='+')

plt.show()

当上述内容在Spyder中运行时:

runfile('C:/Users/john/mypy/mySE_answers/ellipse.py', wdir='C:/Users/john/mypy/mySE_answers')

(-b*x - d + sqrt(-4*a*c*x - 4*a*e + 4*a*x**2 + b**2*x**2 + 2*b*d*x + d**2))/(2*a)

-(b*x + d + sqrt(-4*a*c*x - 4*a*e + 4*a*x**2 + b**2*x**2 + 2*b*d*x + d**2))/(2*a)

T6eUy.png

????

生成的y值函数无处不在:

f0(0.1), f1(0.1)

Out[5]: (0.12952825130864626, 0.6411040771593166)

f0(2)

Traceback (most recent call last):

File "", line 1, in

f0(2)

File "", line 1, in

ValueError: math domain error

In [7]:

域错误需要try / execpt来“感觉”有效的x范围或更多的数学

喜欢尝试/除了下面:(编辑为“关闭”绘图重新评论)

def feeloutXrange(f, midx, endx):

fxs = []

x = midx

while True:

try: f(x)

except:

break

fxs.append(x)

x += (endx - midx)/100

return fxs

midx = (min(xs) + max(xs))/2

xpos = feeloutXrange(f0, midx, max(xs))

xnegs = feeloutXrange(f0, midx, min(xs))

xs_ellipse = xnegs[::-1] + xpos[1:]

y0s = [f0(x) for x in xs_ellipse]

y1s = [f1(x) for x in xs_ellipse]

ys_ellipse = y0s + y1s[::-1] + [y0s[0]] # add y start point to end to close drawing

xs_ellipse = xs_ellipse + xs_ellipse[::-1] + [xs_ellipse[0]] # added x start point

plt.scatter(xs, ys)

plt.scatter(xs, y0, s=100, color = 'red', marker='+')

plt.scatter(xs, y1, s=100, color = 'green', marker='+')

plt.plot(xs_ellipse, ys_ellipse)

plt.show()

wRT7l.png

编辑:将重复的起点添加到椭圆点列表的末尾以关闭绘图

ys_ellipse = y0s + y1s[::-1] + [y0s[0]] # add y start point to end to close drawing

xs_ellipse = xs_ellipse + xs_ellipse[::-1] + [xs_ellipse[0]] # added x start point

P8Grs.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值