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

?
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

?
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}
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)));
pgfplots, see e.g. method 2 in https://tex.stackexchange.com/a/121799/ You can set thedomainfor individual\addplots, not just for theaxis(as in that example). – Torbjørn T. Nov 05 '17 at 09:56domain=<startx>:<endx>aftermark=nonein the\addplotoptions. – Torbjørn T. Nov 05 '17 at 10:37\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\addplotgo 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 thedomainsetting in the question I linked to, so that you would understand that. Sorry. – Torbjørn T. Nov 05 '17 at 11:42