R Lesson 35: Let’s Plot Some Inverse Trigonometric Functions

Hello everybody,

Michael here, and in today’s post, I’ll show you to how to plot some inverse trigonometric functions with R!

In the previous post, we explored how to create R plots of the three basic trigonometric functions-sine, cosine and tangent. This time, we’ll explore how to create R plots of the three basic inverse trigonometric functions-arcsine, arccosine and arctangent. Let’s begin!

First off, the arcsine:

To start off our exploration of plotting inverse trigonometric functions, let’s explore how we can plot the arcsine:

> x <- seq(-3*pi, 3*pi, length.out=100)
> y <- asin(x)
Warning message:
In asin(x) : NaNs produced
> plot(x, y, type='l')

As you can see, we can’t quite use the same approach to plotting the arcsine function that we used to plot the sine function since our sequence of 100 values from -3pi to 3pi yielded all nulls when trying to calculate the arcsine of each value. Let’s try a slightly different approach to plotting the arcsine function, shall we?

> x <- seq(-1, 1, length.out=100)
> y <- asin(x)
> plot(x, y, type='l')

The only modification I made from the previous example was to use a sequence from -1 to 1 (still maintaining 100 equally spaced variables).

Why did I stick with the (-1, 1) sequence? Simply put, the arcsine function is only defined within the range (-1, 1). In other words, it’s not possible to calculate the arcsine of any value outside of the range (-1, 1)-trying to do so will give you an NaN or not a number in R.

Next up, the arccosine

And for our next R plot, let’s graph the arccosine function! Here’s the code to use for a sample arccosine function:

> x <- seq(-1, 1, length.out=100)
> y <- acos(x)
> plot(x, y, type='l')

Aside from using the acos() function, we used the same logic to create this plot that we used for the arcsine plot. Both the arcsine and arccosine functions are only defined for the range (-1, 1), meaning that you will get an NaN in R if you try calculating the arcsine or arccosine for any value outside of this range.

Now, you may have noticed that our arccosine plot looks like a vertical reflection of the arcsine plot. How could that be? The range of x-axis values is the same for both plots, but notice the difference in the range of y-axis values between the two plots. The arcsine plot’s y-axis value range is (-1.5, 1.5) while the arccosine plot’s y-axis value range is (0, 3).

Why do the y-axes in both plots have different value ranges? An easy explanation would be that the arccosine plot is the vertical reflection of the arcsine plot shifted pi/2 radians (or 90 degrees) upward, hence why the arccosine’s y-axis value ranges are higher.

Last but not least, the arctangent

Saving the best for last, let’s plot an arctangent function in R! Here’s the code for a sample arctangent plot:

> x <- seq(-30, 30, length.out=100)
> y <- atan(x)
> plot(x, y, type='l')

For creating the arctangent plot, we used similar logic (aside from the atan() function) that we used to create the arcsine and arccosine plots. However, notice that I didn’t use the (-1, 1) sequence range but rather the range of (-30, 30).

You might be thinking, wouldn’t using a sequence outside of the (-1, 1) range give you a bunch of NaNs? In the case of the arctangent function, no. This is because arctangent, unlike arcsine and arccosine, is defined for the range (-infinity, +infinity). In other words, arctangent functions have no finite range, so you could use any sequence of values you want when creating an arctangent plot (I kept it simple with the -30, 30 range).

However, one interesting thing you’ll notice with the arctangent plot is that its y-axis has a range from (-1.5, 1.5). How is that possible? Even though you could a sequence of literally any two numbers, the range of possible arctangent values will range from approximately -1.5 to 1.5.

Another interesting thing about the arctangent function is that the lower part of the function (the part pointing towards -1.5) represents -infinity in the arctangent function while the upper part of the function (the part pointing away from 1.5) represents +infinity.

Thanks for reading,

Michael

R Lesson 34: Let’s Plot Some Trigonometric Functions

Hello everybody,

Michael here, and in the last two posts we discussed the basics of trigonometry-both with R and basic trig in general. This time, we’ll explore how to plot the three basic trigonometric functions with R-sine, cosine and tangent!

A basic R sine plot

To start our lesson, we’ll create a basic R plot of a sine function. Here’s the code we’ll utilize to create our basic R sine plot:

> x <- seq(0,7*pi,length.out=100)
> y <- sin(x)
> plot(x, y, type='l')

As you can see, these three lines of R code gave us a very simple R sine plot. How did the code accomplish this graph creation? Let’s explain!

  • The seq() function used for the x variable takes 3 parameters-a starting point for the sequence (0), an ending point for the sequence (7*pi), and a value for length.out which indicates how many equally spaced values you want in the sequence (I opted for 100 in this case). The sequence itself will be represented on the x-axis
  • The y variable takes the sine of all 100 values genereated from the sequence in the x variable-these sine values are then plotted on the y-axis.
  • The plot() function takes both the x and y variables along with a type parameter, which indicates the style of graph you want to plot. In this case, I set type to l, indicating I want to plot the sine function with a [solid] linear style.

Now, what exactly does 7*pi mean here? In this case, it indicates that the sequence will end at 7*pi, or roughly 21.98. Something else to note whenever you use pi in trigonometric function plots-sine functions have these things called periods, which in plain English represent the point in a function where it repeats its values. Sine functions have a period of 2*pi which means that they repeat their values every 2*pi-or 6.28-units. Since the endpoint of this sequence is 7*pi, there are 3 1/2 periods in this graph as shown by the 3 1/2 low and high points on this graph.

A basic R cosine plot

Now that we’ve explored sine plots with R, let’s turn our attention to cosine plots! Here’s the code to create a basic cosine plot in R:

> x <- seq(0, 10*pi, length.out=100)
> y <- cos(x)
> plot(x, y, type='l')

Conceptually, the cosine plot works the same way as the sine plot, as both plots have periods of 2*pi (represented by the peaks and valleys in this graph). Since the endpoint of the sequence is 10*pi, this cosine plot will have five periods. Both plots will also generate a sequence of X equally spaced values (X being the number specified in the length.out parameter).

The one difference between the cosine and sine plots? The former calculates out the cosine for all sequence values while the latter calculates the sine for all sequence values.

Last but not least, let’s explore tangent plots!

A basic R tangent plot:

Just as we explored basic R sine and cosine plots, let’s explore how to create a basic R tangent plot! Here’s the code for one such plot:

> x <- seq(-5*pi, 3*pi, length.out=100)
> y <- tan(x)
> plot(x, y, type='l')

Creating the tangent plot follows the same logic as creating the sine and cosine plots, with the exception that you’re looking for the tangent of all the equally spaced values in the sequence.

You may also be wondering why the tangent plot looks so different from the sine and cosine plots. One main reason for this is because tangent functions, unlike sine and cosine functions, has a little something called asymptotes.

What are asymptotes? To explain this concept, I feel it is important to mention that sine and cosine functions have a range of values between -1 and 1, which explains why we get smooth, wave-like plots as shown earlier in this post. However, tangent functions have a range of values between negative infinity and positive infinity. Asymptotes are straight imaginary lines that approach various curves on the tangent plot but never fully meet them.

Still a little confused? Allow me to illustrate:

This is the tangent plot we just created. Pay attention to the two curves on this graph with the red line borders (aka the asymptotes). The asymptotes on the first curve (the one that appears to have its lowest point at -60 on the y-axis) appears to be going down to negative infinity, yet the asymptotes will never touch the curve no matter how far down it goes. Likewise for the second curve (the one that appears to have its highest point at 60), which appears to be going up to positive infinity but similar to the first curve, the asymptotes will never touch the curve no matter how high it goes.

Thanks for reading,

Michael