0
$\begingroup$

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:

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:

loop

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}]
$\endgroup$
9
  • $\begingroup$ "below the x-axis" - then you're supposed to shift the $y$-component downward, no? $\endgroup$ Sep 1, 2015 at 14:35
  • $\begingroup$ You probably should also match the derivatives. $\endgroup$ Sep 1, 2015 at 14:49
  • $\begingroup$ @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)… $\endgroup$ Sep 1, 2015 at 15:20
  • $\begingroup$ I don't understand what you guys mean by matching the derivatives. $\endgroup$
    – juliodesa
    Sep 1, 2015 at 15:21
  • 3
    $\begingroup$ @Guesswhoitis. Given the treatment at x == 2, I guess the public is warned about some engineering faults :) $\endgroup$ Sep 1, 2015 at 15:25

1 Answer 1

2
$\begingroup$

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[
      Thread[f1[5] == f2[5]], 
            {f1'[5][[2]] == f2'[5][[2]]/(f2'[5][[1]])}], {a, b}, 
             Backsubstitution -> True] /. C[1] -> 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}

Mathematica graphics


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[
       Thread[f1[5] == f2[5]],
       {f1'[5][[2]] == f2'[5][[2]]/(f2'[5][[1]])},
       {f1''[5][[2]] == (f2'[5][[1]] (f2''[5][[2]]) - (f2'[5][[2]]) (f2''[5][[1]]))/
                        (f2'[5][[1]])^3}
       ], Backsubstitution -> True] /. C[1] -> 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[2], {x, 0, 20}]

Mathematica graphics

$\endgroup$
1
  • 2
    $\begingroup$ You accepted too soon ... $\endgroup$ Sep 1, 2015 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.