Q: What is the best way to smooth a curve? I am working on a fitting algorithm that requires a boundary condition of f(x=0) = 0. However, let's say my fitting result is a numpy array y, if I naively set y[0]=0, the curve would look pretty awkward because of a sudden bump from y[0] to y[1]. I made up something similar to what my final curve looks like:
import numpy as np
from matplotlib import pyplot as plt

fig, ax = plt.subplots(1,1, figsize=(10,6))

x = np.arange(0,3.5,0.02)
y = 1/(1+(x-1.2)**2) - 0.3
y[0] = 0

ax.plot(x,y)
ax.axhline(color = 'r')
ax.axvline(color = 'r')

plt.show()

Below is the plot of the above python code. I am wondering if there is a smarter way to smooth the curve (just like the red line I drew)? I tried scipy.signal.savgol_filter but it is not very effective.
In my application, the value near zero is not very important. As long as the whole plot looks smooth and continuous, it would good.

Edit:
I realized that I might have described the question in a poor way. What I mean is not fitting the relationship I presented in the above code y = 1/(1+(x-1.2)**2) - 0.3. I mean there is some functional F(y(x)), and I am trying to find y(x) such that F(y(x)) is a minimum (something like a least-square fit). There is an imposed condition for y(x), requiring y(0) = 0. However, if I simply enforce y[0] = 0, the resultant curve y(x) would like awkward near 0. I wonder if there is a way to smooth it.


A: The best way for setting a boundary condition is to force it in your fitting curve definition.
In your case you have your fitting curve written as:
y(x) = 1/(1+(x-a)**2) - b
and I guess you are fitting a and b. The problem is that as long as you leave both a and b to be free you can't impose the boundary condition.
To impose the boundary condition you have to impose:
0 = y(0) = 1/(1+a**2) - b
Which gives you:
b = 1/(1+a**2)
And thus:
y(x) = 1/(1+(x-a)2) - 1/(1+a2)
Which of course vanishes for x = 0
Now you just have to fit the parameter "a" to your data.
