5

Using Tikz / pgfplots, how can you plot a function with two variables, such as:
|x| + |y| <= 1 / sqrt(2)?

If the function only had one input,X, it is easy to plot:

\begin{figure}[!hb]
\centering
\begin{tikzpicture}[scale=1.0]
\begin{axis}
    \addplot[domain=-10000:10000, blue, ultra thick] {abs(x)};
    \end{axis}
\end{tikzpicture}
\caption{Plot of $\abs(x)$}
\end{figure}

However, the following produces an error

\addplot[domain=-10000:10000, blue, ultra thick] {abs(x) + abs(y) <= 1 / sqrt(2)};
5

You can use a parametrization (well, two in this case):

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
domain=-1/sqrt(2):1/sqrt(2),
samples=400,
xmin=-1.5,
xmax=1.5,
ymin=-1.5,
ymax=1.5,
axis on top=true
]
\addplot+[mark=none,cyan,fill] ({x},{max(1/sqrt(2)-abs(x),0)}) \closedcycle;
\addplot+[mark=none,cyan,fill] ({x},{min(abs(x)-1/sqrt(2),0)}) \closedcycle;
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

3

With PSTricks' implicit plot macro:

\psplotImp[<options>](-2,-2)(2,2){abs(x)+abs(y)-const}

The Minimal Working Example:

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-func}
\begin{document}
\begin{pspicture}(-2,-2)(2,2)
    \pstVerb{/const 1 2 sqrt div def}%
    \pspolygon*[linecolor=cyan](!const 0)(!0 const)(!const neg 0)(!0 const neg)
    \psplotImp[linecolor=red,algebraic,stepFactor=0.1](-2,-2)(2,2){abs(x)+abs(y)-const}
    \psaxes{->}(0,0)(-2,-2)(2,2)[$x$,0][$y$,90]
\end{pspicture}
\end{document}

enter image description here

Algorithm

The following animation discloses the confidential algorithm used (by \psplotImp) behind the scene.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-func}
\begin{document}
\multido{\r=1.5+-0.1}{15}{%
\begin{pspicture}(-1,-1)(1,1)
  \pstVerb{/const 1 2 sqrt div def}%
  \psplotImp[linecolor=red,algebraic,stepFactor=\r](-1,-1)(1,1){abs(x)+abs(y)-const}
\end{pspicture}}
\end{document}

enter image description here

3

enter image description here

MWE with Asymptote, using a contour module.

Some comments: the boundary is constructed as guide g=(contour(f,(-1,-1),(1,1), new real[]{1/sqrt(2)}))[0][0];, where expression new real[]{1/sqrt(2)} stands for an in-line array of level values, (in this case just one, 1/sqrt(2)). The trailing [0][0] means that in general there are a matrix of curves returned, where the first index corresponds to the index in the levels array, and the second index counts the curves that correspond to the particular level value.

% imp.tex :
\documentclass{article}
\usepackage[inline]{asymptote}
\usepackage{lmodern}
\begin{document}
\begin{figure}
\begin{asy}
size(300);
import graph;
import contour;
defaultpen(fontsize(10pt));
pen bgColor=paleyellow;
real f(pair v){return abs(v.x)+abs(v.y);};
guide g=(contour(f,(-1,-1),(1,1), new real[]{1/sqrt(2)}))[0][0];
pen linePen=deepblue+1bp;
pen fillPen=deepblue+opacity(0.3);
pen xyPen=darkblue+0.4bp;
filldraw(g,fillPen,linePen);
real du=0.02;
xaxis(-0.8-du,0.8+du,xyPen, LeftTicks(OmitTick(0),Step=0.2,step=0.1),above=true);
yaxis(-0.8-du,0.8+du,xyPen,RightTicks(OmitTick(0),Step=0.2,step=0.1),above=true);
shipout(bbox(Fill(bgColor)));
\end{asy}
\end{figure}
\end{document}
%% Process:
%
% pdflatex imp.tex 
% asy -f pdf imp-*.asy     
% pdflatex imp.tex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.