0

I'm trying to use Python to fit a curve to a set of points. Essentially the points look like this.

enter image description here

The blue curve indicates the data entered (in this case 4 points) with the green being a curve fit using np.polyfit and polyfit1d. What I essentially want is a curve fit that looks very similar to the blue line but with a smoother change in gradient at points 1 and 2 (meaning I don't require the line to pass through these points).

What would be the best way to do this? The line looks like an arctangent, is there any way to specify an arctangent fit?

I realise this is a bit of a rubbish question but I want to get away without specifying more points. Any help would be greatly appreciated.

1 Answer 1

1

It seems that you might be after interpolation between points rather than fitting a polynomial References: Spline Interpolation with Python and Fitting polynomials to data

However, in either case here is a code snippet that should get you started:

import numpy as np
import scipy as sp
from scipy.interpolate import interp1d

x = np.array([0,5,10,15,20,30,40,50])
y = np.array([0,0,0,12,40,40,40,40])

coeffs = np.polyfit(x, y, deg=4)#you can change degree as you see fit
poly = np.poly1d(coeffs)
yp = np.polyval(poly, x)

interpLength = 10
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)


plt.plot(x, y, '.', x, yp, '-', new_x,new_y, '--')
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.