0

let's say that I have this function

def funtion(x, bb, aa):
    
    if x>aa:
        res = aa
    else:
        xxr = x/aa
        
        res = bb*(1.5*xxr-0.5*xxr**3)
    
    return res

If I do:

xx = np.linspace(0,49,50)
yy = funct(xx,74,33)

I get the following error:

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

The usage of a.any() or a.all() does not solve the problem. The results is different from what I am looking for.

The usage of a.any() or a.all() leads to

array([ 0.        ,  3.36260678,  6.71903609, 10.06311044, 13.38865236,
       16.68948438, 19.959429  , 23.19230876, 26.38194618, 29.52216379,
       32.60678409, 35.62962963, 38.58452292, 41.46528647, 44.26574283,
       46.9797145 , 49.60102401, 52.12349389, 54.54094666, 56.84720483,
       59.03609094, 61.1014275 , 63.03703704, 64.83674208, 66.49436514,
       68.00372875, 69.35865542, 70.55296769, 71.58048808, 72.4350391 ,
       73.11044328, 73.60052314, 73.8991012 , 74.        , 73.89704205,
       73.58404987, 73.05484598, 72.30325291, 71.32309319, 70.10818933,
       68.65236386, 66.9494393 , 64.99323817, 62.77758299, 60.2962963 ,
       57.5432006 , 54.51211843, 51.1968723 , 47.59128475, 43.68917828])

but this is what I expect

array([ 0.        ,  3.36260678,  6.71903609, 10.06311044, 13.38865236,
       16.68948438, 19.959429  , 23.19230876, 26.38194618, 29.52216379,
       32.60678409, 35.62962963, 38.58452292, 41.46528647, 44.26574283,
       46.9797145 , 49.60102401, 52.12349389, 54.54094666, 56.84720483,
       59.03609094, 61.1014275 , 63.03703704, 64.83674208, 66.49436514,
       68.00372875, 69.35865542, 70.55296769, 71.58048808, 72.4350391 ,
       73.11044328, 73.60052314, 73.8991012 , 74.        , 33.        ,
       33.        , 33.        , 33.        , 33.        , 33.        ,
       33.        , 33.        , 33.        , 33.        , 33.        ,
       33.        , 33.        , 33.        , 33.        , 33.        ])

As you can notice, there is a constant value for x>aa, according to the function definition. Maybe I have to re-rewrite the if statement in another way.

Could someone give a glue? It is not possible for me to do a for cycle due to the fact that I need to use the function in another function. Thanks for any kind of help.

diedro
  • 511
  • 1
  • 3
  • 15
  • 1
    Please read [ask] and [mre], and don't lead us down a winding road to the question. If you encountered an error but then solved it, there's no reason to tell us about that process. People don't ask "what have you tried?" in order to get you to "deserve" an answer; they do it in order to *determine your understanding* of the *current* problem, and to help you *refine* the explanation. – Karl Knechtel Feb 03 '23 at 09:27
  • That said: I suspect this question has more to do with math, and your expectations, than Python. It seems that you intend to fit a cubic polynomial to part of the curve, and end up with a horizontal asymptote - I can't fathom your reasoning for that. As x goes to infinity, a constant times the cube of x will go to infinity for any nonzero constant, and it will grow faster than x. – Karl Knechtel Feb 03 '23 at 09:30
  • In other words: "I would like to fit this function with a theoretical." - I can't understand why the data should be expected to look *anything like* the theoretical curve shape, because curves described by that (parameterized) function look *inherently* different from the plot. – Karl Knechtel Feb 03 '23 at 09:32
  • 1
    But as far as the error goes: Make sure you understand **what gets passed to** `curve_fit`, and how Numpy broadcasting operations work generally. For example, if you are receiving an array of values, and you have a piecewise rule for processing *individual* values, then yes, normal `if` logic is not going to work because **each value in the array could be in a different "piece"**. – Karl Knechtel Feb 03 '23 at 09:35
  • for your data look as a log function not a cubic function. – Lucas M. Uriarte Feb 03 '23 at 10:00
  • @Karl Knechte: thanks for your suggestions. The way to set-up the question and the one about the if logic help me to find the solution. I use np.where. According to you, what should I do now? delete my question, answer my question or nothing? – diedro Feb 03 '23 at 10:50
  • This version of the question looks much better, and would be answerable. We have a canonical https://stackoverflow.com/questions/10062954/ about resolving the error that you encountered, but it's purely from a bugfix perspective. In your case, rather than talking about encountering the error, it would be better to show *the attempt that uses `.any` or `.all` (corresponding to your example wrong output), and then directly ask how to make it do the right thing. However, I am pretty sure we have a duplicate question for that, too. – Karl Knechtel Feb 03 '23 at 10:53
  • In the end, I think the question is a duplicate of [How to apply piecewise linear fit in Python?](https://stackoverflow.com/questions/29382903); normally I would be able to close it myself but for technical reasons I will need to get someone else. As it happens, Numpy has a more sophisticated tool `np.piecewise` for these situations; it is not necessary to implement the conditional logic with `.where` yourself. – Karl Knechtel Feb 03 '23 at 10:55
  • Thanks. I need more or less what I was doing before. I have just to show that some "function" works better than others. However, as you pointed it out, the fitting question is another story. Here the good point is, in my humble opinion, that a.any() & a.all() lead to different result respect to np.where. – diedro Feb 03 '23 at 11:01

0 Answers0