# Using Plot and Piecewise to graph a roller coaster

I am graphing a roller coaster using Piecewise and Plot commands on Mathematica. I have the first 2 Piecewise functions and would like to add a loop to the end of it. Here is my initial graph: This is the command I used for the graph above:

Plot[Piecewise[{{0, 0 < x < 1}, {-x + 1, 1 < x < 2}, {((x - 4)^2 - 5),
2 < x < 5}}], {x, -1, 10}]


I would like to add a piece (just from x=2 to around x=6) of the following loop to the end of the above graph: I used the following command for the above graph:

ParametricPlot[{Cos[x] + x/2, Sin[x]}, {x, 1, 16}]


The loop has to be below the x-axis. I might have the wrong function for this graph, since cosine won't go below x=-1. How would I go about doing this?

Even if it's not a "pretty" loop like the one above, it works. I tried to just add a circle to it, but when I try to graph both the positive and negative functions of a circle, I only get the bottom half.

Plot[Piecewise[{{0, 0 < x < 1}, {-x + 1, 1 < x < 2}, {((x - 4)^2 - 5),
2 < x < 5}, {-4, 5 < x < 6}, {-Sqrt[4 - (x - 8)^2] - 4,
5 < x < 10}, {Sqrt[4 - (x - 8)^2] - 4, 5 < x < 10}}], {x, -1, 10}]

• "below the x-axis" - then you're supposed to shift the $y$-component downward, no? – J. M. is in limbo Sep 1 '15 at 14:35
• You probably should also match the derivatives. – Dr. belisarius Sep 1 '15 at 14:49
• @bel, in fact, if OP wants a smooth "ride" in his coaster, he should be matching up to the second derivative (i.e. a $C^2$ curve)… – J. M. is in limbo Sep 1 '15 at 15:20
• I don't understand what you guys mean by matching the derivatives. – juliodesa Sep 1 '15 at 15:21
• @Guesswhoitis. Given the treatment at x == 2, I guess the public is warned about some engineering faults :) – Dr. belisarius Sep 1 '15 at 15:25

Equating function and first derivatives:

f1[x_] := {x, ((x - 4)^2 - 5)}
f2[x_] := {c + Cos[x + a] + (x + a)/2, b + Sin[x + a]}

s = {Reduce[ Join[
{f1'[] == f2'[]/(f2'[])}], {a, b},
Backsubstitution -> True] /. C -> 0 // ToRules}

kk[n_] := Piecewise[
{
{{x, 0}, 0 < x < 1},
{{x, 1 - x}, 1 < x < 2},
{f1[x], 2 < x < 5},
{f2[x] /. s[[n]], x > 5}}, 0]

ParametricPlot[kk[#], {x, 0, 10}] & /@ {1, 2} Here you have a nice smooth design equating the function, the first and second derivatives :)

f1[x_] := {x, ((x - 4)^2 - 5)}
f2[x_] := {c + d Cos[ x + a] + ( x + a)/2, b + d Sin[x + a]}
s = {Reduce[
Join[
{f1'[] == f2'[]/(f2'[])},
{f1''[] == (f2'[] (f2''[]) - (f2'[]) (f2''[]))/
(f2'[])^3}
], Backsubstitution -> True] /. C -> 0 // ToRules};

kk[n_] := Piecewise[
{
{{x, 0}, 0 < x < 1},
{{x, 1 - x}, 1 < x < 2},
{f1[x], 2 < x < 5},
{f2[x] /. s[[n]], x > 5}}, 0]

ParametricPlot[kk, {x, 0, 20}] • You accepted too soon ... – Dr. belisarius Sep 1 '15 at 15:55