# Draw somewhat random function

I am trying to reconstruct this image here

I am not very happy with the code, as it uses hacking and adding white boxes and alike. My problem is now how do I draw the somewhat arbitary function in the figure? I tried to do a polynomial fit, but that did not work. I got dimensions to large, would a spline solution work better?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
xmin=-0.1,xmax=5,
xlabel={z},
ymin=0,ymax=4.5,
xtick={2.5,4},
xticklabels={$v_1$,$v_2$},
ytick={2.5,3.5},
yticklabels={$u_1$,$u_2$},
xlabel={$u$},
ylabel={$v$},
axis lines=middle]
\draw [fill=gray!40!white,thick,dashed] (axis cs:0,0) rectangle (axis cs:4,3.5);
\draw [color=black,fill=white,thick,dashed] (axis cs:0,0) rectangle (axis cs:2.5,2);
\draw [fill=black] (axis cs:0,0) rectangle (axis cs:0.01,4.45);
\addplot+[black,thick,domain=0:5,no marks] {0};
%
\node at (axis cs:2.5,0)    [anchor=south west] {$A$};
\node at (axis cs:4,0)      [anchor=south west] {$A'$};
\node at (axis cs:2.8,2.2)  [anchor=north]      {$B$};
\node at (axis cs:4.2,4)   [anchor=north]       {$B'$};
\node at (axis cs:0.25,2.5) [anchor=north]      {$C$};
\node at (axis cs:0.25,4)   [anchor=north]      {$C'$};
\node at (axis cs:0,0)     [anchor=south west]  {$O$};
%
\end{axis}
\end{tikzpicture}
\end{document}

-

## 4 Answers

Using hobby package, you can draw random curve with specific coordinates.

    \documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns,hobby}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
xmin=-0.1,xmax=5,
xlabel={z},
ymin=0,ymax=4.5,
xtick={2.5,4},
xticklabels={$v_1$,$v_2$},
ytick={2.5,3.5},
yticklabels={$u_1$,$u_2$},
xlabel={$u$},
ylabel={$v$},
axis lines=middle]
\draw [fill=gray!40!white,thick,dashed] (axis cs:0,0) rectangle (axis cs:4,3.5);
\draw [color=black,fill=white,thick,dashed] (axis cs:0,0) rectangle (axis cs:2.5,2);
\draw [fill=black] (axis cs:0,0) rectangle (axis cs:0.01,4.45);
\addplot+[black,thick,domain=0:5,no marks] {0};
%
\node  at (axis cs:2.5,0)    [anchor=south west] {$A$};
\node  at (axis cs:4,0)      [anchor=south west] {$A'$};
\node  at (axis cs:2.8,2.2)  [anchor=north]      {$B$};
\node  at (axis cs:4.2,4)   [anchor=north]       {$B'$};
\node  at (axis cs:0.25,2.5) [anchor=north]      {$C$};
\node  at (axis cs:0.25,4)   [anchor=north]      {$C'$};
\node at (axis cs:0,0)     [anchor=south west]  {$O$};
%
\end{axis}
%Hobby package
\draw [thick] (0.15 ,0.1) to [ curve through ={(1.5,1.5)  . . (3.3,2.25) . . (4.5,3)  }] (5.5,4.5);% curve
\end{tikzpicture}
\end{document}


Output:

You should arrange desired points.

-

Metapost lets you specify the direction of a path as it passes through a point. This lets you draw a random looking function line very naturally here, while ensuring that it actually passes through the critical points.

prologues := 3;
outputtemplate := "random_function.eps";

beginfig(1);
u=.6mm;
z1 = (55u,34u);
z2 = (89u,55u);
z3 = (100u,75u);
z4 = (115u,95u);

path L; L = (x1,0) -- (x2,0) -- z2
-- (0,y2) -- (0,y1) -- z1
-- cycle;

fill L withcolor .8 white; draw L dashed evenly;

theta = 76;
draw origin {dir theta} ..
z1     {dir theta} ..
z2     {dir theta} ..
z3 withcolor .675 red withpen pencircle scaled 0.6;

drawarrow origin -- (x4,0) withpen pencircle scaled 0.8;
drawarrow origin -- (0,y4) withpen pencircle scaled 0.8;

label.llft(btex $O$ etex,origin);
label.bot(btex $v_1$ etex, (x1,0)); label.urt(btex $A$ etex, (x1,0));
label.bot(btex $v_2$ etex, (x2,0)); label.urt(btex $A'$ etex, (x2,0));
label.bot(btex $v$   etex, (x4,0));
label.lft(btex $u_1$ etex, (0,y1)); label.urt(btex $C$  etex, (0,y1));
label.lft(btex $u_2$ etex, (0,y2)); label.urt(btex $C'$ etex, (0,y2));
label.lft(btex $u$   etex, (0,y4));
label.rt(btex $B$ etex, z1);
label.rt(btex $B'$ etex, z2);
label.urt(btex $u(v)$ etex, z3);

endfig;
end.


-
Note that this is also possible with the Hobby package (using the same algorithm, actually). –  Charles Staats Feb 13 '14 at 16:31
+1 for Metapost! Looks very nice :) –  Sean Allred Feb 13 '14 at 17:39

Use of Bezier curve, this alternative yields the following nonlinear curve. As you can see from the overall curve, there is a regularity that can be derived. The low-left and upper-right corner curves are similar, both are within an invisible rectangle. The starting and ending points are clearly identified. The control points are the remaining corners. So these 4 corners determine these particular curves.

The syntax for Bezier curve is

(x1,y1) .. controls (x2,y2) and (x3,y3) .. (x4,y4);


Code

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns,calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
xmin=-0.1,xmax=5,
xlabel={z},
ymin=0,ymax=4.5,
xtick={2.5,4},
xticklabels={$v_1$,$v_2$},
ytick={2.5,3.5},
yticklabels={$u_1$,$u_2$},
xlabel={$u$},
ylabel={$v$},
axis lines=middle]
\draw [fill=gray!40!white,thick,dashed] (axis cs:0,0) rectangle (axis cs:4,3.5);
\draw [color=black,fill=white,thick,dashed] (axis cs:0,0) rectangle (axis cs:2.5,2);
\draw [fill=black] (axis cs:0,0) rectangle (axis cs:0.01,4.45);
\addplot+[black,thick,domain=0:5,no marks] {0};
%
\node at (axis cs:2.5,0)    [anchor=south west] {$A$};
\node at (axis cs:4,0)      [anchor=south west] {$A'$};
\node at (axis cs:2.8,2.2)  [anchor=north]      {$B$};
\node at (axis cs:4.2,4)    [anchor=north]       {$B'$};
\node at (axis cs:0.25,2.5) [anchor=north]      {$C$};
\node at (axis cs:0.25,4)   [anchor=north]      {$C'$};
\node at (axis cs:0,0)      [anchor=south west]  {$O$};
%
\draw (axis cs:0,0) .. controls (axis cs:0,2.5) and (axis cs:2.5,0) .. (axis cs:2.5,2.5) .. controls (axis cs:2.5,4)  and (axis cs:4,2.5) .. (axis cs:4.2,4);
\end{axis}
\end{tikzpicture}
\end{document}

-

With PSTricks just for fun. I use a real function called Weirdstress function as follows.

\def\f{x+sin(3*x)/3+sin(x^2/2)/2}


It is differentiable everywhere.

The complete code is given as follows.

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot,pst-eucl}

\psset{saveNodeCoors,PointSymbol=none}

\def\f{x+sin(3*x)/3+sin(x^2/2)/2}% Weirdstress function copyrighted by me

\begin{document}
\begin{pspicture}(-.5,-.5)(5.5,5.5)
\pstGeonode[PosAngle={-135,-30,-30,45}]{O}(*2 {\f}){B}(*3.8 {\f}){B'}(B|0,0){A}(B'|0,0){A'}(0,0|B){C}(0,0|B'){C'}
\pstGeonode[PosAngle={-90,-90,180}](A){v_1}(A'){v_2}(C){u_1}(C'){u_2}
\pspolygon*[linecolor=gray,opacity=.5](C')(C)(B)(A)(A')(B')
\psplot[algebraic,plotpoints=100,linecolor=blue]{0}{4.5}{\f}
\psaxes[labels=none,ticks=none]{->}(0,0)(-.5,-.5)(5,5)[$v$,0][$u$,90]
\uput[90](*4.5 {\f}){$u(v)$}
\psCoordinates[linestyle=dashed](B)
\psCoordinates[linestyle=dashed](B')
\end{pspicture}
\end{document}


-