For a task for school, I have to write my own fit function by using the least squares method. The problem is I don't know how to do that, specifically I don't know how to minimize my function to calculate my fit parameters.The problem here is also that my fit function is not linear, so my book says I have to try guess some values for my fit parameters and then minimize my function. But still I don't know how do to that. The code that you can find below is my code right now, I got it from somebody but I don't understand what it does so :).

Thanks in advance!

def fit(x,mu,gamma,back,A):
   return A*(gamma/((x-mu)**2+gamma**2))+back 
def Ls_rechte(y): 
   Ls = 0
   for i in range(len(Positie)):
       Ls = Ls + (Intensiteit[i]- fit(Positie[i],y[0],y[1],y[2],y[3]))**2/(FoutI[i]**2)
       return Ls
nu = len(Positie)-4
mini = minimize(Ls_rechte,(150,0,100,1))
  • $\begingroup$ For simple gradient descent, you can vary each parameter slightly and determine if an increase or decrease in each parameter will reduce the sum-of-squared error. Then you can change each parameter value in that direction, and repeat the process until no further reduction in error can be found. This method usually works only for simple cases. $\endgroup$ – James Phillips Nov 29 '19 at 15:54