4

How do you draw functions that look like this? I apologize for not providing you with an attempt to solve this problem - I am a second-day user of Latex. Greatly appreciate your help.

\documentclass[paper=a4, fontsize=11pt]{scrartcl} 
\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage{fourier} % Use the Adobe Utopia font for the document - comment this line to return to the LaTeX default
\usepackage[english]{babel} % English language/hyphenation
\usepackage{amsmath,amsfonts,amsthm} % Math packages
\usepackage{sectsty} % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all sections centered, the default font and small caps

\usepackage{fancyhdr} % Custom headers and footers
\usepackage{xfrac}


\begin{document}
\[ F_x(x) = \left\{ 
  \begin{array}{l l}
    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{array} \right.\]
  \\

\end{document}
4
  • You mean you want to plot it? Something like tex.stackexchange.com/questions/132476/…? Commented Aug 29, 2014 at 13:06
  • Yes, this is correct. Plotting a function that has multiple arguments, I guess (?).
    – Olga
    Commented Aug 29, 2014 at 13:12
  • You can use something like pgfplots as the above comment shows but alternatively (and more commonly) you can plot the graph in whatever plotting software you are used to (gnuplot, excel, matlab, ....) and include the resulting image using \includegraphics Commented Aug 29, 2014 at 13:13
  • 2
    But plots produced by matlab and excel are ugly.
    – JPi
    Commented Aug 29, 2014 at 13:21

2 Answers 2

6
\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}
7
  • 1
    Not that I am easily impressed...WOW, the graph looks impressive. I will use it as a template for other similar graphs.
    – Olga
    Commented Aug 29, 2014 at 13:34
  • P.S. How do you make this graph thicker?
    – Olga
    Commented Aug 29, 2014 at 13:40
  • Put very thick, before smooth
    – JPi
    Commented Aug 29, 2014 at 13:44
  • Hey! Thanks again. I tried plotting a different graph by using your template but it didn't work. It was quite strange because I only changed 2 things: x^2 became 2x, and 1-3(1-x)^2 became 6-6x. As you can tell, these are the derivatives. Any suggestions please?
    – Olga
    Commented Aug 29, 2014 at 17:30
  • Hmm. I'll have a look tomorrow.
    – JPi
    Commented Aug 30, 2014 at 0:51
5

The pstricks way. It can be compiled with pdflatex if you launch it with the --enable-write18 (MiKTeX) or --shell-escape (TeXLive, MacTeX) switch:

\documentclass[11pt, a4paper, svgnames, pdf]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{nccmath}
\usepackage{lmodern}

\usepackage{pst-plot}

\begin{document}

\psset{unit=4cm, linewidth = 0.6pt,  ticksize = -2pt 2pt}
\begin{pspicture*}(-1.6,-0.9)(1.9,1.6)
\psaxes{->}(0,0)(-1.6,-0.9)(1.9,1.6)[ $ x $, -135][ $ y $,-135]%
\uput[d](-0.05,0){0}
{\psset{linestyle = dashed,, linecolor = LightSteelBlue}
\psline(0, 0.25)(0.5, 0.25)(0.5, 0)\uput[d](0.5,0){ $ \mfrac{1}{2} $}\uput[l](0, 0.25){ $ \mfrac{1}{4} $}
\psline(0,1)(1,1)(1,0)}
\psset{linewidth = 1.5pt, linecolor =IndianRed ,plotpoints=50,plotstyle=curve, algebraic, labelsep = 0.5em}
\psline(-1.6,0)(0,0)
\psplot{0}{0.5}{x^2}
\psplot{0.5}{1}{1-3*(1-x)^2}
\psline(1,1)(2,1)
\psdots[dotsize = 3pt](0,0)(0.5, 0.25)(1,1)
\end{pspicture*}

\end{document}

enter image description here

5
  • Thanks, the graph is beautiful. I tried it, but the code isn't working for me.
    – Olga
    Commented Aug 29, 2014 at 14:50
  • Do you have any error message? You can compile with pdflatex, although it's based on pstricks (pdf option in \documentclass). I think I forgot you must set the compiler with the --shell-escape switch.
    – Bernard
    Commented Aug 29, 2014 at 15:53
  • That's right, I had an error message. How do you set the compiler with the --shell-escape switch?
    – Olga
    Commented Aug 29, 2014 at 17:31
  • That depends on your editor… Which one do you use?
    – Bernard
    Commented Aug 29, 2014 at 18:22
  • Unfortunately, I don't work on Mac. I can only explain for TeXmaker or TeX studio. There must be a place where calls to compilers are written. I suppose TeXshop has a configuration menu.
    – Bernard
    Commented Aug 29, 2014 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .