7

I'm using tikz to create two arbitrary curves and fill the area between them. What I'm trying to do is:

enter image description here

This is my approach:

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[line join=round] \draw [-latex,thick] (-0.5,0) -- (4.5,0) node[below]{$x$}; \draw [-latex,thick] (0,-0.5) -- (0,3.5) node[left]{$y$};

\draw [fill=white!50!blue,opacity=0.3, smooth,samples=100,domain=0.5:3.5] plot(\x, {sin(\x200)0.25+0.5}) -- plot(\x, {sin(\x200)0.25+2.5}) -- cycle;

\draw (0,0) node[below left]{$0$} ; \draw (2,1.5) node[]{$D$} ; \draw [blue, domain=-0.25:4, smooth, variable=\x] plot (\x, {sin(\x200)0.25+0.5}); \draw [blue, domain=-0.25:4, smooth, variable=\x] plot (\x, {sin(\x200)0.25+2.5}); \draw [blue] (0.5,-0.25) -- (0.5,3) ; \draw [blue] (3.5,-0.25) -- (3.5,3); \draw (0.5,0) node[below left]{$a$} ; \draw (3.5,0) node[below right]{$b$}; \draw (4,3) node[right]{$y=\psi(x)$} ; \draw (4,1) node[right]{$y=\varphi(x)$}; \end{tikzpicture}

\end{document}

And what I get:

enter image description here

I don't know how to fix these crossed lines.

I'm also having trouble to define the sin(\x*200)*0.25+2.5 in the y axis, to get something very similar:

enter image description here

2 Answers2

9
\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw [fill=white!50!blue, opacity=0.3, smooth, samples=100, domain=0.5:3.5] plot(\x, {sin(\x*200)*0.25+0.5}) -- plot[domain=3.5:0.5] (\x, {sin(\x*200)*0.25+2.5}) -- cycle; 
\draw[yshift=-3cm, fill=white!50!red, opacity=0.3, smooth, samples=100, domain=0.5:3.5] plot({sin(\x*200)*0.25+0.5},\x) -- plot[domain=3.5:0.5] ({sin(\x*200)*0.25+2.5},\x) -- cycle; 
\end{tikzpicture}
\end{document}

Two shaded regions

6

As usual when something strange comes out of a computer, it's because it's doing exactly what you told it to do.

The problem is this line:

\draw [fill=white!50!blue,opacity=0.3, smooth,samples=100,domain=0.5:3.5] plot(\x, {sin(\x*200)*0.25+0.5}) -- plot(\x, {sin(\x*200)*0.25+2.5}) -- cycle; 

You've told it to fill a region starting from 0.5 to 3.5 following the function sin(200x)+0.5 then connect that to a plot from 0.5 to 3.5 following the function sin(200x)+2.5 and then join to the starting point. So your region enclosure goes from the bottom right to the top left and then from the top right back to the bottom left.

How to fix this? Well you need to tell LaTeX to graph one of those functions from right to left instead of left to right. Time to brush off those dusty high school algebra skills. We replace all the instances of \x in one function to 4-\x (since the domain is 0.5–3.5). Replacing the problematic line with:

\draw [fill=white!50!blue,opacity=0.3, smooth,samples=100,domain=0.5:3.5] plot(\x, {sin(\x*200)*0.25+0.5}) -- plot(4-\x, {sin((4-\x)*200)*0.25+2.5}) -- cycle; 

will give the desired results.

For your second graph, you're essentially doing an inverse function graph which means swapping x and y. In TikZ terms this means swapping the first and second parameters in coordinates and plot statements.

Don Hosek
  • 14,078