How To Use The Cosh Function in PHP With Examples

How to use the cosh function in php with examples

When it comes to trigonometry, the hyperbolic cosine which is a counterpart of the cosine function are function used in mathematics, physics, engineering, and other scientific disciplines in the context of geometry. The hyperbolic cosine function abbreviated to "cosh" is defined mathematically as the sum of the exponential function. It's used to show the exponential growth or decay of certain phenomena. Similar to normal cosine, but with a few differences, as it can take any number and produce a positive result. This is highly useful in various mathematical computations, in particular those in the fields of engineering, physics, and other applied sciences.

What is the Hyperbolic cosine (cosh) formula?

The hyperbolic cosine function can be broken down into the following formula; (e^x) and (e^(-x)) divided by two, as outlined below, where "e" is the base of the natural logarithm.

cosh(x) = (e^x + e^(-x)) / 2

Luckily if you're using PHP, instead of writing this function each time you want to work out the hyperbolic cosine of a number you can instead use the pre-defined function cosh(), which has been in PHP since the days of version 4 and still used in PHP 8+.

How to use the Cosh function in PHP?

To work out the hyperbolic cosine of a number in PHP, you pass the number into the cosh function in your PHP code. Then PHP will return a float which is your hyperbolic cosine number.

echo cosh(5);

# Outputs
74.209948524788

Why is the Cosh function useful?

There are many reasons why the cosh function in PHP is useful. Firstly, using this function means you're not required to work out the formula yourself which is prone to human error but in a more practical sense, using the cosh function or more importantly the hyperbolic cosine of a number could be used in the following ways.

  • To model temperature changes. Using the cosh function, you could model temperature changes over a time period in a given scenario, where they follow an exponential pattern, such as cooling down or heating up.
  • Financial growth prediction. You could use the Cosh function to predict financial growth such as investments or financial assets, again assuming it follows an exponential pattern
  • Model population growth. A popular use of the cosh function is to predict popular growth over time, following an exponential pattern

Let's take the population growth example, where we predict that the global population will increase by 1% over the next ten years. In the following PHP code, we use the cosh function to give us a positive final number. If we wanted we could make the number a whole number using PHP round or make the number a bit more human-readable, using the number_format function in PHP. Using the hyperbolic cosine formula, it predicts that following that the population would increase by 39,472,877.6242!

$initialPopulation = 7888000000;
$growthRatePercentage = 0.01;
$timeInYears = 10;

$finalPopulation = $initialPopulation * cosh($growthRatePercentage * $timeInYears);

echo "Final population after " . $timeInYears . " years: " . $finalPopulation;

# Outputs
Final population after 10 years: 7927472877.6242

Within engineering you may use hyperbolic functions can be used to describe the shapes of cables and suspension bridges, such as the curve of a freely hanging cable when subjected to uniform force. Or in physics, you might use hyperbolic functions in the context of special relativity such as the Lorentz factor.

Other mathematical functions used alongside the cosh function in PHP which are used in traditional trigonometry, include the cos function, used to find the Cosine of an angle that is radians, acosh which returns the inverse hyperbolic cosine of a number. Other PHP functions include sinh (which returns the hyperbolic sine of a number) and tanh (which returns the hyperbolic tangent of a number).

Whilst PHP isn't traditionally a scientific computing programming language, PHP does offer these mathematical functions (including many others) to provide users the option to build applications that require such complex mathematical operations, which are more likely to be used to process scientific data or calculations. PHP (for web-based tools) can help bridge the gap between complex theoretical models to online end-user interfaces. Not only does this versatility highlight PHP's feature capacity (especially when compared to languages like Python) but allows PHP developers to create web applications in scientific and technical fields.

Conclusion

Using PHP's built-in functions is a great way to keep your code robust and error and bug-free. The PHP cosh function is no different. So next time you're working with numbers and looking to work out the exponential change of something, be sure to use the cosh function to get the answer you need! Combine the cosh function with other built-in maths functions within your application.

Be sure to check out PHP's full list of Mathematical functions available to you as a developer.

Senior PHP developer with near two decades of PHP experience. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all