Home > database >  After a value in an array, start to calculate in another way
After a value in an array, start to calculate in another way

Time:12-21

I need to calculate the variation of an angle and store in an array and I have the problem below: The angle increases its value through calculations and when the value of the angle is better than 90°, python starts to pic the suplementary value, not the continuation, something like this:

Undesirable way (and that's ocurring): 23.45°, 26.7°,..., 84.5°, 88.33°, 85°, 82.3° ...

Desirable way: 23.45°, 26.7°,..., 84.5°, 88.33°, 91.2°, 94.7° ...

My code is:

import math as mt
import numpy as np

l = 85
s = 170
q = 30.07
p = 120

theta = (3.81*np.pi)/180
alfa = np.linspace((104.07*np.pi)/180, (26.19*np.pi)/180, 40)

d = np.sqrt(s**2   q**2 - 2*s*q*np.cos(alfa))
gama = np.arccos((l**2   p**2 - s**2 - q**2   2*s*q*np.cos(alfa))/(2*l*p))
betalinha = np.arcsin((s / d) * np.sin(alfa))*180/np.pi

print(np.around(betalinha, 2))

Output: [66.59 68.46 70.34 72.22 74.12 76.03 77.95 79.88 81.82 83.77 85.74 87.72 89.7 88.29 86.28 84.25 82.21 80.16 78.09 76.01 73.92 71.81 69.69 67.55 65.4 63.23 61.05 58.86 56.65 54.43 52.2 49.95 47.68 45.41 43.12 40.82 38.5 36.18 33.84 31.49]

CodePudding user response:

diff = np.insert(np.diff(betalinha), 0, 1)

while j < len(diff):
    if diff[j] < 0:
        betalinha[j] -= np.pi
        betalinha[j] *= -1
    j =1

Output: array([1.16218675, 1.19480394, 1.22758774, 1.26054302, 1.29367464, 1.32698746, 1.36048627, 1.39417585, 1.42806092, 1.46214611, 1.49643599, 1.53093502, 1.56564752, 1.60057772, 1.63572963, 1.67110713, 1.70671387, 1.74255327, 1.7786285 , 1.81494244, 1.85149766, 1.88829639, 1.92534047, 1.96263135, 2.00017004, 2.03795707, 2.07599248, 2.11427574, 2.15280579, 2.19158094, 2.23059888, 2.26985664, 2.30935057, 2.34907629, 2.38902871, 2.42920199, 2.46958953, 2.51018397, 2.55097717, 2.59196023])

CodePudding user response:

I'm not sure what the root problem is, but I believe you can correct it by

  1. Computing the diff of the values (values less than the previous ones yield a negative value)
  2. Multiply the diff by 2 and cumsum
  3. Add that to betalinha
# Compute the diff (adding a dummy value to the beginning since diff returns an array of N - 1 length)
diff = np.insert(np.diff(betalinha), 0, 1)

# Reset positive values to 0
diff[diff > 0] = 0

# Fix decreasing values
betalinha -= diff.cumsum() * 2

Output:

>>> betalinha
array([ 66.59,  68.46,  70.34,  72.22,  74.12,  76.03,        77.95,  79.88,
        81.82,  83.77,  85.74,  87.72,  89.7 ,  91.11, <---   93.12,  95.15,
        97.19,  99.24, 101.31, 103.39, 105.48, 107.59,       109.71, 111.85,
       114.  , 116.17, 118.35, 120.54, 122.75, 124.97,       127.2 , 129.45,
       131.72, 133.99, 136.28, 138.58, 140.9 , 143.22,       145.56, 147.91])
  • Related