title: Scipy fmin updating data inside a function
body: <p>I have a problem with my code. I don't know how to update the values of the variable <code>error</code> inside a function. The values only update outside the function.
  Can somebody help me with this problem?</p>
  
  <p>The idea of the code is to fit a model to my data <code>(x,y) = (mes1,pasajeros1)</code>. The model is a polynomial function plus a sine modulated by an exponential function.</p>
  
  <p><code>Error</code> is a vector, which is the difference between the data (<code>y</code> vector) and the fit (polynomial function plus sine modulated by a exponential of the previous iteration), with every iteration every component of the vector <code>error</code> has to be smaller.</p>
  
  <p>This is the code:</p>
  
  <pre><code>def base_pol(x,N):
       M=np.ones(x.size)
       for i in range(1,N+1):
       M=np.vstack([M ,x**i])
       return M.T
  
  def pol(x,theta):
      N=theta.size
      pol=base_pol(x,N-1)@theta
      return pol
  
  def bestpolsin(theta, theta2, m):
      fpol = pol(mes1,theta)
      fsinexp = 0
      best_aic = 10000000
      best_bic = 10000000
      best_parametros = []
  
      def Fsinexp(x):
          Fsinexp = x[0]*(np.sin(x[1]*mes1+x[2]))*np.exp(x[3]*mes1)
          return Fsinexp
  
      def Jpolsin(x):
          j = 0.5*(error@error) - error@Fsinexp(x) + 0.5*(Fsinexp(x)@Fsinexp(x))
      return j 
  
      for i in range(m):
          error = pasajeros1 - fpol - fsinexp
          parametros = fmin(Jpolsin, theta2, args=(error,))
          fsinexp = Fsinexp(parametros)
          aic = AIC(4*(i+1), Jpolsin(parametros))
          bic = BIC(4*(i+1), Jpolsin(parametros))
          best_parametros.append(parametros) 
          if best_aic&gt;aic:
             best_aic=aic
             r_1=i+1
          if best_bic&gt;bic:
             best_bic=bic
             r_2=i+1
       p=r_1==r_2
  
       return aic, bic, r_1, best_parametros, p
  </code></pre>