Trignometry calculator

Calculate values of Sine(sin), Cosine(cos), Tangent(tan), Cotangent(cot), Secant(sec), Cosecant(csc), Arc Sine(asin), Arc Cosine(acos), Arc Tangent(atan), Arc Cotangent(acot), Arc Secant(asec), Arc Cosecant(acsc).

To put everything in separate script will need creation of too many files .Hence best thing to do is make them functions and place it in your '.bashrc' file in your home folder.Copy the entire codes below  (either radian or degree according to your need) and paste it in your .bashrc file.The codes below uses command line tool bc. Hence make sure that you have installed bc.


RADIANS

-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------


#trignometry
sin ()
{
    echo "scale=5;s($1)" | bc -l
}

cos ()
{
    echo "scale=5;c($1)" | bc -l
}

tan ()
{
    echo "scale=5;s($1)/c($1)" | bc -l
}

csc ()
{
    echo "scale=5;1/s($1)" | bc -l
}

sec ()
{
    echo "scale=5;1/c($1)" | bc -l
}

ctn ()
{
    echo "scale=5;c($1)/s($1)" | bc -l
}

asin ()
{
    if (( $(echo "$1 == 1" | bc -l) ));then
       echo "90"   
    elif (( $(echo "$1 < 1" | bc -l) ));then
       echo "scale=3;a(sqrt((1/(1-($1^2)))-1))" | bc -l
    elif (( $(echo "$1 > 1" | bc -l) ));then
       echo "error"
    fi
}

acos ()
{
    if (( $(echo "$1 == 0" | bc -l) ));then
       echo "90"
    elif (( $(echo "$1 <= 1" | bc -l) ));then
       echo "scale=3;a(sqrt((1/($1^2))-1))" | bc -l
    elif (( $(echo "$1 > 1" | bc -l) ));then
       echo "error"
    fi
}

atan ()
{
    echo "scale=3;a($1)" | bc -l
}

acot ()
{
    echo "scale=5;a(1/$1)" | bc -l
}

asec ()
{
    echo "scale=5;a(sqrt(($1^2)-1))" | bc -l
}

acsc ()
{
    echo "scale=5;a(1/(sqrt($1^2)-1))" | bc -l
}




-------------------------------------------------------------------------------------------------------------------------------------------------



DEGREES

-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#trignometry
sin ()
{
    echo "scale=5;s($1*0.017453293)" | bc -l
}

cos ()
{
    echo "scale=5;c($1*0.017453293)" | bc -l
}

tan ()
{
    echo "scale=5;s($1*0.017453293)/c($1*0.017453293)" | bc -l
}

csc ()
{
    echo "scale=5;1/s($1*0.017453293)" | bc -l
}

sec ()
{
    echo "scale=5;1/c($1*0.017453293)" | bc -l
}

ctn ()
{
    echo "scale=5;c($1*0.017453293)/s($1*0.017453293)" | bc -l
}

asin ()
{
    if (( $(echo "$1 == 1" | bc -l) ));then
       echo "90"   
    elif (( $(echo "$1 < 1" | bc -l) ));then
       echo "scale=3;a(sqrt((1/(1-($1^2)))-1))/0.017453293" | bc -l
    elif (( $(echo "$1 > 1" | bc -l) ));then
       echo "error"
    fi
}

acos ()
{
    if (( $(echo "$1 == 0" | bc -l) ));then
       echo "90"
    elif (( $(echo "$1 <= 1" | bc -l) ));then
       echo "scale=3;a(sqrt((1/($1^2))-1))/0.017453293" | bc -l
    elif (( $(echo "$1 > 1" | bc -l) ));then
       echo "error"
    fi
}

atan ()
{
    echo "scale=3;a($1)/0.017453293" | bc -l
}

acot ()
{
    echo "scale=5;a(1/$1)/0.017453293" | bc -l
}

asec ()
{
    echo "scale=5;a(sqrt(($1^2)-1))/0.017453293" | bc -l
}

acsc ()
{
    echo "scale=5;a(1/(sqrt($1^2)-1))/0.017453293" | bc -l
}



-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

After saving it. Run ". .bashrc" (without quotes) in your terminal.

To find value of Sine of 45 , run sin 45 in terminal. Similarly for
Cosine ..              cos 45
Tangent  ..           tan 45
Cotangent ..         cot 45  
Secant ..               sec 45
Cosecant ..           csc 45
Arc Sine ..           asin  .73
Arc Cosine ..       acos  .73
Arc Tanget ..       atan  .75
Arc Cotangent ..  acot  .73
Arc Secant ..       asec 45
Arc Cosecant ..   acsc 45


Enjoy Linux !!

3 comments:

  1. Thanks, this was incredibly useful. Some small mistakes though. In the radians section you have asin 1 and acos 0 set to return 90 but in radians it should return pi/2. But that's easy to fix, just replace the lines
    echo "90"
    with
    echo "scale=3;a(1)*2" | bc -l"


    Also, you need to make a special case for asin -1. Add (immediately after the line above) the lines
    elif (( $(echo "$1 == -1" | bc -l) ));then
    echo "scale=3;-a(1)*2" | bc -l"
    to fix this for radians (same for degrees but it's just echo "-90").

    ReplyDelete
    Replies
    1. Another issue: asin(x) = atan(x/sqrt(1-x^2)) which you simplified to atan(sqrt(1/(1-x^2)-1)), but this is incorrect for negative x as it loses the sign of x. acos(x) has a similar problem with a more complicated solution, so it's probably best to just define acos(x) as pi/2-asin(x) (or 90-asin(x) for degrees).

      Delete
  2. Hi Anonymous,
    thanks for your hint. Could you or somebody else please just enter the correct code here for that issues. I'm not a math expert at that moment, sorry.

    ReplyDelete