User Tools

Site Tools


gams:missing_trig_functions_arccos_arcsin_tan

Missing trig functions (arccos, arcsin, tan)

My model requires the tangent and inverse sine and cosine functions, but these functions are not built in to GAMS. What should I do?

Note:

Older distributions: All these functions can be constructed from the GAMS built-in functions. For example, the definition of tan(x) is sin(x)/cos(x). While computing arcsin and arccos from the GAMS built-in function arctan is less trivial, the formulas, derivations, and some examples are given in the model below.

$TITLE ARCTRIG  inverse sine and cosine example
*
* The GAMS built-in functions include the arctan function,
* but arcsin, arccos, and tan are not included.
* One can obtain the inverse sine and cosine functions
* by using only the arctan function, using the formulas
*
*    arcsin(y) =        arctan(y/sqrt(1-y*y)), y in (-1,1)
*    arccos(y) = pi/2 - arctan(y/sqrt(1-y*y)), y in (-1,1)
*
* while tan(x) = sin(x) / cos(x).
*
*
* To derive the above formula for arcsin(x), we have
*         y = sin x
*  arcsin y = x,
*     cos x = sqrt(1 - (sin x)**2) when x is in [-pi/2, pi/2],
*
*  Now use the formula for tan x and substitute from above.
*
*  tan x = sin x / cos x
*        = y / sqrt(1-y**2),    and
*
*      x = arctan( y / sqrt(1-y**2) ) = arcsin(y)
*
*
* To get the formula for arccos(y), we note that sine and cosine
* are horizontal translations of each other, so we use the identity
*
*      cos x = sin(pi/2 - x)
*
* and the formula for arcsin(y) above.

 
sets J  / 1 * 10 /;

scalar N;
N = card(J) + 1;
scalar pi;
pi = arctan(INF) * 2;
* the arctan function can accept an infinite argument
* arctan(1) * 4 also works


* The range of the arcsin function is usually taken as [-pi/2, pi/2]
parameters
x(J),
y(J),
arcsiny(J),
sin_diff(J);

x(J) = (-pi/2) + pi * ord(J)/N;
y(J) = sin(x(J));
arcsiny(J) = arctan( y(J) / sqrt(1-sqr(y(J))) );
sin_diff(J) = arcsiny(J) - x(J);

display sin_diff;


* The range of the arccos function is usually taken as [0, pi]
parameters
arccosy(J),
cos_diff(J);

x(J) =  pi * ord(J)/N;
y(J) = cos(x(J));
arccosy(J) = (pi/2) - arctan( y(J) / sqrt(1-sqr(y(J))) );
cos_diff(J) = arccosy(J) - x(J);

display cos_diff;
gams/missing_trig_functions_arccos_arcsin_tan.txt · Last modified: 2017/09/02 17:21 by support