\begin{align*}
P(x) =
\begin{cases}
\frac{\frac{1}{2}x^{2}}{4} & 0<x<2 \\
\left(-\frac{\frac{1}{2}}{2}\left(\frac{x^{2}}{2}-4x\right)\right)-1 & 2<x<4\\
\end{cases}
\end{align*}
\item[(c)] Graph $P(x)$.
\begin{tikzpicture}
\begin{axis}[
legend pos=north west,
axis x line=middle,
axis y line=middle,
grid = major,
width=8cm,
height=6cm,
grid style={dashed, gray!30},
xmin=0, % start the diagram at this x-coordinate
xmax=4, % end the diagram at this x-coordinate
ymin= 0, % start the diagram at this y-coordinate
ymax= 1, % end the diagram at this y-coordinate
y label style={at={(axis description cs:-0.2,0.5)},rotate=90,anchor=south},
ylabel=$P(x)$,
tick align=outside,
minor tick num=-3,
enlargelimits=false,
tension=0.08]
\addplot[smooth, black, domain=0:2] {((1/2)*((x)^2))/4};
\addplot[smooth, black, domain=2:4] {((-.25)*(((x)^2)/2)-4(x))-1};
\end{axis}
\end{tikzpicture}
答案1
您在写第二个等式时犯了几个错误。首先, 中没有隐式乘法\pgfmath
,因此您需要写4*x
,而不是4x
。
其次,你写了太多的括号,以至于它们配对错误:
代码:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[]
\begin{axis}[
legend pos=north west,
axis x line=middle,
axis y line=middle,
grid = major,
width=8cm,
height=6cm,
grid style={dashed, gray!30},
xmin=0, % start the diagram at this x-coordinate
xmax=4, % end the diagram at this x-coordinate
ymin= 0, % start the diagram at this y-coordinate
ymax= 1, % end the diagram at this y-coordinate
y label style={at={(axis description cs:-0.2,0.5)},rotate=90,anchor=south},
ylabel=$P(x)$,
tick align=outside,
minor tick num=-3,
enlargelimits=false,
tension=0.08]
\addplot[black, domain=0:2] {1/8*(x)^2};
\addplot[blue, domain=2:4] {-1/4*((x)^2/2 -4*x)-1};
\end{axis}
\end{tikzpicture}
\end{document}
完成您需要的操作(请注意,这比您的代码更简洁,而且可编译……以供您下次发布问题时使用!)
答案2
解决方案是重现您问题中显示的图像,并使用更正和稍微优化的代码:
\documentclass[varwidth, border=3.141592]{standalone}
\usepackage{mathtools}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\[
P(x) = \begin{dcases}
\dfrac{\frac{1}{2}x^{2}}{4} & 0<x<2 \\
-\dfrac{\frac{1}{2}}{2}\biggl(\dfrac{x^{2}}{2} - 4x\biggr) -1
& 2<x<4
\end{dcases}
\]
\begin{tikzpicture}
\begin{axis}[
height=6cm, width=8cm,
axis lines = left,
legend pos = north west,
grid,
grid style={dashed, gray!30},
ylabel=$P(x)$,
tick align=outside,
minor tick num=1,
enlargelimits=false,
tension=0.08,
no marks,
]
\addplot +[thick, domain=0:2] {1/8*(x)^2};
\addplot +[thick, domain=2:4] {-1/4*((x)^2/2-4*x)-1};
\legend{blue, red}
\end{axis}
\end{tikzpicture}
\end{document}