0

How can I plot with tikz on the same figure the graph of two functions which are defined on different subintervals ?

For example, which would be the code for plotting the functions enter image description here

?

Cris
  • 1,189
  • 1
    Look at pgfplots, see e.g. method 2 in https://tex.stackexchange.com/a/121799/ You can set the domain for individual \addplots, not just for the axis (as in that example). – Torbjørn T. Nov 05 '17 at 09:56
  • Thank you for the answer, Torbjorn, but I still I don't know how exactly to set the domains. – Cris Nov 05 '17 at 10:29
  • 1
    Add domain=<startx>:<endx> after mark=none in the \addplot options. – Torbjørn T. Nov 05 '17 at 10:37
  • Trying to plot f_2, I added as follows:

    \addplot+[mark=none] domain=<0>:<4> {x^2-2*x+1}; \addplot+[mark=none] domain=<4>:<10> {x^2-7};

    and nothing than an error... Package pgfplots Error: Sorry, the supplied plot command is unknown or unsupported by pgfplots!

    – Cris Nov 05 '17 at 11:34
  • You misunderstood two things/I was unclear about two things: 1) all the plot settings for an \addplot go within the square brackets, separated by commas, e.g. \addplot [mark=none,domain=0:4]. 2) The <> should not be included. It's common when demonstrating syntax to write <description of something> as a placeholder, and then the <> are not actually part of the syntax. I assumed you had seen the domain setting in the question I linked to, so that you would understand that. Sorry. – Torbjørn T. Nov 05 '17 at 11:42

2 Answers2

3

Here is an example for the second one:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[no markers]
        \addplot [blue,domain=0:4] {x^2-2*x+1};
        \addplot [blue,domain=4:10] {x^2-7};
    \end{axis}
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688
  • @Cris, you should vote this unswer (by clicking on up-pen at top left side of answer) or even accept it (by clicking check mark) ... – Zarko Nov 05 '17 at 13:50
  • I've voted. Sorry, I am new on the forum. – Cris Nov 05 '17 at 18:20
2

An alternative Asymptote MWE to plot piecewise functions:

// piecewiseplot.asy
//
// run asy piecewiseplot.asy
//
// to get piecewiseplot.pdf

settings.tex="pdflatex";
import graph;
size(9cm,6cm,IgnoreAspect);
import fontsize;defaultpen(fontsize(9pt));
texpreamble("\usepackage{lmodern}");

pen linePen=darkblue+0.9bp;
pen line2Pen=orange+0.9bp;

real xmin=0,xmax=5;
real ymin=0,ymax=20;

xaxis(xmin,xmax,RightTicks(Step=1,step=0.5));
yaxis(ymin,ymax,LeftTicks (Step=5,step=2.5)    );

real f1a(real x){ return exp(x);}
real f1b(real x){ return exp(1)*log(x);}
real f2a(real x){ return x^2-2x+1;}
real f2b(real x){ return x^2-7;}

real f1(real x){ return (x<1)? f1a(x): f1b(x);}
real f2(real x){ return (x<4)? f2a(x): f2b(x);}
guide gf1a=graph(f1a,xmin,1,500);
guide gf1b=graph(f1b,1,xmax,500);

draw(graph(f2,xmin,xmax,500),line2Pen);

draw(gf1a,linePen);
draw(gf1b,linePen);

dot((1,f1a(1)),UnFill);
dot((1,f1b(1)));

label("$f_1(x)$",(4,f1(4)),plain.N);
label("$f_2(x)$",(5,f2(5)),plain.W);

shipout(bbox(Fill(paleyellow)));

enter image description here

g.kov
  • 21,864
  • 1
  • 58
  • 95