PHP cos() function
The cos()
function in PHP is used to calculate the cosine of a given angle, which is expected to be in radians. The cosine function is a fundamental trigonometric function that computes the ratio of the length of the adjacent side of a right-angled triangle to the length of the hypotenuse.
Syntax:
Parameters:
- $angle_in_radians: The angle in radians for which you want to calculate the cosine value.
Return Value:
- The function returns the cosine of the given angle as a float.
Important Note:
Since the cos()
function expects the angle in radians, if your angle is in degrees, you need to convert it to radians using the deg2rad()
function.
Example 1: Calculating Cosine of an Angle in Radians
Output:
In this example, or is 0.5.
Example 2: Calculating Cosine of an Angle in Degrees
To calculate the cosine of an angle given in degrees, convert it to radians using deg2rad()
.
Output:
Here, is approximately 0.7071.
Practical Usage:
- The
cos()
function is widely used in trigonometric calculations, particularly in geometry, physics, and engineering. - It's useful for calculating the horizontal component of a vector, modeling periodic functions (e.g., waves, oscillations), or working with angles in various fields.
Example 3: Cosine in Physics (Projectile Motion)
In physics, the horizontal distance traveled by a projectile launched at an angle with an initial velocity can be calculated as:
Where:
- is the initial velocity,
- is the time of flight,
- is the launch angle.
Output:
Summary:
cos()
calculates the cosine of an angle in radians.- If the angle is in degrees, use
deg2rad()
to convert it to radians before usingcos()
. - It is widely used in mathematical, physical, and engineering applications to compute angles, directions, or periodic functions.