CoCalc Shared FilesMTH 333 Material / Project Roller Coaster / Part I / Building a Roller Coaster.ipynbOpen in CoCalc with one click!
Author: Austin Powers
Views : 35

Buidling a Roller Coaster

When constructing your roller coaster it may be useful to design individual pieces and then connect those pieces at endpoints. For instance, suppose you want to match the end of a semicircle of radius 3 going from the point (3,0,0)(3,0,0) to the point (0,0,3)(0,0,3) described by the vector valued function r1(t)=3cos(t),0,3sin(t)0tπ2\mathbf{r}_1(t) = \langle 3\cos(t), 0, 3\sin(t) \rangle \qquad 0 \leq t \leq \frac{\pi}{2} with a straight line segment of length 4. We repesent the straight line segment using r2(t)=t,0,00t4.\mathbf{r}_2(t) = \langle t, 0, 0 \rangle \qquad 0 \leq t \leq 4.

Let's begin by examining a picture of our two pieces

In [1]:
var('t') # declare a variable r1 = vector([3*cos(t),0,3*sin(t)]) # define our first vector valued function r2 = vector([t,0,0]) # define the 2nd vector valued function above plot1 = parametric_plot3d(r1, (t,0,pi/2), color='red', thickness=5) plot2 = parametric_plot3d(r2, (t,0,4), color='green', thickness=5) show(plot1+plot2) # show the plot together

The above does not quite match up in the way we desire. To move the green straight line segment to the end of the semicircle, we need to shift the starting point over from r2(0)=0,0,0\mathbf{r}_2(0)= \langle 0,0,0 \rangle to r1(0)=3,0,0\mathbf{r}_1(0) = \langle 3,0,0 \rangle. This can be done easily by adding the a vector representing the change from 0,0,0\langle 0,0,0 \rangle to 3,0,0\langle 3,0,0 \rangle. This results in a change in r2\mathbf{r}_2 to r2(t)+3,0,0.\mathbf{r}_2(t) + \langle 3,0,0 \rangle. Observe how this changes our picture.

In [2]:
r2 = r2 + vector([3,0,0]) # shift over r2 plot2 = parametric_plot3d(r2, (t,0,4), color='green', thickness=5) show(plot1+plot2) # show the plot together

Saying r2=r2+3,0,0\mathbf{r}_2 = \mathbf{r}_2 + \langle 3, 0, 0 \rangle does not make much sense mathematically. But, this is common practice in computer science, so one needs to understand the meaning. When == is used in computing sciene, the typical interpretation is to take the right-hand-side and assign the value to the variable on the left-hand-side. In this particular instance, the right-hand-side is the "old" r2\mathbf{r}_2 given by t,0,0\langle t,0,0 \rangle added to the vector 3,0,0\langle 3,0,0 \rangle to produce the new function t+3,0,0\langle t+3, 0,0 \rangle. This function is now assigned to the variable r2\mathbf{r}_2 and from here on r2\mathbf{r}_2 with represent the vector valued function t+3,0,0\langle t+3, 0,0 \rangle (unless it is changed again).

To find the length of the roller coaster we make use of the standard formula for arc length. Recall the distance traveled by an object on a path r\mathbf{r} as tt goes from aa to bb is given by abr(t)dt.\int_a^b \Vert \mathbf{r}'(t) \Vert \, dt. In the above example we use the above equation on each piece of the curve. The following sage commands are helpful

  • diff - used to take the derivative of a function

  • norm - used to find the magnitude of a vector

  • numerical_integral - used to perform a definite integral and get a decimal answer.

In [3]:
# we find the length of the red and green parts of the above picture # numerical integral returns two numbers, the integral approximation and the popential error r1_length = numerical_integral(norm(diff(r1,t)),0,pi/2) r2_length = numerical_integral(norm(diff(r2,t)),0,4) print "The length the red curve is", r1_length, "and the green curve is", r2_length # we only need the first number in the tuples total_length = r1_length[0] + r2_length[0] print "The total length is", total_length
The length the red curve is (4.71238898038469, 5.2318027470129476e-14) and the green curve is (4.0, 0.0) The total length is 8.71238898038

Our path is currently 8.71 units. Suppose we would like to lengthen the path to be exactly 100 units. Since we are using vector valued functions this can be achieved easily. This is due to the fact that a vectors can be scaled to any length by multiplying then by an appropriate constant. Here out path is currently about 8.71 units and we wish it to be 100 units. Therefore, we multiply by the scalar c=100/8.71.c = 100/8.71. Thus, the vector valued functions given by cr1andcr2c\mathbf{r}_1 \qquad \text{and} \qquad c\mathbf{r}_2 should have the desired length.

Observe the following

In [4]:
# redefine our function c = 100/total_length r1 = c*r1 r2 = c*r2 # recreate the plots plot1 = parametric_plot3d(r1, (t,0,pi/2), color='red', thickness=5) plot2 = parametric_plot3d(r2, (t,0,4), color='green', thickness=5) show(plot1+plot2) # show the plot together # make sure the new length is 100 as we desire r1_length = numerical_integral(norm(diff(r1,t)),0,pi/2) r2_length = numerical_integral(norm(diff(r2,t)),0,4) # we only need the first number in the tuples total_length = r1_length[0] + r2_length[0] print "The total length is", total_length
The total length is 100.0

Unfortunately, scaling the vectors has caused them not to meet up at the endpoints as we desire. Luckily, we already know how to fix this.