python - Finding the intersection of a curve from polyfit -


this seems simple can't quite figure out. have curve calculated x,y data. have line. want find x, y values 2 intersect.

here i've got far. it's super confusing , doesn't give correct result. can @ graph , find intersection x value , calculate correct y value. i'd remove human step.

import numpy np import matplotlib.pyplot plt pylab import *  scipy import linalg import sys import scipy.interpolate interpolate import scipy.optimize 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() val in x_mid:     root,infodict,ier,mesg = optimize.fsolve(pdiff,val,full_output=true)     # ier==1 indicates root has been found     if ier==1 , 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 intersection value? ") print (p(q)) 

any ideas work?

thanks

i don't think understand trying in code, described in english can done with

from __future__ import division import numpy np import matplotlib.pyplot 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-') root in roots:     plt.plot(root, 99 - root, 'ro') 

enter image description here


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -