1

enter image description here

I was wondering if there is an easy way to generate the following image in latex using the Tikz package.

3 Answers3

6

A bit awkward but fine:

\documentclass[tikz]{standalone}

\begin{document} \begin{tikzpicture} \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$}; \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 [thick, ->] (0,-.5)--(0,6) node[left]{$y$}; \draw [thick, ->] (-.5,0)--(8.3,0) node[below]{$x$}; \end{tikzpicture} \end{document}

Output:

enter image description here

4

With pgfplots:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{fillbetween}

\begin{document} \begin{tikzpicture} \begin{axis}[ axis lines=middle, xlabel=$x$, ylabel=$y$, xlabel style = {anchor=north east}, ylabel style = {anchor=north east}, xtick=\empty, ytick=\empty, clip=false ] \addplot [name path=A,domain=0:5] {x^2} node[left] {$f(x)$}; \path [name path=B] (\pgfkeysvalueof{/pgfplots/xmin},0) -- (\pgfkeysvalueof{/pgfplots/xmax},0); \addplot [orange] fill between [of=A and B, soft clip={(2,0) rectangle (4,25)},]; \node at (3,3) {$S$}; \draw[dashed] (2,2^2) -- (2,0) node[below] {$a$} (4,4^2) -- (4,0) node[below] {$b$}; \end{axis} \end{tikzpicture} \end{document}

enter image description here

Note: MWE is base on examples in pgfplots manual, section 5.7 Fill between.

Zarko
  • 296,517
  • 1
    Dear @Sebastiano, you are right. Corrected, however this should correct OP for exercise and make him familiar with code :-). Thank you for pointing mi on these errors. – Zarko Sep 01 '20 at 09:12
  • For using your answer, The OP should compute the equation of that function. Isn't? –  Sep 01 '20 at 20:57
  • @C.F.G, no, pgfplots compute given function. Also some curve can be determined by set of coordinates or can be drawn as you do in your answer. pgfplots is based on tikz/pgf packages. – Zarko Sep 01 '20 at 21:19
3

For fun! ( will be updated if more, :-) )

Compile with Asymptote or Asymptote Web Application.

Zarko's code

import graph;
size(10cm,8cm,false);
real f(real x){return x^2;}
path F=graph(f,0,5,350);

picture pic; fill(pic,box((2,0),(4,25)),orange); clip(pic,F--(5,0)--cycle); add(pic);

label("$S$",(3,3)); draw(Label("$a$",BeginPoint),(2,0)--(2,f(2)),dashed); draw(Label("$a$",BeginPoint),(4,0)--(4,f(4)),dashed); draw(Label("$f(x)$",Relative(.99),LeftSide),F); draw(Label("$x$",Relative(.99)),(0,0)--(5,0),Arrow); draw(Label("$y$",Relative(.99),LeftSide),(0,0)--(0,5^2),Arrow);

enter image description here

C.F.G's code

unitsize(1cm);
path f=(-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);

picture pic; fill(pic,box((0.5,0),(6,5)),pink); draw(pic,(0.5,0)--(0.5,5),dashed); draw(pic,(6,0)--(6,5),dashed); clip(pic,f--(8.2,0)--(-0.3,0)--cycle); add(pic);

label("$S$",(3.2,1.5)); label("$a$",(0.5,0),S); label("$b$",(6,0),S); draw(Label("$f(x)$",Relative(.95),LeftSide),f,red+1bp); draw(Label("$y$",Relative(.99)),(0,-0.5)--(0,6),Arrow); draw(Label("$x$",Relative(.99),LeftSide),(-0.5,0)--(8.3,0),Arrow);

enter image description here

Not mine!