1

I have a set of distances x=c*r/rs

 array([ 0.09317335,  0.1863467 ,  0.27952006,  0.37269341,  0.46586676,
        0.55904011,  0.65221346,  0.74538682,  0.83856017,  0.93173352,
        1.02490687,  1.11808022,  1.21125357,  1.30442693,  1.39760028,
        1.49077363,  1.58394698,  1.67712033,  1.77029369,  1.86346704])

and number density (sigma) array([ 9.56085037e+14, 5.13431506e+14, 3.26960286e+14, 2.27865084e+14, 1.68325130e+14, 1.29590176e+14, 1.02918831e+14, 8.37487042e+13, 6.94971037e+13, 5.86086377e+13, 5.00994710e+13, 4.33218850e+13, 3.78349864e+13, 3.33300619e+13, 2.95856349e+13, 2.64394232e+13, 2.37702922e+13, 2.14863249e+13, 1.95167455e+13, 1.78063354e+13])

which I have plotted to get the following graph. It is a log log plot.

I have a function enter image description here

which should fit my graph according to theory. I don't how to use scipy.opt.leastsquare to use the function and fit my graph. The parameters to fit are c and rs

Cilyan
  • 7,883
  • 1
  • 29
  • 37
Srivatsan
  • 9,225
  • 13
  • 58
  • 83
  • Awesome write-up with graphs and formulas. – hughdbrown Feb 07 '14 at 17:15
  • Your model function has no parameters except for a constant factor, what do you want to fit, just the factor?? –  Feb 07 '14 at 17:17
  • @Nabla Actually, x=c*r/rs, wherein c and rs are the parameters i need to fit – Srivatsan Feb 07 '14 at 17:20
  • 1
    This might be a good starting point for you [Non-linear optimization with scipy](http://stackoverflow.com/questions/12942153/how-to-perform-non-linear-optimization-with-scipy-numpy-or-sympy) – jbh Feb 07 '14 at 17:20
  • @srivatsan Is your data `x` and `Sigma(x)` or `r` and `Sigma(r)`? Even if the second is the case, you are left with only two degrees of freedom and those are equivalent to axis rescaling. Anyway, if you have a recent version of scipy you can use [scipy.optimize.curve_fit](http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html#scipy.optimize.curve_fit) instead, which will take care of sum stuff like error calculation. The example should explain the use. –  Feb 07 '14 at 17:25
  • Is your `y` axis label correct? What does `MPC` stand for? – CT Zhu Feb 07 '14 at 17:27
  • @Nabla sorry for the axis of the graph.. It is actually x and Sigma(x). I am only confused as to how to include that Sigma function as it has three conditions. – Srivatsan Feb 07 '14 at 17:29
  • 1
    @srivatsan But if you only have `x` and `Sigma(x)` then you cannot fit the parameter `c` because `Sigma(x)` does not depend on it. You only can fit `r_s` if `delta_c` and `rho_c` are constants. Also you write `Sigma(x)` as a normal python function, you can use `if` for the different conditions. –  Feb 07 '14 at 17:33
  • @CTZhu `Mpc` probably stands for Megaparsec, a distance measure in astronomy. –  Feb 07 '14 at 17:42
  • @CTZhu yes as Nabla points out, it does stand for Megaparsec – Srivatsan Feb 07 '14 at 18:02

1 Answers1

1

Option 1: Using scipy.optimize.curve_fit

Option 2: write your own function to output R2 or sse, then minimize this function using scipy.optimize. I've always used this method for complicated problems and would recommend the algorithms of SLSQP and L-BFGS-B.

Yuxiang Wang
  • 8,095
  • 13
  • 64
  • 95