Trigonometry For Matlab
Trigonometry For Matlab
Sine
Degrees/Radians Conversion
Y = sin(X)
Description
Y = sin(X) returns the sine of the elements of X. The sin function operates element-wise on
arrays. The function accepts both real and complex inputs.
For real values of X, sin(X) returns real values in the interval [-1, 1].
For complex values of X, sin(X) returns complex values.
Plot Sine Function
Example: Plot the sine function over the domain −π≤x≤π.
x = -pi:0.01:pi;
plot(x,sin(x)), grid on
Sine of Vector of Complex Angles
Y = sind(X)
Description
Y = sind(X) returns the sine of the elements in X, which are expressed in degrees.
Sine of 180 Degrees Compared to Sine of π Radians
Example:
sind(180)
ans = 0
sin(pi)
ans = 1.2246e-16
Sine of Vector of Complex Angles, Specified in Degrees
Example:
z = [90+i 15+2i 10+3i];
y = sind(z)
y = 1×3 complex
1.0002 + 0.0000i 0.2590 + 0.0337i 0.1739 + 0.0516i
sinpi
Compute sin(X*pi) accurately
Syntax
Y = sinpi(X)
Description
Y = sinpi(X) computes sin(X*pi) without explicitly computing X*pi. This calculation is more
accurate than sin(X*pi) because the floating-point value of pi is an approximation of π. In
particular:
For integers, sinpi(n) is exactly zero.
For odd integers, sinpi(n/2) is +1 or -1.
Calculate Sine of Multiples of π
Y = asin(X)
Description
Y = asin(X) returns the Inverse Sine (sin-1) of the elements of X in radians. The function accepts
both real and complex inputs.
For real values of X in the interval [-1, 1], asin(X) returns values in the interval [-π/2,
π/2].
For real values of X outside the interval [-1, 1] and for complex values
of X, asin(X) returns complex values.
Examples
Example: Find the inverse sine of the elements of vector x. The asin function acts on x element-
wise.
x = [0.5i 1+3i -2.2+i];
y = asin(x)
y = 1×3 complex
0.0000 + 0.4812i 0.3076 + 1.8642i -1.1091 + 1.5480i
Plot Inverse Sine Function
Y = asind(X)
Description
Y = asind(X) returns the inverse sine (sin-1) of the elements of X in degrees. The function accepts
both real and complex inputs.
For real values of X in the interval [-1, 1], asind(X) returns values in the interval [-90,
90].
For real values of X outside the interval [-1, 1] and for complex values
of X, asind(X) returns complex values.
Examples
Example: Plot the inverse sine function over the domain −1≤x≤1.
x = -1:.01:1;
plot(x,asind(x))
grid on
2. Cos
Cosine of argument in radians
Syntax
Y = cos(X)
Description
Y = cos(X) returns the cosine for each element of X. The cos function operates element-wise on
arrays. The function accepts both real and complex inputs.
For real values of X, cos(X) returns real values in the interval [-1, 1].
For complex values of X, cos(X) returns complex values.
Examples
Y = cosd(X)
Description
Y = cosd(X) returns the cosine of the elements of X, which are expressed in degrees.
Examples
Example:
cosd(90)
ans = 0
cos(pi/2)
ans = 6.1232e-17
Cosine of Complex Angles Specified in Degrees
Example: Create an array of three complex angles and compute the cosine.
z = [180+i 45+2i 10+3i];
y = cosd(z)
y = 1×3 complex
-1.0002 + 0.0000i 0.7075 - 0.0247i 0.9862 - 0.0091i
cospi
Compute cos(X*pi) accurately
Syntax
Y = cospi(X)
Description
Y = cospi(X) computes cos(X*pi) without explicitly computing X*pi. This calculation is more
accurate than cos(X*pi) because the floating-point value of pi is an approximation of π. In
particular:
For odd integers, cospi(n/2) is exactly zero.
For integers, cospi(n) is +1 or -1.
Calculate Cosine of Multiples of π
Y(2)
ans = 6.1232e-17
Use cospi to calculate the same values. In this case, the results are exact.
Z = cospi(X)
Z = 1×5
1 0 -1 0 1
Z(2)
ans = 0
acos
Inverse cosine in radians
Syntax
Y = acos(X)
Description
Y = acos(X) returns the Inverse Cosine (cos-1) of the elements of X in radians. The function
accepts both real and complex inputs.
For real values of X in the interval [-1, 1], acos(X) returns values in the interval [0, π].
For real values of X outside the interval [-1,1] and for complex values
of X, acos(X) returns complex values.
Inverse Cosine of Value
Example: Find the inverse cosine of the elements of vector x. The acos function acts
on x element-wise.
x = [0.5i 1+3i -2.2+i];
y = acos(x)
y = 1×3 complex
1.5708 - 0.4812i 1.2632 - 1.8642i 2.6799 - 1.5480i
Plot Inverse Cosine Function
Y = acosd(X)
Description
Y = acosd(X) returns the inverse cosine (cos-1) of the elements of X in degrees. The function
accepts both real and complex inputs.
For real values of X in the interval [-1, 1], acosd(X) returns values in the interval [0,
180].
For values of X outside the interval [-1, 1] and for complex values of X, acosd(X) returns
complex values.
Inverse Cosine of 0
Example: Show that the inverse cosine, followed by cosine, returns the original values of X.
cosd(acosd([2 3]))
ans = 1×2
2 3
acosd([2 3]) returns two complex angles, which are then passed to the cosd function. cosd returns
the original values, 2 and 3.
3. Tan
Tangent of argument in radians
Syntax
Y = tan(X)
Description
Y = tan(X) returns the tangent of each element of X. The tan function operates element-wise on
arrays. The function accepts both real and complex inputs.
For real values of X, tan(X) returns real values in the interval [-∞, ∞].
For complex values of X, tan(X) returns complex values.
Plot Tangent Function
Y = tand(X)
Description
Y = tand(X) returns the tangent of the elements of X, which are expressed in degrees.
Examples
Example:
tand(90)
ans = Inf
tan(pi/2)
ans = 1.6331e+16
tand(90) is infinite, whereas tan(pi/2) is large but finite.
Tangent of Vector of Complex Angles, Specified in Degrees
Example
z = [180+i 15+2i 10+3i];
y = tand(z)
y = 1×3 complex
0.0000 + 0.0175i 0.2676 + 0.0374i 0.1758 + 0.0539i
atan
Inverse tangent in radians
Syntax
Y = atan(X)
Description
Y = atan(X) returns the Inverse Tangent (tan-1) of the elements of X in radians. The function
accepts both real and complex inputs.
For real values of X, atan(X) returns values in the interval [-π/2, π/2].
For complex values of X, atan(X) returns complex values.
Inverse Tangent of a Value
Example: Find the inverse tangent of the elements of vector x. The atan function acts
on x element-wise.
x = [0.5i 1+3i -2.2+i];
Y = atan(x)
Y = 1×3 complex
0.0000 + 0.5493i 1.4615 + 0.3059i -1.2019 + 0.1506i
Plot the Inverse Tangent Function
atand
Inverse tangent in degrees
Syntax
Y = atand(X)
Description
Y = atand(X) returns the inverse tangent (tan-1) of the elements of X in degrees. The function
accepts both real and complex inputs.
For real values of X, atand(X) returns values in the interval [-90, 90].
For complex values of X, atand(X) returns complex values.
Inverse Tangent of Vector
Example
x = [-50 -20 0 20 50];
y = atand(x)
y = 1×5
-88.8542 -87.1376 0 87.1376 88.8542
The atand operation is element-wise when you pass a vector, matrix, or N-D array.
Inverse Tangent of Complex Value
Example:
atand(10+i)
ans = 84.3450 + 0.5618i
atan2
Four-quadrant inverse tangent
Syntax
P = atan2(Y,X)
Description
P = atan2(Y,X) returns the four-quadrant inverse tangent (tan-1) of Y and X, which must be real.
The atan2 function follows the convention that atan2(x,x) returns 0 when x is mathematically
zero (either 0 or -0).
Examples
D = atan2d(Y,X)
Description
D = atan2d(Y,X) returns the four-quadrant inverse tangent (tan-1) of Y and X, which must be
real. The result, D, is expressed in degrees.
Inverse Tangent of Four Points on the Unit Circle
Example:
x = [1 0 -1 0];
y = [0 1 0 -1];
d = atan2d(y,x)
d = 1×4
0 90 180 -90
Four-Quadrant Inverse Tangent
The four-quadrant inverse tangent, atan2d(Y,X), returns values in the closed interval [-
180,180] based on the values of Y and X as shown in the graphic.
In contrast, atand(Y/X) returns results that are limited to the interval [-90,90], shown on the right
side of the diagram.
4. Csc
Cosecant of input angle in radians
Syntax
Y = csc(X)
Description
Y = csc(X) returns the cosecant of the elements of X. The csc function operates element-wise on
arrays. The function accepts both real and complex inputs.
For real values of X, csc(X) returns real values in the interval [-∞, -1] and [1, ∞].
For complex values of X, csc(X) returns complex values.
Plot Cosecant Function
Plot the cosecant function over the domain −π<x<0 and 0<x<π as shown.
x1 = -pi+0.01:0.01:-0.01;
x2 = 0.01:0.01:pi-0.01;
plot(x1,csc(x1),x2,csc(x2)), grid on
Cosecant of Vector of Complex Angles
Y = cscd(X)
Description
Y = cscd(X) returns the cosecant of the elements of X, which are expressed in degrees.
Cosecant of 180 Degrees Compared to Cosecant of π Radians
Example
z = [35+i 15+2i 10+3i];
y = cscd(z)
y = 1×3 complex
1.7421 - 0.0434i 3.7970 - 0.4944i 5.2857 - 1.5681i
cospi
Compute cos(X*pi) accurately
Syntax
Y = cospi(X)
Description
Y = cospi(X) computes cos(X*pi) without explicitly computing X*pi. This calculation is more
accurate than cos(X*pi) because the floating-point value of pi is an approximation of π. In
particular:
For odd integers, cospi(n/2) is exactly zero.
For integers, cospi(n) is +1 or -1.
Calculate Cosine of Multiples of π
Y(2)
ans = 6.1232e-17
Use cospi to calculate the same values. In this case, the results are exact.
Z = cospi(X)
Z = 1×5
1 0 -1 0 1
Z(2)
ans = 0
acsc
Inverse cosecant in radians
Syntax
Y = acsc(X)
Description
Y = acsc(X) returns the Inverse Cosecant (csc-1) of the elements of X in radians. The function
accepts both real and complex inputs.
For real values of X in the intervals [-∞, -1] and [1, ∞], acsc(X) returns real values in the
interval [-π/2, π/2].
For real values of X in the interval (-1, 1) and for complex values of X, acsc(X) returns
complex values.
Inverse Cosecant of a Value
Example: Find the inverse cosecant of the elements of vector x. The acsc function acts
on x element-wise.
x = [0.5i 1+3i -2.2+i];
Y = acsc(x)
Y = 1×3 complex
0.0000 - 1.4436i 0.0959 - 0.2970i -0.3795 - 0.1833i
Plot the Inverse Cosecant Function
Plot the inverse cosecant function over the intervals −10≤x<−1 and 1<x≤10.
x1 = -10:0.01:-1.01;
x2 = 1.01:0.01:10;
plot(x1,acsc(x1),'b')
hold on
plot(x2,acsc(x2),'b')
grid on
acscd
Inverse cosecant in degrees
Syntax
Y = acscd(X)
Description
Y = acscd(X) returns the inverse cosecant (cosec-1) of the elements of X in degrees. The function
accepts both real and complex inputs.
For real values of X in the intervals [-∞, -1] and [1, ∞], acscd(X) returns values in the
range [-90, 90].
For real values of X in the interval (-1, 1) and for complex values of X, acscd(X) returns
complex values.
Inverse Cosecant of Vector
Example:
x = [20 10 Inf];
y = acscd(x)
y = 1×3
2.8660 5.7392 0
The acscd operation is element-wise when you pass a vector, matrix, or N-D array.
Inverse Cosecant of Complex Value
Example:
acscd(1+i)
ans = 25.9136 -30.4033i
5. sec
Secant of angle in radians
Syntax
Y = sec(X)
Description
Y = sec(X) returns the secant of the elements of X. The sec function operates element-wise on
arrays. The function accepts both real and complex inputs.
For real values of X, sec(X) returns real values in the interval [-∞, -1] and [1, ∞].
For complex values of X, sec(X) returns complex values.
Plot Secant Function
Y = secd(X)
Description
Y = secd(X) returns the secant of the elements of X, which are expressed in degrees.
Examples: Secant of 90 Degrees Compared to Secant of π/2 Radians
secd(90)
ans = Inf
sec(pi/2)
ans = 1.6331e+16
secd(90) is infinite, whereas sec(pi/2) is large but finite.
Secant of Vector of Complex Angles, Specified in Degrees
Example:
z = [35+i 15+2i 10+3i];
y = secd(z)
y = 1×3 complex
1.2204 + 0.0149i 1.0346 + 0.0097i 1.0140 + 0.0094i
asec
Inverse secant in radians
Syntax
Y = asec(X)
Description
Y = asec(X) returns the Inverse Secant (sec-1) of the elements of X in radians. The function
accepts both real and complex inputs.
For real values of X in the interval [-∞, -1] and [1, ∞], asec(X) returns values in the
interval [0, π].
For real values of X in the interval (-1, 1) and for complex values of X, asec(X) returns
complex values.
Inverse Secant of a Value
Example: Find the inverse secant of the elements of vector x. The asec function acts
on x element-wise.
x = [0.5i 1+3i -2.2+i];
Y = asec(x)
Y = 1×3 complex
1.5708 + 1.4436i 1.4749 + 0.2970i 1.9503 + 0.1833i
Plot the Inverse Secant Function
Example: Plot the inverse secant function over the intervals −5≤x≤−1 and 1≤x≤5.
x1 = -5:0.01:-1;
x2 = 1:0.01:5;
plot(x1,asec(x1),'b')
hold on
plot(x2,asec(x2),'b')
grid on
asecd
Inverse secant in degrees
Syntax
Y = asecd(X)
Description
Y = asecd(X) returns the inverse secant (sec-1) of the elements of X in degrees. The function
accepts both real and complex inputs.
For real values of X in the intervals [-∞, -1] and [1, ∞], asecd(X) returns values in the
interval [0, 180].
For real values of X in the interval (-1, 1) and for complex values of X, asecd(X) returns
complex values.
Inverse Secant of Vector
Example:
x = [10 1 Inf];
y = asecd(x)
y = 1×3
84.2608 0 90.0000
The asecd operation is element-wise when you pass a vector, matrix, or N-D array.
Inverse Secant of Complex Value
Example
asecd(1+i)
ans = 64.0864 +30.4033i
6. cot
Cotangent of angle in radians
Syntax
Y = cot(X)
Description
Y = cot(X) returns the cotangent of elements of X. The cot function operates element-wise on
arrays. The function accepts both real and complex inputs.
For real values of X, cot(X) returns real values in the interval [-∞, ∞].
For complex values of X, cot(X) returns complex values.
Plot Cotangent Function
Example: Plot the cotangent function over the domain −π<x<0 and 0<x<π.
x1 = -pi+0.01:0.01:-0.01;
x2 = 0.01:0.01:pi-0.01;
plot(x1,cot(x1),x2,cot(x2)), grid on
Cotangent of Vector of Complex Angles
Y = cotd(X)
Description
Y = cotd(X) returns the cotangent of the elements of X, which are expressed in degrees.
Example:
cotd(90)
ans =
0
cotd(pi/2)
ans =
36.466487130706170
Cotangent of Complex Angle, Specified in Degrees
Example
x = 35+5i;
y = cotd(x)
y = 1.3958 - 0.2606i
acot
Inverse cotangent in radians
Syntax
Y = acot(X)
Description
Y = acot(X) returns the Inverse Cotangent (cot-1) of the elements of X in radians. The function
accepts both real and complex inputs.
For real values of X, acot(X) returns values in the interval [-π/2, π/2].
For complex values of X, acot(X) returns complex values.
Inverse Cotangent of a Value
Example: Find the inverse cotangent of the elements of vector x. The acot function acts
on x element-wise.
x = [0.5i 1+3i -2.2+i];
Y = acot(x)
Y = 1×3 complex
1.5708 - 0.5493i 0.1093 - 0.3059i -0.3689 - 0.1506i
Plot the Inverse Cotangent Function
Example: Plot the inverse cotangent function over the intervals −2π≤x<0 and 0<x≤2π.
x1 = -2*pi:pi/30:-0.1;
x2 = 0.1:pi/30:2*pi;
plot(x1,acot(x1),'b')
hold on
plot(x2,acot(x2),'b')
grid on
acotd
Inverse cotangent in degrees
Syntax
Y = acotd(X)
Description
Y = acotd(X) returns the inverse cotangent (cot-1) of the elements of X in degrees. The function
accepts both real and complex inputs.
For real values of X, acotd(X) returns values in the range [-90, 90].
For complex values of X, acotd(X) returns complex values.
Inverse Cotangent of Vector
Example
x = [0 20 Inf];
y = acotd(x)
y = 1×3
90.0000 2.8624 0
The acotd operation is element-wise when you pass a vector, matrix, or N-D array.
Inverse Cotangent of Complex Value
Example:
acotd(1+i)
ans = 31.7175 -23.0535i
hypot
Square root of sum of squares (hypotenuse)
Syntax
C = hypot(A,B)
Description
Example: Compute the hypotenuse of a right triangle with side lengths of 3 and 4.
C = hypot(3,4)
C=5
deg2rad
Convert angle from degrees to radians
Syntax
R = deg2rad(D)
Description
R = deg2rad(D) converts angle units from degrees to radians for each element of D.
Right Angle in Radians
D = rad2deg(R)
Description
D = rad2deg(R) converts angle units from radians to degrees for each element of R.
Example: Convert pi into degrees.
D = rad2deg(pi)
D = 180