从polyfit查找曲线的交点 [英] Finding the intersection of a curve from polyfit

查看:103
本文介绍了从polyfit查找曲线的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但我不太清楚.我有一条根据x,y数据计算的曲线.然后我有一行.我想找到两个相交处的x,y值.

This seems simple but I can't quite figure it out. I have a curve calculated from x,y data. Then I have a line. I want to find the x, y values for where the two intersect.

这是我到目前为止所掌握的.这非常令人困惑,并且无法给出正确的结果.我可以看一下图,找到交点x值,然后计算正确的y值.我想删除此人工步骤.

Here is what I've got so far. It's super confusing and doesn't give the correct result. I can look at the graph and find the intersection x value and calculate the correct y value. I'd like to remove this human step.

import numpy as np
import matplotlib.pyplot as plt
from pylab import * 
from scipy import linalg
import sys
import scipy.interpolate as interpolate
import scipy.optimize as optimize

w = np.array([0.0, 11.11111111111111, 22.22222222222222, 33.333333333333336, 44.44444444444444, 55.55555555555556, 66.66666666666667, 77.77777777777777, 88.88888888888889, 100.0])
v = np.array([0.0, 8.333333333333332, 16.666666666666664, 25.0, 36.11111111111111, 47.22222222222222, 58.333333333333336, 72.22222222222221, 86.11111111111111, 100.0])

z = np.polyfit(w, v, 2)
print (z)
p=np.poly1d(z)
g = np.polyval(z,w)
print (g)
N=100
a=arange(N)
b=(w,v)
b=np.array(b)
c=(w,g)
c=np.array(c)
print(c)
d=-a+99
e=(a,d)
print (e)
p1=interpolate.PiecewisePolynomial(w,v[:,np.newaxis])
p2=interpolate.PiecewisePolynomial(w,d[:,np.newaxis])

def pdiff(x):
    return p1(x)-p2(x)

xs=np.r_[w,w]
xs.sort()
x_min=xs.min()
x_max=xs.max()
x_mid=xs[:-1]+np.diff(xs)/2
roots=set()
for val in x_mid:
    root,infodict,ier,mesg = optimize.fsolve(pdiff,val,full_output=True)
    # ier==1 indicates a root has been found
    if ier==1 and x_min<root<x_max:
        roots.add(root[0])
roots=list(roots)        
print(np.column_stack((roots,p1(roots),p2(roots))))

plt.plot(w,v, 'r', a, -a+99, 'b-')
plt.show()
q=input("what is the intersection value? ")
print (p(q))

有什么想法可以使它起作用吗?

Any ideas to get this to work?

谢谢

推荐答案

我不认为我完全理解您在代码中尝试做的事情,但是用英语描述的内容可以做到

I don't think I fully understand what you are trying to do in your code, but what you described in English can be done with

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

w = np.array([0.0, 11.11111111111111, 22.22222222222222, 33.333333333333336,
              44.44444444444444, 55.55555555555556, 66.66666666666667,
              77.77777777777777, 88.88888888888889, 100.0])
v = np.array([0.0, 8.333333333333332, 16.666666666666664, 25.0,
              36.11111111111111, 47.22222222222222, 58.333333333333336,
              72.22222222222221, 86.11111111111111, 100.0])

poly_coeff = np.polynomial.polynomial.polyfit(w, v, 2)
poly = np.polynomial.polynomial.Polynomial(poly_coeff)
roots = np.polynomial.polynomial.polyroots(poly_coeff - [99, -1, 0])

x = np.linspace(np.min(roots) - 50, np.max(roots) + 50, num=1000)
plt.plot(x, poly(x), 'r-')
plt.plot(x, 99 - x, 'b-')
for root in roots:
    plt.plot(root, 99 - root, 'ro')

这篇关于从polyfit查找曲线的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