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).
In terms of the traditional tangent function with a complex argument, the identity is,
tanh in R
The tanh() is a built-in mathematical R function that calculates the hyperbolic tangent of numeric data. It takes a numerical value, vector, or array and returns the hyperbolic tangent.
Syntax
tanh(x)
Parameters
x: It is a numeric value, array, or vector.
Example
Let’s calculate the tanh value of 1.
tanh(1)
Output
[1] 0.7615942
If you pass the 0 to the tanh() function, it will return 0.
tanh(0)
Output
[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")
Output
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)
tanh(rv)
Output
[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.
tanh(pi)
Output
[1] 0.9962721
Let’s see another example of pi.
tanh(pi / 4)
Output
[1] 0.6557942
That is it for the tanh() function in R programming.
See also

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.