Probably the easiest thing to do would be to use TikZ's front end to gnuplot. If you use the raw gnuplot
option you can pass a more complicated script to gnuplot to make it to what you want (rather than just plot a simple function).
I think it's almost impossible to do within TeX itself, which is after all a text processing system and not a computer algebra system. You could use pgfmath
(the math engine within TiKZ) to generate the surface points, and only plot the points where z is sufficiently close to 0. But how would you connect these points?
Another method would be to use the function to write a differential equation and then implement Euler's method to plot a curve. But that probably won't be as pretty as you want—assuming the curve is closed you would have to guess-and-check to make sure you plotted enough to close the curve and not too much to overlap.
There might also be a LuaTeX option but I don't know anything about that.
This particular curve is highly singular so first-stab TeX efforts probably wouldn't be very successful here.
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\begin{document}
\[ F_x(x) =
\begin{cases}
0 & \quad \text{for $x <0$}\\
x^2 & \quad \text{$0\leq x<0.5$}\\
1-3(1-x)^2 & \quad \text{$0.5\leq x<1$}\\
1 & \quad \text{$x\geq 1$}
\end{cases}
\]
\begin{tikzpicture}
\begin{axis}
\addplot[smooth,samples=200,domain=-2:0]{0};
\addplot[smooth,samples=200,domain=0:0.5]{x^2};
\addplot[smooth,samples=200,domain=0.5:1]{1-3*(1-x)^2};
\addplot[smooth,samples=200,domain=1:2]{1};
\end{axis}
\end{tikzpicture}
\end{document}
Edited:
Implementing the change suggested by the OP works fine for me:
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\begin{document}
\[ F_x(x) =
\begin{cases}
0 & \quad \text{for $x <0$}\\
2x & \quad \text{$0\leq x<0.5$}\\
6-6x & \quad \text{$0.5\leq x<1$}\\
1 & \quad \text{$x\geq 1$}
\end{cases}
\]
\begin{tikzpicture}
\begin{axis}
\addplot[smooth,samples=200,domain=-2:0]{0};
\addplot[smooth,samples=200,domain=0:0.5]{2*x};
\addplot[smooth,samples=200,domain=0.5:1]{6-6*x};
\addplot[smooth,samples=200,domain=1:2]{1};
\end{axis}
\end{tikzpicture}
\end{document}
Edited again to create red lines and dots, as per request:
\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{amsmath}
\usepackage{pgfplots}
\begin{document}
\[ F_x(x) =
\begin{cases}
0 & \quad \text{for $x <0$}\\
x^2 & \quad \text{$0\leq x<0.5$}\\
1-3(1-x)^2 & \quad \text{$0.5\leq x<1$}\\
1 & \quad \text{$x\geq 1$}
\end{cases}
\]
\begin{tikzpicture}
\begin{axis}
\addplot[Red,smooth,samples=200,domain=-2:0]{0};
\addplot[Red,smooth,samples=200,domain=0:0.5]{x^2};
\addplot[Red,smooth,samples=200,domain=0.5:1]{1-3*(1-x)^2};
\addplot[Red,smooth,samples=200,domain=1:2]{1};
\fill[Red] (axis cs:0,0) circle(0.5mm) (axis cs:0.5,0.25) circle(0.5mm) (axis cs:1,1) circle(0.5mm);
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
In this answer, I will explain how to make animations in both PDF and GIF format:
Make sure you have installed the following applications and set the PATH system variable to their locations.
gswin32c.exe
andgswin64c.exe
repectively.The first two sections will guide you to create some supporting files that will simplify your workflow. The last section will show how to make use of the supporting files to create animations.
Creating A Multi Purpose Batch File for GIF Animation
Create a batch file named
GifAnimate.bat
as follows. It is recommended to save it in a separate directory such that any other projects can make use of it. Don't forget to set the PATH to its location.Creating A Multi Purpose Batch File and Template for PDF Animation
Create a batch file named
PdfAnimate.bat
as follows. It is recommended to save it in a separate directory such that any other projects can make use of it. Don't forget to set the PATH to its location.Create a template file named
PdfAnimateTemplate.tex
as follows. It is recommended to save it in your local TDS such that any other projects can make use of it.Creating GIF and PDF Animations
Create a LaTeX input file named
test.tex
as follows:First compile it with either LaTeX or XeLaTeX.
With LaTeX is as follows (do one after another):
With XeLaTeX is as follows:
You will have a PDF output named
test.pdf
. It consists of 15 pages.To get the corresponding GIF animation, execute
GifAnimate.bat test 20 250
, we will get the following output:To get the corresponding PDF animation, execute
PdfAnimate.bat test 25 3
, we will get a PDF animation (cannot be shown here).Miscellaneous
Responding to Yiannis Lazarides' comment below. I am lazy to animate Batman as the curve has many discontinuities. Instead, I present another example. Hopefully the following graph is more exciting!
Responding to the given comment
If you want to animate
test.pdf
(consists of 15 pages) in your PDF document, use the following template and compile withpdflatex
.where
<OutputScale>
is a real number representing the scaling factor.<FrameRate>
is an integer representing frames per second.Of course,
<
and>
are not the part of the syntax.