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

Advertisements

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

Advertisements

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

R Lesson 33: Inverse Trigonometric Ratios in R

Advertisements

Hello everybody,

Michael here, and in today’s post, we’ll be expanding our knowledge of R trigonometry by learning inverse trigonometric ratios in R!

In the previous post, we learned some basics of R trigonomtery (and trigonometry in general). However, let’s explore some more advanced R trigonometrical concepts!

Inverse Trigonometric Functions

In the previous post R Lesson 32: Basic Trigonometry, R Style, we learned about the three basic trigonometric concepts-sine, cosine and tangent. Today, we’ll explore inverse trigonometric functions such as the arc-sine, arc-cosine and arc-tangent.

What do these trigonometric concepts represent? Well, let’s go back to our triangle illustration from the previous post:

This illustration gives a visual representation of the three most basic trigonometric concepts-sine, cosine, and tangent with the classic SOHCAHTOA mnemonic.

Now, I did mention that the sine of an angle in a right triangle is the ratio of the opposite side’s length to the hypotenuse’s length. With that said, arcsine is simply the inverse of sine-meaning arcsine is the ratio of the hypotenuse’s length to the opposite side’s length.

The same logic applies for arccosine and arctangent, as these ratios are simply the inverse of the cosine and tangent ratios, respectively. Arccosine would be the ratio of the hypotenuse’s length to the adjacent side’s length while arctangent would be the ratio of the adjacent side’s length to the opposite side’s length.

How would these inverse trigonometric ratios affect our calculations in this triangle? Let’s find out using the 38 degree angle as an example:

RATIONUMERIC FORM
sine7/12.2~0.57
arcsine12.2/7~1.74
cosine10/12.2~0.82
arccosine12.2/10=1.22
tangent7/10=0.7
arctangent10/7~1.43

As you can see here, the three regular trigonometric ratios yielded values less than 1 while the three inverse trigonometric ratios yiedled values greater than 1. Interesting, isn’t it?

And now, let’s explore how to work with more advanced trigonometry in R!

Advanced Trigonometry, R style

Now that we’ve seen how to use the basic trigonometric ratios in R, let’s see how we can utilize these more advanced trigonometric ratios!

> asin(31)
[1] NaN
Warning message:
In asin(31) : NaNs produced
> acos(54)
[1] NaN
Warning message:
In acos(54) : NaNs produced
> atan(14)
[1] 1.499489

As I did when first testing out the sine, cosine, and tangent functions in R, I tested the three inverse trigonometric functions (arcsine, arccosine and arctangent) in R using whole numbers as parameters. However, you can see that for the arcsine and arccosine functions (asin() and acos() respectively), that didn’t quite work out. Interestingly, using a whole number for the atan() function worked just fine.

How can that be? Well, just like the regular trigonometric functions in R, these inverse trigonometric functions calculate the ratios using radians (more on those here: R Lesson 32: Basic Trigonometry, R Style). However, the asin() and acos() functions only take input values ranging from -1 to 1 because they only work with a limited range of angles. Arcsine only works with angles ranging from -π/2 to π/2 radians (-90 to 90 degrees) while arccosine only works with angles ranging from 0 to π radians (or 0 to 180 degrees). Arctangent, on the other hand, can take a wider range of numerical inputs since it works with angles of any length (in fact, the angle lengths arctangent works with encompass negative infinity to positive infinity).

Let’s try executing our inverse trigonometric functions in R with the new inverse trigonometric ratio information that we learned!

> asin(0.5)
[1] 0.5235988
> acos(0.43)
[1] 1.126304
> atan(22)
[1] 1.525373

As you can see, with the rules we discussed above, we’re now able to obtain valid outputs for the asin(), acos() and atan() functions!

Thanks for reading,

Michael

R Lesson 32: Basic Trigonometry, R Style

Advertisements

Hello everybody,

So far this year, we’ve explored the basics on making our own Python game with the pygame module-pretty cool right! After all, it was the first time this blog delved into game development.

