How to find arcsin for a coulmn containg values?

Hello everzone,

I have a column of values and i need to find arcsin for evry value in this column. I am very new to shell scripting and would be glad to receive any help regarding this. Also i want them to be saved in the next column.
Desired output:

#  #
a  arcsin(a)
b  arcsin(b)
c  arcsin(c)
d  arcsin(d)

Thanks,
Dinesh

Hello Dinesh,

Could you please show the sample input too for your requirement. Also use code tags as per forum rules with your codes/same input.

NOTE: You can go through either forum rules or you can see a button in BAR while posting or replying to anyone named code.

Thanks,
R. Singh

1 Like

Hello R. Singh,

Thanks for the info. I keep forgetting code tags. You can find a sample of my input file. Actually my original file will contain more then 50k lines in a column.

       
0.4       
2       
2.433105012       
2.433105012       
2.8       
3.12409987       
3.12409987       
3.687817783       
3.687817783       
4.4   

I am able to calculate the arcsin using python script. But large part of my code is in shell and it would great if i am able to do the same using shell.

Thanks,
Dinesh.n

Given that the domain of values for the arcsine function is -1 <= x <= 1 and most of your values are not in that range, I'm not sure why we should bother.

And, you haven't told us what operating system or shell you're using.

If you have a 1993 or later version of ksh on your system, the following is a simple way to do what you requested:

#!/bin/ksh
while read x
do	printf '%15.10f %15.10f\n' "$x" "$((asin(x)))"
done < file

which, with your sample input in file with blank lines removed, produces the output:

   0.4000000000    0.4115168461
   2.0000000000            -nan
   2.4331050120            -nan
   2.4331050120            -nan
   2.8000000000            -nan
   3.1240998700            -nan
   3.1240998700            -nan
   3.6878177830            -nan
   3.6878177830            -nan
   4.4000000000            -nan
1 Like

Hello R. Singh,

I want the value of inverse sin. Also i cant reply your mess since i have less then ten post made. Anyway i really apppreciate your effort.

Thanks,
Dinesh.n

Hello Dinesh,

No problem, I requested you to post in actual post only not in message. Following may help you in same.

 awk '{print atan2($0, sqrt(1-$0 * $0))}' Input_file 2>/dev/null
 

Thanks,
R. Singh

1 Like

Hello Don Cragun,

Thats true you are right. I am sorry for wasting most of your time. My real value should be looking:

  -37.5    40.96950085
  -37.5    40.57708713
  -37.5    40.20572099
  -37.5    39.85599077
  -37.5    39.52847075
  -37.5     39.22371731
  -37.5     38.94226496
  -37.5    38.68462227
  -37.5    38.45126786
  

I want the first column to be divided by the second column and then i want to find arcsin for this values. For eg,

arcsin($1/$2)

. I am using red hat linux and my script are in "ksh".

Thanks,
Dinesh.n

Hello Dinesh,

Following may help you in same.

cat values2
37.5    40.96950085
37.5    40.57708713
37.5    40.20572099
37.5    39.85599077
37.5    39.52847075
37.5     39.22371731
37.5     38.94226496
37.5    38.68462227
37.5    38.45126786
awk 'BEGIN{OFS="\t";print "VALUES:" OFS OFS OFS "reverse sine"} {A=$1/$2;print $0 OFS atan2(A, sqrt(1-A * A))}' OFS="\t" values2 2>/dev/null

Output will be as follows.

VALUES:                 reverse sine
37.5    40.96950085     1.15629
37.5    40.57708713     1.17885
37.5    40.20572099     1.20184
37.5    39.85599077     1.22524
37.5    39.52847075     1.24905
37.5     39.22371731    1.27323
37.5     38.94226496    1.29779
37.5    38.68462227     1.32268
37.5    38.45126786     1.3479
 

Where values2 is the Input_file I have used, hope this helps.

Thanks,
R. Singh

1 Like

Hello R. Singh,

Thanks for the code. It worked for me.

Thanks,
Dinesh.n

The domain of the inverse sinus (arcsine) function ranges from -0.5 \pi to + 0.5 \pi, (+90 deg - -90 deg; with corresponding sinus values from -1 to +1, as Don Cragun has already pointed out). Values above or below ~1.6 (roughly 0.5 \pi) make still no sense therefore.

I hope this helps.

/PS: You posted this in the meantime:

So this point is moot.

bakunin

And, if you want to try it in ksh directly without awk :

#!/bin/ksh
printf '%15s %15s %15s %15s\n' "x" "y" "x/y" "arcsine(x/y)"
while read x y
do	printf '%15.10f %15.10f %15.10f %15.10f\n' "$x" "$y" "$((x/y))" "$((asin(x/y)))"
done < file

producing, with your latest data:

              x               y             x/y    arcsine(x/y)
 -37.5000000000   40.9695008500   -0.9153150325   -1.1562894523
 -37.5000000000   40.5770871300   -0.9241668797   -1.1788488014
 -37.5000000000   40.2057209900   -0.9327030849   -1.2018367803
 -37.5000000000   39.8559907700   -0.9408874118   -1.2252407460
 -37.5000000000   39.5284707500   -0.9486832981   -1.2490457726
 -37.5000000000   39.2237173100   -0.9560542083   -1.2732346025
 -37.5000000000   38.9422649600   -0.9629640196   -1.2977876235
 -37.5000000000   38.6846222700   -0.9693774373   -1.3226828775
 -37.5000000000   38.4512678600   -0.9752604293   -1.3478960944
1 Like