0

I have a problem, I have 2 lists with x and y values, and I would like to create a function based on these. But the problem is that I would like to build a function like this one:

f(x) = a * (x-b)**c

I already know scipy.interpolate but I couldn't find anything to return a function like this one.

is there a quite easy way to try to create the best function I can by searching which values of a,b and c match the most?

thanks for your help!

Edit:

here is what my current values of x and y look like:

enter image description here

I created this function :

def problem(values):
    s = sum((y - values[0]*(x-values[1])**values[2])**2 for x,y in zip(X,Y))
    return(s)

and I tried to find the best values of a,b and c with scipy.optimize.minimize but I don't know with which values of a,b and c I should start...

values = minimize(problem,(a,b,c))
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

2

(Edited to account for the OP's added code and sub-question.)

The general idea is to use a least-squares minimization to find the "best" values of a, b, and c. First define a function whose parameters are a, b, c that returns the sum of the squares of the differences between the given y values and the calculated values of a * (x-b)**c. (That function can be done as a one-liner.) Then use an optimization routine, such as one found in scipy, to minimize the value of that function value. Those values of a, b, c are what you want--use them to define your desired function.

There are a few details to examine, such as restrictions on the allowed values of a, b, c, but those depend somewhat on your lists of x and y values.

Now that you have shown a graph of your x and y values, I see that your values are all positive and the function is generally increasing. For that common situation I would use the initial values

a = 1.0
b = 0.0
c = 1.0

That gives a straight line through the origin, in fact the line y = x, which is often a decent first guess. In your case the x and y values have a very different scale, with y about a hundred times larger than x, so you would probably get better results with changing the value of a:

a = 100.0
b = 0.0
c = 1.0

I can see even better values and some restrictions on the end values but I would prefer to keep this answer more general and useful for other similar problems.

Your function problem() looks correct to me, though I would have written it a little differently for better clarity. Be sure to test it.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
0

def problem (a , b, c, d):

return a * (x[d]-b)**c

I guess is what you are after. With D being what value of the X array. Not sure where Y comes into it.

Scott Hardy
  • 101
  • 2