0
$\begingroup$

I'm trying to write code for a force test that will output the maximum force before structural failure occurs. I'm a bit of a novice to python, so the issue here might be something simple that I'm missing. Essentially, when I run the code, it will run indefinitely without outputting any value (so far, the longest I've let it run is about 10 minutes). I've been using a fake data frame with a vector length of about 80, so I think it must be an issue with the code rather than an issue of my vector being too long/requiring a long processing time.

Here's the code:

def find_new_max(data):

    n=5
    a=1

    groups=np.array_split(data['Force'], len(data['Force'])//n)

    slope=[group.max() - group.min() for group in groups[0:a]]

    while np.all(np.array(slope)) > 0:
        a=a+1

    return(max(data['Force'][0:a*n]))


find_new_max(df)

Essentially, the goal is to have the code analyze slope by looking at groups consisting of five data points each, starting with just the first group, and then moving on to the consecutive group if a negative slope is not identified. Once it finds a negative slope, the loop should stop, and it should use the new "a" value to search the vector up to the group with the negative slope for a maximum value.

I'm not quite sure what's wrong here, as no errors are popping up. I've also tried to have it search only one group at a time like this:

def find_new_max(data):

    n=5
    b=0
    a=1

    groups=np.array_split(data['Force'], len(data['Force'])//n)

    slope=[group.max() - group.min() for group in groups[b:a]]

    while np.all(np.array(slope)) > 0:
        b=b+1     
        a=a+1

    return(max(data['Force'][0:a*n]))


find_new_max(df)

But the same problem occurs.

Any help is appreciated, thank you!

$\endgroup$
3
  • $\begingroup$ Within the while loop you are updating the value of a, but since you are not recalculating the value of slope the value for the while statement does not change, causing it to always be true and therefore run indefinitely. $\endgroup$
    – Oxbowerce
    Commented Sep 18, 2023 at 19:09
  • $\begingroup$ @Oxbowerce I changed the code to include slope=[group.max() - group.min() for group in groups[:a]] under the while statement to ensure that the value of slope is also changing as a updates, but the problem persists. $\endgroup$ Commented Sep 18, 2023 at 19:27
  • $\begingroup$ Fixed it! I was using group.max() and group.min(), which would always yeild a positive number. Just switched to group[-1] and group[0] respectively and it works. $\endgroup$ Commented Sep 18, 2023 at 19:58

1 Answer 1

0
$\begingroup$

A couple of things:

  1. Your array is not being split into groups of 5. Your array is split into 5 groups of length = (len(data['force'])/n). See array_split documentation: https://numpy.org/doc/stable/reference/generated/numpy.array_split.html#numpy-array-split

  2. your slope variable is not being updated within the while loop, causing it to run indefinitely.

  3. The np.all method is returning a boolean, not an integer. How come you decided to use the .all() method? Set the while loop to continue until slope is negative, not a boolean.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.