作为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)

????

生成的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()

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

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

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

  1. python用函数绘制椭圆_在Python中绘制椭圆轨道(使用numpy,matplotlib)

    我想知道如何使用公式ay2 bxy cx dy e = x2绘制椭圆轨道? 我首先确定了a,b,c,d,e常数,现在我假设通过给出x值我将获得y,这将给出我想要的图形,但我不能通过使用matplotl ...

  2. python画椭圆的逻辑_Python plt画椭圆

    1.计算椭圆轨迹的方法: def get_ellipse(e_x, e_y, a, b, e_angle): """[summary] 获取椭圆轨迹 Args: e_x ...

  3. 用python画苹果的代码_使用python画个小猪佩奇的示例代码

    基本原理 选好画板大小,设置好画笔颜色.粗细,定位好位置,依次画鼻子.头.耳朵.眼睛.腮.嘴.身体.手脚.尾巴,完事儿. 都知道,Turtle 是 Python 内置的一个比较有趣味的模块,俗称&qu ...

  4. 用python画时序图源代码_使用python实现画AR模型时序图

    背景: 用python画AR模型的时序图. 结果: 代码: import numpy as np import matplotlib.pyplot as plt """ ...

  5. 用python画微信捂脸_用 Python 画一个捂脸表情

    微信中的捂脸表情相信大家都不陌生,我见过以及自己使用这个表情的频率都是比较高的,可以说这个表情算是大部分人的主打表情之一了,本文我使用 Python 来画一下这个表情,我们使用到的库还是 turtle ...

  6. 用python画动态皮卡丘_利用Python绘制萌萌哒的皮卡丘

    开发工具 Python版本:3.6.4 相关模块: turtle模块. 环境搭建 安装Python并添加到环境变量即可. 在cmd窗口运行"pikachu.py"文件即可. 效果如 ...

  7. python画一颗心_利用python画一颗心的方法示例

    前言 Python一般使用Matplotlib制作统计图形,用它自己的说法是'让简单的事情简单,让复杂的事情变得可能'.用它可以制作折线图,直方图,条形图,散点图,饼图,谱图等等你能想到的和想不到的统 ...

  8. python画星空的程序_用python画星空源代码是什么?

    用python画星空源代码是什么? 用python画星空源代码是from turtle import * from random import random,randint screen = Scre ...

  9. python画有权重网络图_使用Python的networkx绘制精美网络图教程

    最近因为数学建模3天速成Python,然后做了一道网络的题,要画网络图.在网上找了一些,发现都是一些很基础的丑陋红点图,并且关于网络的一些算法也没有讲,于是自己进http://networkx.git ...

最新文章

  1. 【深度学习理论】(2) 卷积神经网络
  2. 最受欢迎 Top 12 Python 开源框架,你都用过吗?
  3. pig安装部署_【视频系列】K3S 入门到生产部署
  4. java面试题3(java基础)
  5. Spring @Bean and @Configuration
  6. python输入框_selenium+python 对输入框的输入处理方法
  7. freeswitch 安装 fail2ban 动态拦截IP攻击
  8. JS or C#?不存在的脚本之争
  9. js 值类型和引用类型
  10. 亿级APP支付宝在移动端的高可用技术实践
  11. KNN(k-nearest neighbor algorithm)--从原理到实现
  12. 用Python实现一个简单的加密程序
  13. 二分法中为啥常用mid=beg+(end-beg)/2 而非mid=(beg+end)/2 ?
  14. weka使用训练集分类测试集_技术分享
  15. v-distpicker的简单安装和使用 — 一款地区选择插件
  16. 地方门户类网站建设解决方案
  17. 一位AI大佬给算法校招同学的建议:观点篇
  18. 用过最好用的swf格式视频转换器,swf转成mp4
  19. Windows打印机共享相关问题
  20. Fluent的表达式应用

热门文章

  1. Effective Java(一)—— 创建和销毁对象
  2. auxiliary variable(辅助变量)的引入
  3. 茶 —— 一片树叶的故事(茶叶的品类)
  4. 电梯、电梯调度与电梯问题
  5. IPython notebook(Jupyter Notebook) 的使用
  6. Python 数据结构与算法——侏儒排序
  7. C 标准库—— string.h
  8. ROC曲线与AUC区域的理解与实践
  9. mysql读写分离java配置方法_springboot配置数据库读写分离
  10. lisp 任意点 曲线距离_lisp程序能求多段线上任意一点到其中一个端点的距离,,每次要量桩号很烦...哪位大侠给个..谢谢了...........