epselonzero
epselonzero

Reputation: 71

Finding value of a parameter using set of data

I have a set of value for x and y and I'm looking to find a way to find the value of a parameter for a function.

I have a function y = Ax^{4/3}. I was thinking about using curvefit, but I'm not sure if this is the right way.

I tried to linearize the function and find the slope with polyfit, but the slope change radically if I remove some points.

Edit: I tried curvefit and something strange is happening. curvefit gives me A=0.55, but this value doesn't fit at all. However, 0.055 seems to work. Here's my code.

def func(A,x):
  return A*x**(4/3)
popt,pcov = curve_fit(func,x[:18], y[:18])

Any help will be appraciated.

Upvotes: 1

Views: 247

Answers (1)

blunova
blunova

Reputation: 2532

Here is an example on how to fit data to your model:

Import relevant libraries:

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

Define x values:

x = np.linspace(0, 10)

Define a function representing your model:

def f(x: np.ndarray, a: float) -> float:
    return a * x ** (4/3)

Let's sample data from the above model and add noise:

y = f(x, a=16) * np.random.uniform(1, 2, len(x))

Perform the curve fitting:

popt, pcov = curve_fit(f, x, y)

Plot the results:

plt.scatter(x, y)
plt.plot(x, f(x, *popt), c="r")
plt.show()

enter image description here

Upvotes: 1

Related Questions