Thursday, June 13, 2019

Using the random function in Desmos

Desmos has recently added a new function to its graphing calculator for generating random numbers.

Basic usage

The simplest way to use it is to type random() to get a random number between 0 and 1.



You can get the calculator to generate new random numbers by using the crossed arrows next to the plus.




Rolling a die

What if you don't want a random number between 0 and 1? For instance, perhaps you want to simulate a dice roll.

In the olden days you might do something like floor(6*random() + 1) and this also works in Desmos. However, we can also use Desmos to select a random element of a list. So, this is a more elegant solution:


Or, if you don't want to type all the numbers on a die, you could even use this:





Drawing from a distribution

Desmos also has the ability to work with probability distributions, and the random function works on these too. (If you didn't know about the distribution functions you should stop reading now, and go and explore them as they are very impressive!)

For instance, if you know that IQs are normally distributed with a mean of 100 and a standard deviation of 15, you can set this up in Desmos and then ask for a random IQ from this distribution.



If you are concerned that IQs should be integers even though the underlying distribution is normal, you can treat it like any other expression and write round(I.random).

If you want to simulate rolling 100 dice and counting the number of 3s that are rolled, this could be accomplished using a binomial distribution.


Maybe you want the fun of using distributions but you want all outcomes to be equally likely? For instance, maybe you want to choose a random real number between 5 and 20. You could devote a few seconds to thinking and come up with an expression like 15random() + 5 but why would you do that when Desmos has uniform distributions?



Generating multiple random numbers

With all the methods shown so far, it is also possible to ask Desmos to generate multiple random numbers at once. For instance, perhaps you want to generate 100 random numbers between 0 and 1?

Simple enough, just use random(100).

Or if you want to simulate the rolling of 5 dice, you could use [1,...,6].random(5). Perhaps you want to note their total? That is straightforward.



You can also draw a number of random samples from some distribution. For instance, perhaps you want to generate a sample of 5000 random IQs based on the normal distribution described above. The results can then be plotted as a histogram.




Once the sample is shown, it is worth clicking on the graph of the distribution itself, to see how closely it matches the histogram. You can also generate another random sample using the crossed arrows up the top.

There are many more applications of the random function for helping to visualise distributions and sampling but hopefully this gives you some idea of the potential.




Monday, July 3, 2017

Five cool things with Dynamic Labels in Desmos

With dynamic labels now in Desmos, a new world of possibilities is opened up for interacting with graphs and diagrams. This post is my attempt to highlight five uses where I see dynamic labels enhancing a graphing screen.

The basic idea is that in your point labels you can now include variables, and the label will dynamically update as the variable changes. It is helpful to note that if you hide a point you can still show a label, so this allows you to get text anywhere on the screen.

Making a dynamic label in Desmos

In each of the examples listed below, I've shown how it applies to a basic graphing screen, but they work even better as screens within larger Desmos activities.

1. Maximum area for a fixed perimeter

This classic problem can now be explored completely from a graphing screen.

For example, below is an example of how it might look if only three sides of fencing area required, and the maximum area is sought. Spoiler alert: the maximum area is not generated by a square.

Link to the graph is here.

Finding the maximum area with three sides of fencing


2. Unit Circle

The unit circle in Desmos was always good but now you can have the angle displaying on the arc, so students don't need to look over to the expression list to see its size.

Link to the graph is here.

Exploring angles in a unit circle

3. Exploring gradient

Having the gradient of a line appear directly on the line is useful both when exploring gradient in terms of rise and run, and also when looking at gradients of tangents.

Link to the basic gradient construction is here and link to the graph with a movable tangent is here.

Exploring gradient

A graph's tangents are shown together with their gradients


4. Normal distributions

Really any continuous random variable, or indeed any integration visualisation, is improved by having the area appear on the graph itself. 

Although it's not an interesting problem for normal distributions, finding the median by dragging a point feels much more intuitive when the area is displayed on the graph.

Link to the graph is here.

A normal distribution shown with probabilities

5. Distances between points

This one is probably most useful in a Desmos classroom activity when you can have a graph overlay to generate a locus, but when exploring distances between points it is nice to be have the distance appear on the graph.

Also note you can label a point like A({p},{q}) to see the point name and its coordinates.

Link to the graph is here.
The point A can be moved and the distance from C is updated dynamically

These represent a selection of applications I have either encountered or thought of in the past week since dynamic labels were added. 

I look forward to seeing other examples either on Twitter or in the comments section. Although preferably Twitter because I don't check my blog often enough to see the comments.

Tuesday, January 10, 2017

Drawing vertical tangents when the gradient is undefined.

A common thing that I want to do in Desmos is draw a tangent line to a function that a user can drag around or animate. A simple way I have been using is below:


Problem

There is a slightly annoying problem with this method: If the derivative is undefined at a point then the graph won't display a vertical tangent; it just won't draw a line at all here.



Solution

I recently found a way to test for undefinedness in Desmos. The expression {k = k: 0, 1} will return 1 if k is undefined (or 0 if it's defined). So we can use this technique to graph a separate vertical line at x = a when the value of f'(a) is undefined.

Note that if the gradient is defined, I actually don't want this line to appear at all, which is why I threw in an undefined value (if I just put 0 here then it would graph x = 0 for these values of a).

I am much happier with this version of the graph.

[Update] Minor Technical Problem: Undefined function values

After posting this, I realised that the additional rule will also graph a vertical line when the function itself is undefined at a point. I'm not sure what the best behaviour is for this case, but perhaps it would be nice to have a draggable point along the x-axis with no vertical line.

To implement this, change line 2 from (a, f(a)) to a more complicated conditional

And the vertical tangent line can be set to only appear when the function is defined but the derivative is undefined.


Wednesday, January 4, 2017

Transformations using Draggable Points in Desmos

Activities in Desmos allow students to interact with graphs before answering a question, and this facility allows for a more natural way of asking questions that are quite hard to ask just using pen-and-paper tests.

Imagine a point A is at (2, 1) and another point A' is at (2, 1). What is the transformation?

With draggable points, a student could move a point A around the screen and see the image point A'. After moving the point around, the student could see that the transformation was a reflection in the y-axis.


Now imagine on the next screen, the point A is at (2, 1) and the image A' is again at  (2, 1). But this time the student drags the point around and sees that the transformation was a horizontal translation.


Once a student has engaged with these two screens the original question can be asked, but it seems much less abstract. Imagine a point A is at (2, 1) and another point A' is at (2, 1). What is the transformation?

This question can also be asked in Desmos, and is basically a duplicate of the previous screen where the point A is no longer draggable.


Thinking of a transformation as a function T: R2 → R2 could seem very abstract and hard to visualise (without an ability to see in four dimensions). By allowing the first point to be draggable we can provide a good intuition of what a transformation is.

Also, it is interesting to me that simply by changing between the dynamic versions (Screen 1 and Screen 2) and the static version (Screen 3) changes the answer.

I'm not sure how easily these concepts could be shown using a pen-and-paper quiz (one could include some points A, B, C and their images A', B', C' but this seems a bit messy and not as helpful if we want students to think of it as a function mapping any point in R2 to another point in R2.)

The activity shown in this post is just to show the idea but if it's helpful, you might like to copy and edit it to see how it is implemented. This is the link.




New Blog

Welcome to my blog. I have had blogs before and they never last long, so I don't hold out much hope for this one.

However, I am keen to post a few thoughts about this and that, so I thought this might be better than just tweeting them as they occur to me.

I suppose I should include various disclaimers about the views here are my own and not necessarily endorsed by my employer(s). If anyone has read this far then please assume that the disclaimer has been written correctly and appears wherever anything controversial or wrong is written.