答案1
以下是第二个示例:
\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}
答案2
Asymptote
MWE
绘制分段函数的另一种方法:
// 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)));