However, building a good game takes time; this includes the BINGO game we’ve been developing. With that said, I’ll need to spend a little more time fine-tuning the BINGO game and planning out the game development series going forward. I’d hate to go too long without keeping fresh content on this blog, so with that in mind, in today’s post, we’ll explore something a little different-R trigonometry (you may recall I did a number of R mathematics posts last year)

Last year, we explored some basic R calculus-this year, we’ll explore basic R trigonometry. For those who don’t know what I’m talking about, trigonometry is a branch of mathematics that deals with the study of triangles and their angles.

Trigonometry basics

Before we get into the fun of exploring trig with R, let’s explore some basic trigonometry terms using this handy-dandy illustration:

Here we have a simple right triangle with a 90 degree angle, a 52 degree angle, and a 38 degree angle along with two sides of lengths 10 and 7 and a hyptoensue of length 12.2 (remember the Pythagorean theorem for right triangles-to find the length of the hypotenuse, a squared + b squared = c squared).

If you’ve ever taken at least precalculus with basic trigonometry, you’ll certainly recognize the mnemonic on the upper-right hand corner of the screen-SOHCAHTOA. If you’re not familiar with this mnemonic, here’s what it means:

  • SOH: To find the sine of an angle in the triangle, take the ratio of the length of the side opposite the angle to the length of the hypotenuse
  • CAH: To find the cosine of an angle in the triangle, take the ratio of the length of the side adjacent to the hypotenuse to the length of the hypotenuse
  • TOA: To find the tangent of an angle in the triangle, take the ratio of the length of the side opposite to the hypotenuse to the length of the side adjacent to the hypotenuse

Trigonometry, R style

Now that we’ve discussed the basics of trigonometry, let’s discuss trigonometry, R style. Here are some examples of the three basic trigonometric functions executed in R:

> cos(20)
[1] 0.4080821
> tan(20)
[1] 2.237161
> sin(20)
[1] 0.9129453

In this example, I used R’s built-in cos(), tan() and sin() functions to calculate the cosine, tangent, and sine of a given angle, respectively. Since these functions are built-in to R, there’s no need to install any extra packages to utilize these functions

You may be wondering if these functions can calculate the sine/cosine/tangent of an angle given specific side lengths (as shown in the illustration above). The answer to that is no, but then again, you can easily calculate these trigonometric ratios using simple division. For instance, in the illustration above, the sine of the 52 degree angle in the triangle is ~0.82 (rounded to two decimal places) because the opposite/hypotenuse length ratio is 10/12.2.

So, how does R calculate trigonometric ratios of certain angles? They use a little something called radians, which I’ll explain more right here.

Radians

What are radians, exactly? Well, when measuring angles in shapes, there are two metrics we use-degrees (which I’m sure you’re familiar with) and radians.

Let’s take this illustration of a circle:

As you see, the length of this circle’s radius is 8-the arc of the circle that is formed also has a length of 8. Therefore, the angle that is formed at the circle’s center has a length of 1 radian.

Now, how do you convert radians to degrees. Easy-1 degree is equal to pi/180 radians.

Why are radians expressed as a ratio of pi? This is because, for one, the circumference of a circle is 2pi times the circle’s radius. For two, the length of a full circle is 360 degrees-or 2pi radians; similarly, the length of a half-circle is 180 degrees-or pi radians.

Now, let’s analyze how radians work in the context of our R examples (all of which used 20 degree angles).

I mentioned earlier that 1 degree equals pi/180 radians, so 20 degrees would be 20*(pi/180) radians, which when converted to simplest form, equals pi/9 radians, which is then used to calculate various trigonometric ratios for any given angle.

The degrees-to-radians formula is so versatile that in R, it can be used on any integer, positive or negative. Check out some of these examples:

> cos(-3)
[1] -0.9899925
> sin(355)
[1] -3.014435e-05
> tan(15000)
[1] -1.98891
> cos(412)
[1] -0.8998537
> sin(-1333)
[1] -0.8218865
> tan(10191)
[1] -0.338695

Yes, I can use R’s basic trigonometric functions on a wide variety of integers and obtain valid results from each integer (although let’s be real, where will you ever find a 15000 degree angle)?

Thanks for reading,

Michael