-3

Is there any method or way to get the approximate equation(x = g(y)) for doing a reverse lookup from Y to X. The following is the simple y = f(x) and its plot.

import numpy as np
import matplotlib.pyplot as plt  
formula = "2*x**6 + x**5 -3*x**4 + 7*x**3 + 9*x**2 + x + 6"
x = np.arange(1, 10)
y = eval(formula)
plt.plot(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

enter image description here

Can you please suggest any possible way in R or Python to get the reverse lookup function(From Y to X) with a minimal margin of error?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • So if I understand you correctly you want the polynomial regression curve? If so have a look at [sklearn](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html) – LeoE Nov 25 '19 at 17:56
  • Sorry, I don't understand. Y will go to a set of X's; the inverse is not a function. Are you saying 'any X', 'the X nearest some value', or what? – Charles Merriam Nov 25 '19 at 17:56
  • @CharlesMerriam OP's picture goes from x=1 to x=9. Over that range, f(x) is strictly increasing so there is an inverse (over that range). – G5W Nov 25 '19 at 18:48

1 Answers1

1

Here is a basic R solution. Select x's across the range that you want to cover, generate f(x) then fit a function to y as a function of x. I compute and plot the inverse of one value as confirmation.

f = function(x) 2*x**6 + x**5 -3*x**4 + 7*x**3 + 9*x**2 + x + 6

## Generate inverse function
x = seq(0,9, 0.1)
y = f(x)
f_inv = approxfun(y, x)

## Plot as a confirmation
plot(f, xlim=c(0,9))
x100K = f_inv(100000)
points(x100K, 100000, pch=16, col="red")

Test of inverse function

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Thank you. how can we get the equation from f_inv and use it another language say in C. The r document has a warning "The value returned by approxfun contains references to the code in the current version of R: it is not intended to be saved and loaded into a different R session. This is safer for R >= 3.0.0." – Padmanabham Nooka Feb 06 '20 at 14:33