0

I have been trying to curve fit double broken power law.

def sbpl(x,f0,a1,a2,a3,xb1,xb2):
    if x[0] < xb1:
         F=f0* x**(-a1)
    elif xb1 <= x <= xb2:
        F=f0* ((xb1)**(a2-a1)) *(x)**(-a2)
    else:   
        F=f0*((xb1)**(a2-a1))*((xb2)**(a3-a2))*(x**(-a3))   
    return F

x= array([5.5,9.0,17.0,18.0,19.0,43.0,45.0])
y = array([1.14e-03,2.08e-03,3.18e-03,2.53e-03,2.81e-03,1.491e03,1.311e-03])

popt,pcov=optimize.curve_fit(sbpl,x,y,sigma=err,p0=[1.,1.,0.,0.,0.,0.])

but it shows an error.

line 20, in sbpl     
elif xb1 <= x <= xb2:   

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

  • 1
    Please add your code and error as a text in this question. Avoid uploading images of text stuff. – Rishabh Kumar Mar 01 '21 at 07:38
  • You write `xb1 <= x <= xb2` but `x` seems to be an array, and Python doesn't let you compare an array with a single value as part of an if-test (such type of expression can be used in `np.where` clauses though). – JohanC Mar 01 '21 at 10:52

1 Answers1

0

The number of points is too low compared to the number of parameters to be optimised. This doesn't allow to chose for sure a piecewise function. A linear piecewise function might be as convenient as a piecewise power function or another piecewise law.

For example, a regression method for linear piecewise function is shown page 12 in this paper : https://fr.scribd.com/document/380941024/Regression-par-morceaux-Piecewise-Regression-pdf

Following this method, the numerical calculus is detailed below with symbols defined in the referenced paper. H(x) is the step Heaviside's function :

enter image description here

By comparison, just as good fitting can be obtained with much simpler model :

enter image description here

For a more convenient answer to your specific problem you should edit a more representative data.

JJacquelin
  • 1,529
  • 1
  • 9
  • 11