0

Good morning. In my course, and in order to explain the mean value of a function, I need to reproduce this photo using tikz enter image description here

My problem is the superposed colours. In fact, I would like to keep both of them visible. Any help is very appreciated Here is my code :

 \documentclass{article}
 \usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \draw[help lines, color=gray, dashed] (-2,-1) grid (8,5);
    \draw[->, thick] (-2,0)--(8,0) node[right]{$x$};
    \draw[->, thick] (0,-1)--(0,5) node[above]{$y$};
        
\begin{scope}
     \clip (-0.3,3.7) .. controls (2.3,0.9) and (3.1,3.9) .. (5.1,4) .. 
        controls (6.5,4.1) and (6.5,3) .. (8.2,2)--(6,-1)--(-.3,-1)--cycle;
    \fill[pink] (0.5,0)node [below=5pt, black] {$a$}--(6,0)node [below=5pt, 
        black] {$b$}--(6,5)--(0.5,5)--(0.5,0)--cycle ;
    \draw[thick, red, dashed] (0.5,0)--(0.5,5);
   \draw[thick, red, dashed] (6,0)--(6,5);
   \node at (3.2,1.5) {$\Huge S$};
   \fill[green] (0,0)node [below=5pt, black] {$a$}--(6,0)node [below=5pt, 
       black] {$b$}--(6,2)--(0,2)--(0,0)--cycle ;
    \end{scope}
  \draw[ultra thick, red] (-0.3,3.7) .. controls (2.3,0.9) and (3.1,3.9) .. (5.1,4) .. controls (6.5,4.1) and (6.5,3) .. (8.2,2)node[black, above=15pt,pos=.9]{$f(x)$};
\draw[ultra thick, blue] (0,2) .. controls (2,2) and (4,2) .. (6,2)node[black, above=15pt,pos=.9]{$f(x)$};
        
\end{tikzpicture}
\end{document}
3
  • Your code doesn't reproduce showed image. To make bot filled shapes visible, you need to use opacity options for fill.
    – Zarko
    Commented Feb 7, 2021 at 15:26
  • @Zarko Thank you very much for you answer
    – A H
    Commented Feb 7, 2021 at 17:04
  • @Zarko I don't understand why you voted negatively ?
    – A H
    Commented Feb 7, 2021 at 18:22

1 Answer 1

2

I'll give you what I use with my students. A pattern=north east lines for the area which represents the mean value.

\documentclass[tikz,border=10pt]{standalone} 
\usetikzlibrary{patterns}
\usepackage{mathrsfs}

\begin{document}
    \begin{tikzpicture}[xscale=4,yscale=3]
        \scriptsize
        
        %Function area
        \definecolor{clr1}{RGB}{207,160,233}    
        \fill[color=clr1,smooth] plot[domain=0:2](\x,{0.25+1.5*2.718^(-(\x)*(\x))}) -- (2,0) -- (0,0) -- cycle;
        
        % Mean area
        \path[pattern color=red,pattern=north east lines] (0,0) rectangle (2,0.91); 
        
        %Function curve
        \draw[line width=1.2pt,color=blue,smooth,samples=100,domain=0:2] plot(\x,{0.25+1.5*2.718^(-(\x)*(\x))});
        \node [color=blue] at (0.7,1.5) {\small $\mathscr{C}_f$};
        
        %Mean function
        \draw[line width=1.2pt,color=red,smooth,samples=100,domain=0:2] plot(\x,{0.91});
        
                
        
        \draw[line width=1pt,-stealth] (-0.6,0) -- (2.3,0);
        \draw[line width=1pt,-stealth] (-0.3,-0.3) -- (-0.3,2);
        \node [below left] at (-0.3,0) {$O$};
        \draw[densely dashed] (0,1.75) -- (0,0) node [below] {\small \strut $a$};
        \draw[densely dashed] (2,0.91) -- (2,0) node [below] {\small \strut $b$};
        \draw[densely dashed] (0,0.91) -- (-0.3,0.91) node [left,red] {\small \strut $\mu$};
        
    \end{tikzpicture}
\end{document}

Mean value of a function

4
  • 1
    Thank you very much
    – A H
    Commented Feb 7, 2021 at 17:15
  • You're welcome. Don't hesitate to vote and accept my answer ;)
    – SebGlav
    Commented Feb 7, 2021 at 17:53
  • 1
    I just accepted your answer (sorry, I just accepted your answer)
    – A H
    Commented Feb 27, 2023 at 17:54
  • Two years is not that long to accept an answer ;) Thanks a lot.
    – SebGlav
    Commented Feb 27, 2023 at 18:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .