Python

# Asin() Python

We have standard built-in math module functions in the Python programming language for higher-level mathematical calculations. We also use the math module for inverse trigonometric functions that perform operations opposite to trigonometric functions. This article focused on one of the inverse trigonometric functions, i.e., asin(). Asin() function is also referred to as the inverse of sine or arcsine of the number lies between the range of -1 and +1. To access this function, we have to import the math module then call this function by using math static objects. Let’s use this Asin() function to perform arcsine operations.

## Syntax of Asin() in Python

The syntax of asin() is math.asin(x) that is supported by Python through the math module. Here, ‘x’ is a number value that lies in the range of -1 to +1.

## Parameters Passed in Asin()

Asin() function accepts one parameter that is -1<=x<=1. If the value exceeds or precedes it, it throws a value error.

## Return Value in Asin()

It returns the floating data type value that is the arcsine value of the given number. The arcsine value should be in radian lies in the interval –π/2 to π/2 or -1 to +1. This depicts that we have to alter the degree into radian first. The radian function allows the conversion of angles from degree to radians.

## Example 1: Program of asin() Function on Integers

As discussed, Asin() function only accepts the value that is in the range from -1 to +1. So, by taking the two positive integers and two negative integers having values 0.25, -0.5, 0, 1, and -1 in the interval [-1,+1], we got the results in radian from the following example program. We called math.asin() function through a print statement. We have been using the spyder terminal for the implementation.

import math
"parameter pass in asin() -1<=x<=1"
print(math.asin(0.25))
print(math.asin(-0.5))
print(math.asin(0))
print(math.asin(1))
print(math.asin(-1)) By running the code, we got output results that are shown in radian. ## Example 2: Program of Asin() on Tuple and List items

Asin() function works perfectly on Tuple and list items. In the code below, we are declaring the tuple items. We are taking multiple items in a tuple of positive and negative integers having values of 0.21, 0.35, 0.55, -0.78, and -0.89. After that, we have utilized a print statement to get the desired output.

import math
Tuple = (0.21, 0.35, 0.55, -0.78, -0.89)
print('Result of Tuple Item  = %.2f' %math.asin(Tuple)) We got the result of the corresponding tuple values. Now, we are declaring the list item of multiple value of positive and negative integer. The list is represented by “Lis” keyword having values -0.25, 0.72, -0.56, 0.44 and 0.95. The print statement will help to display the result.

import math
Lis = [-0.25, 0.72, -0.56, 0.44 , 0.95]
print('Result of List Item = %.2f' %math.asin(Lis)) After running the program, we have the output results of the list items below. ## Example 3: Program of Asin() “ValueError():math domain error”

As Asin() function only accepts the number that lies in the interval [-1,+1]. If the number does not lie between the range, the valueError:math domain error occurs. Initially, the math module has been imported into the code, then we have initialized a variable titled “var1” with a value of “2.1”. We have declared a print statement that will output the result on the console screen. Inside the print statement, we have called the math.asin() function.

import math
# number
var1 = 2.1
print("Result of asin(",var1,") is = ", math.asin(var1)) By taking var1=2.1 and executing a program, math domain error occurred in this way. This shows that Asin() function support on numbers lies in the range of -1 to +1. If the value is not in the range as stated, then an error occurs. ## Example 4: Program of Asin() TypeError()

We have got the results from the numeric values in the above example code. But now in this program, we take string values to see the output. We initialize two variables one with “var1” with a string value “2” and the other as “var2” with a string value “x”. And printed the result by using the print statement. In a print statement, we called math.asin() and passed an argument “var1”.

import math
"number value"
var1 = "2"
print("Result of asin(",var1,") is = ", math.asin(var1)
"character value"
var2 = "x"
print("Result of asin(",var2,") is = ", math.asin(var2)) After interpretation and implementation of the above program, we have a TypeError message that means Asin() is not accepting the string values. A floating value is required. As “var1” and “var2” are strings not a number so it throws an exception. ## Example 5: Program of Asin() Function Plot

We can plot a graph of an Asin() function by importing matplotlib for interactive visualization. Using this, we generate the graph of an Asin(). In this code, “p” and “q” are the two points of x and y coordinates. “p” has given a positive value of 0.5 and “q” has given a negative value of -0.9. We initialize i = -1 and use the while condition that is “i” should be less than or equal to the 1. Use append() method that appends an element to the end of the list by passing the parameter value “i” the value we assign i=i+0.25 to increment a variable by 0.23. We will get the image of the graph by using “plt.plot” and passing the values p and q and by calling plt. Show() will help the visual graph to be shown on the output screen.

import math
"matplotlib is a python library for plotting graph"
import matplotlib.pyplot as plt
p=[0.5]
q=[-0.9]
i=-1
while (i<=1):
p.append(i)
q.append(math.asin(i))
i=i+0.25
plt.plot(p,q)
plt.show() After execution of a program, the graphical visualization of asin() function is shown in the picture below . ## Example 6: Program of Asin() Function for Array and Plot

As we have discussed the simple running programs, let’s move to the complex program by taking multiple numeric values for generating the graph in this code “in_Arr” referred to as an input array that has values in square brackets -0.7,-0.5, -0.93, 0.28, 0.39 and 0.15 and “out_Arr” as output array with the null value in square brackets .”Q” represents the x-axis and “R” represents the y-axis and “in” keyword used in the code is used to iterate through a sequence of “for loop”. With the print statement, we can see the output of in_Arr and out_Arr. Passing the two parameters that contain x and y points in plt.plot() function that draws lines from point to point. In the function plt.title(), we have set the title for the plot as “math.asin()”.plt.xlabel has value as “Q” for x-axis as and plt.ylabel has values as “R” is for the y-axis. To show the plot we utilizes plt.show() function.

import math
import matplotlib.pyplot as plt

in_Arr = [-0.78, -0.57, -0.93,
0.28, 0.39,  0.15]
out_Arr = []
for i in range(len(in_Arr)):
out_Arr.append(math.asin(in_Arr[i]))
i =i+1
print("Input_Array : \n", in_Arr)
print("\nOutput_Array : \n", out_Arr)
plt.plot(in_Arr, out_Arr,)
plt.title("math.asin()")
plt.xlabel("Q")
plt.ylabel("R")
plt.show() You will see the output of the code on this console screen. Now, the graph plot of this code is like this. ## Conclusion

As we have a complete walkthrough of an Asin() function in Python by importing the math module and implementing simple examples. We have added unique examples for the novice as well as expert users along with their detailed implementation. Go through all examples to get a better understanding of this topic. 