1

I am interested in drawing the following figure with tikz

enter image description here

I (think) I know how to do this with Bézier curves using \draw with filling, but I want the top curve to be an exact graph of a function that depends on a parameter, like f(x)=cos(kx) or f(x)=exp(kx), where I can adjust the parameter k to later animate my tikz figure. My problem is that I do not know how to use \draw to connect everything together, along with plotting the top curve using an actual function along with the green filling underneath. I would appreciate all help on this.

2
  • 1
    What you tried so far? At known function you can draw it by \draw plot (\x,<your function>). Of course, for plot you can define function domain and number of samples too. See TikZ & PGF manual, 22.5 Plotting a Function, page 344
    – Zarko
    Commented Feb 11, 2023 at 8:07
  • @Zarko thank you. I don't actually know how to combine plotting a specific function for the top curve along with the draw command to connect the lines that make up the three straight-line sides. I also would not know how to make the green filling in this hybrid approach.
    – user209867
    Commented Feb 11, 2023 at 8:13

1 Answer 1

1

You can add the points and the plot in the same \draw command.

Something like this:

\documentclass[tikz,border=2mm]{standalone}

% example of a function with a parameter \k
\tikzset{declare function={f(\x,\k)=0.5*cos(2*\x r)+\k;}}

\begin{document}
\begin{tikzpicture}
\draw (0,3) |- (4,0) -- (4,3);
\draw[fill=green!20] plot[samples=101,domain=0:4] (\x,{f(\x,2)}) -- (4,0) -| cycle;
\end{tikzpicture}

\begin{tikzpicture}
\draw (0,3) |- (4,0) -- (4,3);
\foreach[count=\ii]\i in {green,orange,yellow}
  \draw[fill=\i!20] plot[samples=101,domain=0:4] (\x,{f(\x,2.5-0.5*\ii)}) -- (4,0) -| cycle;
\end{tikzpicture}
\end{document}

First figure

Second figure

2
  • thank you very much for the great answer
    – user209867
    Commented Feb 11, 2023 at 9:14
  • 1
    @TeXAndSuch, you're welcome!! Commented Feb 11, 2023 at 9:15

You must log in to answer this question.