# Tikz: 3D piecewise function

I have the following piecewise function

whose graphic is given by

Question 1. How can we plot the surface corresponding to the middle piece is not drawn appropriately?

Question 2. How can we move the ticks and label of $z$-axis to the other side?

I plot this function in the following way.

\documentclass[a4paper,12pt]{article}

%\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}

\begin{figure}[ht]
\centering
\begin{tikzpicture}[
baseline=(origin),
declare function={
f(\t,\y)=(y<=0)*2*\t+(\t>0)*(\y>\t)*(\y<=\t^2)*2*(1-\y/\t)*\t+(\y>=\t^2)*(-2)*\t;
}
]
\begin{axis}[
width=100mm,
height=60mm,
view={135}{45},
xmin=0,
xmax=1,
xlabel={$t$},
ymin=-1,
ymax=1,
ylabel={$y$},
zmin=-2,
zmax=2,
zlabel={$z$},
zlabel style={rotate=-90},
]
\coordinate (origin) at (axis cs:0,0,0);
\draw[variable=\t,domain=0:1,color=red,thick] plot (axis cs:\t,0,{2*\t});
\draw[variable=\t,domain=0:1,color=red,thick] plot (axis cs:\t,{\t^2},{(-2)*\t});
\end{axis}
\end{tikzpicture}
\caption{Graphic of $f:[0,1]\times[-1,1]\to[-2,2]$.}
\end{figure}

\end{document}

• It seems your plot is piecewise linear. You may search patch in the manual. – Symbol 1 Aug 17 '17 at 13:18
• My problem is the parametric parsel of the domain. I could not manage it. I could not even find a closer example. – bkarpuz Aug 18 '17 at 20:00
• I personally do not believe that pgfplots can handle piecewise domain properly. The only way seems to be dividing one \addplot into three \addplots and parameterizing each piece extremely carefully. Or using tons of samples to wipeout the aliasing, which wastes a lot of time. – Symbol 1 Aug 18 '17 at 20:39

I have obtained the following approximate solution.

\documentclass[a4paper,12pt]{article}

\usepackage{pgfplots}

\begin{document}

\begin{figure}[ht]
\centering
\begin{tikzpicture}[baseline=(origin)]
\begin{axis}[
width=100mm,
height=60mm,
view={135}{45},
xmin=0,
xmax=1,
xlabel={$t$},
ymin=-1,
ymax=1,
ylabel={$y$},
zmin=-2,
zmax=2,
zlabel={$z$},
zlabel style={rotate=-90},
]
\coordinate (origin) at (axis cs:0,0,0);
\fill[color=blue] (axis cs:0,0,0)
-- (axis cs:0,-1,0)
-- (axis cs:1,-1,2)
-- (axis cs:1,0,2)
-- cycle;
\fill[color=blue] (axis cs:0,0,0)
\foreach \pt in {0.1,0.2,...,1}{ -- (axis cs:\pt,0,{2*\pt})}
\foreach \pt in {1,0.9,...,0}{ -- (axis cs:\pt,{\pt^2},{(-2)*\pt})};
\fill[color=blue] (axis cs:0,0,0) -- (axis cs:0,1,0) -- (axis cs:1,1,-2)
\foreach \pt in {1,0.9,...,0}{ -- (axis cs:\pt,{\pt^2},{(-2)*\pt})};
\draw[variable=\t,domain=0:1,color=red,thick] plot (axis cs:\t,0,{2*\t});
\draw[variable=\t,domain=0:1,color=red,thick] plot (axis cs:\t,{\t^2},{(-2)*\t});
\end{axis}
\end{tikzpicture}
\caption{Graphic of $f:[0,1]\times[-1,1]\to[-2,2]$.}
\end{figure}

\end{document}