0

I am trying to fit the data using a physical model. The code snippet is included here:

from scipy.optimize import fmin as simplex

def chi2(p1,x,y_r):
    chisq = 0.0
    for i in range (len(x)):
        real_f=(np.abs(gamma(tret_m,alpha_m)[i])*(p1[0]*sin(p1[1]*x[i]+p1[2]))+ \
                p1[3]*np.abs((gamma(tret_m,alpha_m)[i]))**2
        if (300>p1[3]>250 and 65>p1[0]>60):#constraining parameters
            chisq += (real_f-y_r[i])**2
        else:
            chisq=1.0e15
    return (chisq)

popt = simplex(chi2, guess1, args=(x,obs),ftol=1.0e-6,maxiter=1000000, maxfun=1000000) 

The parameters produced at output are same as that provided as initial guesses (passed as guess1)! The output shows this (apart from list of parameters):

  Optimization terminated successfully.
  Current function value: 1000000000000000.000000
  Iterations: 19
  Function evaluations: 170

Any clue through which I can make this function run till the function value converges?

EDIT:
1. gamma(tret,alpha) is a function I have defined elsewhere. I am passing tret_m and alpha_m as parameters to gamma (these parameters have been initialized with numerical values). gamma function returns an array of size 1X600.
2. x is a 1X600 array extracted from a file.

user3440489
  • 259
  • 1
  • 4
  • 13
  • Could you fix your indentation? Currently, I can't be sure that your `if` statement is within the `for`-loop, since you didn't properly below your function declaration. Also, I noticed tabs; this is Python, better to use (four) spaces for indentation. –  Oct 13 '14 at 09:19
  • Have you tried using the `full_output=True` option? –  Oct 13 '14 at 09:25
  • Your indentation is still incorrect; the `if` is now beyond the previous statement `real_f = ...`. Also, there's `yr` and `y_r`. And what are `tret_m` and `alpha_m`? I guess constants of shape `x.shape`. Sorry, but your example is to incomplete to be tested by someone else. –  Oct 13 '14 at 11:10
  • Thanks for looking into it. Actually gamma function uses many datasets from different files to return 1X600 array using alpha_m and tret_m values(which are some measured constants). It is indeed difficult to test since it requires many data files. I intend to know if there is some problem in using the function itself. – user3440489 Oct 13 '14 at 12:04

0 Answers0