8

Anyone have ay idea how can I draw this figure enter image description here

using Tikz or PgfPlots? For one dimension I can bur, for two I have no idea. I would like to provide a function $z=f(x,y)$, the ranges for $x; (a\le x\le b)$ and $y; (c\le y\le d)$ and the number of divisions $m$ and $n$ in each interval. Many thanks.

14

Probably it is better to use the cube* plot mark from pgfplots, but for simple enough functions the following may do. You can define a function, the number of steps, and the plot range.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\tikzset{pics/3d bar/.style={code={%
 \tikzset{3d bar/.cd,#1}
 \path[3d bar/x face] (\mydx/2,\mydy/2,0) -- (\mydx/2,\mydy/2,\myh)
    -- (-\mydx/2,\mydy/2,\myh) -- (-\mydx/2,\mydy/2,0) -- cycle;
 \path[3d bar/y face] (\mydx/2,\mydy/2,0) -- (\mydx/2,\mydy/2,\myh)
    -- (\mydx/2,-\mydy/2,\myh) -- (\mydx/2,-\mydy/2,0) -- cycle;
 \path[3d bar/z face] (\mydx/2,\mydy/2,\myh) -- (-\mydx/2,\mydy/2,\myh)
    -- (-\mydx/2,-\mydy/2,\myh) -- (\mydx/2,-\mydy/2,\myh) -- cycle;
    }},3d bar/.cd,dx/.store in=\mydx,dx=1,dy/.store in=\mydy,dy=1,
        h/.store in=\myh,h=1,x face/.style={draw=blue!50,fill=cyan!20},
        y face/.style={draw=blue!50,fill=cyan!50},
        z face/.style={draw=blue!50,fill=cyan!30}}
\begin{document}
\tdplotsetmaincoords{70}{110}%
\begin{tikzpicture}[tdplot_main_coords]
 \begin{scope}[declare function={f(\x,\y)=1+3*exp(-\x/5-\y/4);% function
    n=5;% steps
    xmin=0;xmax=5;ymin=0;ymax=5;}]
  \pgfmathtruncatemacro{\myn}{n}
  \tikzset{3d bar/dx/.evaluated={(xmax-xmin)/n},
     3d bar/dy/.evaluated={(ymax-ymin)/n}}
  \foreach \i in {1,...,\myn}
   {\foreach \j in {1,...,\myn}
   {\pgfmathsetmacro{\myx}{xmin+(\i-0.5)*(xmax-xmin)/\myn}
    \pgfmathsetmacro{\myy}{ymin+(\j-0.5)*(ymax-ymin)/\myn}
    \pgfmathsetmacro{\myf}{f(\myx,\myy)}
    \path (\myx,\myy,0) pic{3d bar={h=\myf}};}}  
 \end{scope}
 %
 \begin{scope}[xshift=7cm,
     declare function={f(\x,\y)=0.5+3*exp(-\x/5-\y/4);% function
     n=20;% steps
     xmin=0;xmax=5;ymin=0;ymax=5;}]
  \pgfmathtruncatemacro{\myn}{n}
  \tikzset{3d bar/dx/.evaluated={(xmax-xmin)/n},
     3d bar/dy/.evaluated={(ymax-ymin)/n}}
  \foreach \i in {1,...,\myn}
   {\foreach \j in {1,...,\myn}
   {\pgfmathsetmacro{\myx}{xmin+(\i-0.5)*(xmax-xmin)/\myn}
    \pgfmathsetmacro{\myy}{ymin+(\j-0.5)*(ymax-ymin)/\myn}
    \pgfmathsetmacro{\myf}{f(\myx,\myy)}
    \path (\myx,\myy,0) pic{3d bar={h=\myf}};}}  
 \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

4
  • Wonderfull!!! Many Thanks! – Angelo Aliano Filho Apr 17 at 20:59
  • Please: provide the code where I plot the function f(x,y) together (above the Riemman sum). – Angelo Aliano Filho Apr 17 at 21:14
  • 1
    To me the output and functionality look very similar to what is asked in the question. I recommend to ask a separate, clear question if you wish to add another ingredient. – user240002 Apr 17 at 22:04
2

If I may add a tiny cherry (a cat guru taught me once) on the beautiful answer by user240002...

To avoid using (annoying IMHO, \pgfmathsetmacro), include the variables to evaluate directly in the loop.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\tikzset{pics/3d bar/.style={code={%
 \tikzset{3d bar/.cd,#1}
 \path[3d bar/x face] (\mydx/2,\mydy/2,0) -- (\mydx/2,\mydy/2,\myh)
    -- (-\mydx/2,\mydy/2,\myh) -- (-\mydx/2,\mydy/2,0) -- cycle;
 \path[3d bar/y face] (\mydx/2,\mydy/2,0) -- (\mydx/2,\mydy/2,\myh)
    -- (\mydx/2,-\mydy/2,\myh) -- (\mydx/2,-\mydy/2,0) -- cycle;
 \path[3d bar/z face] (\mydx/2,\mydy/2,\myh) -- (-\mydx/2,\mydy/2,\myh)
    -- (-\mydx/2,-\mydy/2,\myh) -- (\mydx/2,-\mydy/2,\myh) -- cycle;
    }},3d bar/.cd,dx/.store in=\mydx,dx=1,dy/.store in=\mydy,dy=1,
        h/.store in=\myh,h=1,x face/.style={draw=blue!50,fill=cyan!20},
        y face/.style={draw=blue!50,fill=cyan!50},
        z face/.style={draw=blue!50,fill=cyan!30}}
\begin{document}
\tdplotsetmaincoords{70}{110}%
\begin{tikzpicture}[tdplot_main_coords]
    \begin{scope}[declare function={f(\x,\y)=1+3*exp(-\x/5-\y/4);% function
                n=5;% steps
            xmin=0;xmax=5;ymin=0;ymax=5;}]
        \pgfmathtruncatemacro{\myn}{n}
        \tikzset{3d bar/dx/.evaluated={(xmax-xmin)/n},
            3d bar/dy/.evaluated={(ymax-ymin)/n}}
          
        \foreach \i in {1,...,\myn}
        {% here
            \foreach [evaluate ={
                    \myx = xmin+(\i-0.5)*(xmax-xmin)/\myn ;
                    \myy = ymin+(\j-0.5)*(ymax-ymin)/\myn ;
                    \myf = f(\myx,\myy) ;
                }] \j in {0,...,\myn}
            {\path (\myx,\myy,0) pic{3d bar={h=\myf}};}
        }  
    \end{scope}
    %
    \begin{scope}[xshift=7cm,
            declare function={f(\x,\y)=0.5+3*exp(-\x/5-\y/4);% function
                n=20;% steps
                xmin=0;xmax=5;ymin=0;ymax=5;}]
            \pgfmathtruncatemacro{\myn}{n}
            \tikzset{3d bar/dx/.evaluated={(xmax-xmin)/n},
                3d bar/dy/.evaluated={(ymax-ymin)/n}}
            \foreach \i in {1,...,\myn}
            {% here
                \foreach [evaluate ={
                        \myx = xmin+(\i-0.5)*(xmax-xmin)/\myn ;
                        \myy = ymin+(\j-0.5)*(ymax-ymin)/\myn ;
                        \myf = f(\myx,\myy) ;
                    }] \j in {0,...,\myn}
                {\path (\myx,\myy,0) pic{3d bar={h=\myf}};}
            }  
        \end{scope}
    \end{tikzpicture}
\end{document}
           

More details in pfg manual v3.1.5.b p1003, section 89 Repeating Things: The Foreach Statement.

enter image description here

1

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.