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) to the point (0,0,3) described by the vector valued function
with a straight line segment of length 4. We repesent the straight line segment using
Let's begin by examining a picture of our two pieces
var('t')# declare a variabler1=vector([3*cos(t),0,3*sin(t)])# define our first vector valued functionr2=vector([t,0,0])# define the 2nd vector valued function aboveplot1=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⟩ to r1(0)=⟨3,0,0⟩. This can be done easily by adding the a vector representing the change from ⟨0,0,0⟩ to ⟨3,0,0⟩. This results in a change in r2 to
Observe how this changes our picture.
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⟩ 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 given by ⟨t,0,0⟩ added to the vector ⟨3,0,0⟩ to produce the new function ⟨t+3,0,0⟩. This function is now assigned to the variable r2 and from here on r2 with represent the vector valued function ⟨t+3,0,0⟩ (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 as t goes from a to b is given by
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.
# 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 errorr1_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 tuplestotal_length=r1_length+r2_lengthprint"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
Thus, the vector valued functions given by
should have the desired length.
Observe the following
# redefine our functionc=100/total_lengthr1=c*r1r2=c*r2# recreate the plotsplot1=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 desirer1_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 tuplestotal_length=r1_length+r2_lengthprint"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.