tanh() Function in R with Example

The tanh is a Hyperbolic Tangent function. The tanh() function executes element-wise on arrays. The tanh function takes both real and complex data inputs. All angles are in radians.

In maths, the hyperbolic functions are analogs of the regular trigonometric functions but they are defined using a hyperbola rather than a circle.

The hyperbolic tangent of an angle x is the ratio of the hyperbolic sine(sinh) and hyperbolic cosine(cosh).

tanh() function

In terms of the traditional tangent function with a complex argument, the identity is,

tanh() Function in R

tanh() Function in R

The tanh() is an inbuilt mathematical R function that calculates the hyperbolic tangent of numeric data.




x: It is a numeric value, array, or vector.


Let’s calculate the tanh value of 1.



[1] 0.7615942

If you pass the 0 to the tanh() function, it will return 0.



[1] 0

Plot the tanh() function to a graph

We can use the seq() function to create a series of values and pass that to the plot() function, which will create a line chart.

dt <- seq(-1, 1, by = 0.05)
plot(dt, tanh(dt), typ = "l", col = "red")
abline(v = 0, lty = 6, col = "blue")


Plot the tanh() function to a graph

Applying tanh() function to a Vector

To create a Vector in R, use the c() function. Then pass that vector to the tanh() function.

rv <- c(-1, 0.5, 0, 0.5, 1)


[1] -0.7615942 0.4621172 0.0000000 0.4621172 0.7615942

Passing a pi to the tanh() function

The pi is an inbuilt constant in R programming, and its value is 3.141593.

Let’s find the pi constant’s tanh() value.



[1] 0.9962721

Let’s see another example of pi.

tanh(pi / 4)


[1] 0.6557942

That is it for tanh() function in R programming.

See also

R acos()

R asin()

R atan()

R tan()

R sin()

Leave a Comment