Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the intersection of a curve from polyfit

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.

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?

Thanks

like image 917
user2843767 Avatar asked Oct 06 '13 23:10

user2843767


1 Answers

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')

enter image description here

like image 154
Jaime Avatar answered Sep 21 '22 23:09

Jaime