Home>
Written code

Writing code that calculates the inverse trigonometric function f (x) in increments of 0.01 from 0 to 0.51.

def f (x):
    x = np.arange (0, 0.51, 0.01)
    return np.arcsin (x)
f (x)


Result of running this code
array ([0., 0.01000017, 0.02000133, 0.0300045, 0.04001067,
0.05002086, 0.06003606, 0.07005729, 0.08008558, 0.09012195,
0.10016742, 0.11022305, 0.12028988, 0.13036898, 0.14046141,
0.15056827, 0.16069065, 0.17082967, 0.18098645, 0.19116215,
0.20135792, 0.21157496, 0.22181447, 0.23207768, 0.24236585,
0.25268026, 0.2630222, 0.27339303, 0.28379411, 0.29422684,
0.30469265, 0.31519303, 0.32572949, 0.33630358, 0.3469169,
0.3575711, 0.36826789, 0.37900902, 0.3897963, 0.40063159,
0.41151685, 0.42245406, 0.43344532, 0.44449278, 0.45559867,
0.46676534, 0.4779952, 0.48929078, 0.50065471, 0.51208975,
0.52359878])

I want to write code that executes the above code up to f (x) = 0.2 using the If statement and While statement without changing the range of x.

Written code
def f (x):
    x = np.arange (0, 0.51, 0.01)
    return np.arcsin (x)
LIMIT = 0.2
while f (x)<LIMIT:
    print (f (x))


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

  • Answer # 1

    That will not take much computation time,
    What is necessary is to extract the elements below the specified value after calculating all the elements.

    Assuming that the value increases monotonically within the domain.

    import numpy as np
    arr = np.arcsin (
        np.arange (0.00, 0.51, 0.01)
    )
    arr = arr [arr<0.2]
    print (arr)

    Execution results

    [0. 0.01000017 0.02000133 0.0300045 0.04001067 0.05002086
      0.06003606 0.07005729 0.08008558 0.09012195 0.10016742 0.11022305
      0.12028988 0.13036898 0.14046141 0.15056827 0.16069065 0.17082967
      0.18098645 0.19116215]

    The function f is excluded because its existence was not felt.
    Since there are many unclear descriptions, it would be nice if you could solidify the basics a little more.