13

I have problem to draw graphic with LaTeX (especially TikZ)?

Like this:

enter image description here

20

You could also use PGFPlots for plotting your function and the integral. The trapeziums can be generated by plotting the function twice with a low sampling frequency: Once using the ycomb style for the vertical lines, and once using the default sharp plot for the connecting lines.

If you need to do this for several plot, you can define some styles to make it easier to keep everything consistent. That way, you can get the following image

using the following code:

\begin{axis}[
    integral axis,
    ymin=0,
    xmin=0.75, xmax=11.25,
    domain=1.5:10.5,
    xtick={2,...,10},
    xticklabels={$a=x_0$, $x_1$,,,$x_{j-1}$,$x_j$,,$x_{n-1}$,$b=x_n$},
]
% The function
\addplot [very thick, cyan!75!blue] {f} node [anchor=south] {$y=f(x)$};

% The filled area under the approximate integral
\addplot [integral fill=cyan!15] {f} \closedcycle;

% The approximate integral
\addplot [integral line=black] {f};

% The vertical lines between the segments
\addplot [integral, ycomb] {f};

% The highlighted segment
\addplot [integral fill=cyan!35, domain=6:7, samples=2] {f} \closedcycle;
\end{axis}

Here's the complete document with all the styles:

\documentclass{article}

\usepackage{pgfplots} %http://www.ctan.org/pkg/pgfplots
\pgfplotsset{compat=newest, set layers=standard}

\begin{document}

\pgfplotsset{
    integral axis/.style={
        axis lines=middle,
        enlarge y limits=upper,
        axis equal image, width=12cm,
        xlabel=$x$, ylabel=$y$,
        ytick=\empty,
        xticklabel style={font=\small, text height=1.5ex, anchor=north},
        samples=100
    },
    integral/.style={
            domain=2:10,
            samples=9
    },
    integral fill/.style={
            integral,
            draw=none, fill=#1,
            on layer=axis background
        },
        integral fill/.default=cyan!10,
        integral line/.style={
            integral,
            very thick,
            draw=#1
        },
        integral line/.default=black
}


\begin{tikzpicture}[
    % The function that is used for all the plots
    declare function={f=x/5-cos(deg(x*1.85))/2+2;}
]
\begin{axis}[
    integral axis,
    ymin=0,
    xmin=0.75, xmax=11.25,
    domain=1.5:10.5,
    xtick={2,...,10},
    xticklabels={$a=x_0$, $x_1$,,,$x_{j-1}$,$x_j$,,$x_{n-1}$,$b=x_n$},
]
% The function
\addplot [very thick, cyan!75!blue] {f} node [anchor=south] {$y=f(x)$};

% The filled area under the approximate integral
\addplot [integral fill=cyan!15] {f} \closedcycle;

% The approximate integral
\addplot [integral line=black] {f};

% The vertical lines between the segments
\addplot [integral, ycomb] {f};

% The highlighted segment
\addplot [integral fill=cyan!35, domain=6:7, samples=2] {f} \closedcycle;
\end{axis}
\end{tikzpicture}

\end{document}

In case you're not using the newest version of PGFPlots (1.8), the set layers and on axis keys will not be defined. In that case, you can remove those keys and rearrange the \addplot command to make sure everything's drawn in the right order:

\documentclass{article}

\usepackage{pgfplots} %http://www.ctan.org/pkg/pgfplots
\pgfplotsset{compat=newest}

\begin{document}

\pgfplotsset{
    integral axis/.style={
        axis lines=middle,
        enlarge y limits=upper,
        axis equal image, width=12cm,
        xlabel=$x$, ylabel=$y$,
        ytick=\empty,
        xticklabel style={font=\small, text height=1.5ex, anchor=north},
        samples=100
    },
    integral/.style={
            domain=2:10,
            samples=9
    },
    integral fill/.style={
            integral,
            draw=none, fill=#1,
            %on layer=axis background
        },
        integral fill/.default=cyan!10,
        integral line/.style={
            integral,
            very thick,
            draw=#1
        },
        integral line/.default=black
}


\begin{tikzpicture}[
    % The function that is used for all the plots
    declare function={f=x/5-cos(deg(x*1.85))/2+2;}
]
\begin{axis}[
    integral axis,
    ymin=0,
    xmin=0.75, xmax=11.25,
    domain=1.5:10.5,
    xtick={2,...,10},
    xticklabels={$a=x_0$, $x_1$,,,$x_{j-1}$,$x_j$,,$x_{n-1}$,$b=x_n$},
    axis on top
]
% The filled area under the approximate integral
\addplot [integral fill=cyan!15] {f} \closedcycle;

% The highlighted segment
\addplot [integral fill=cyan!35, domain=6:7, samples=2] {f} \closedcycle;

% The function
\addplot [very thick, cyan!75!blue] {f} node [anchor=south] {$y=f(x)$};

% The approximate integral
\addplot [integral line=black] {f};

% The vertical lines between the segments
\addplot [integral, ycomb] {f};

\end{axis}
\end{tikzpicture}

\end{document}
  • @HirWanto: Pleasure! Please don't forget to upvote all answers you find helpful (by clicking the upward pointing triangle next to the answers), and to come back and accept the one you end up using (by clicking the tick mark next to the answer). – Jake Apr 25 '13 at 11:54
  • Hello , 2Jake , i tried your command and I can show trapezium rule for integration in my pdf reader, – Hirwanto Apr 25 '13 at 13:40
  • Hello , @Jake , i tried your command and I can show trapezium rule for integration in my pdf reader, but I have problem also with your command is \pgfplotsset{compat=newest, set layers=standard} .On My opinion, I try to remove your command : \pgfplotsset{compat=newest, set layers=standard}, and the result is I can show in My PDF Reader…. How is your opinion ? – Hirwanto Apr 25 '13 at 13:50
  • @HirWanto: You're probably not using the newest version (1.8) of PGFPlots (the set layers key was only introduced recently). I've added an example that should work correctly with older versions of PGFPlots. – Jake Apr 25 '13 at 13:54
  • What is PGFplots ... ?? Where i can get the newest version pgf plots – Hirwanto Apr 25 '13 at 13:58
11

Here's one possibility; some coordinates are placed first; then we fill the path with the zigzag lines; then, using the coordinates and the to[out=<angle>,in=<angle>] syntax, the curve is built; next, vertical lines are added, and finally the axes and some labels are placed:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\coordinate (p1) at (0.7,3);
\coordinate (p2) at (1,3.3);
\coordinate (p3) at (2,2.5);
\coordinate (p4) at (3,2.5);
\coordinate (p5) at (4,3.5);
\coordinate (p6) at (5,4.1);
\coordinate (p7) at (6,3.4);
\coordinate (p8) at (7,4.1);
\coordinate (p9) at (8,4.6);
\coordinate (p10) at (9,4);
\coordinate (p11) at (9.5,4.7);

% The cyan background
\fill[cyan!10] 
  (p2|-0,0) -- (p2) -- (p3) -- (p4) -- (p5) -- (p6) -- (p7) -- (p8) -- (p9) -- (p10) -- (p10|-0,0) -- cycle;
% the dark cyan stripe
\fill[cyan!30] (p6|-0,0) -- (p6) -- (p7) -- (p7|-0,0) -- cycle;
% the curve
\draw[thick,cyan] 
  (p1) to[out=70,in=180] (p2) to[out=0,in=150] 
  (p3) to[out=-50,in=230] (p4) to[out=30,in=220] 
  (p5) to[out=50,in=150] (p6) to[out=-30,in=180] 
  (p7) to[out=0,in=230] (p8) to[out=40,in=180] 
  (p9) to[out=-30,in=180] (p10) to[out=0,in=260] (p11);
% the broken line connecting points on the curve
\draw (p2) -- (p3) -- (p4) -- (p5) -- (p6) -- (p7) -- (p8) -- (p9) -- (p10);
% vertical lines and labels
\foreach \n/\texto in {2/{a=x_0},3/{x_1},4/{},5/{},6/{x_{j-1}},7/{x_j},8/{},9/{x_{n-1}},10/{b=x_n}}
{
  \draw (p\n|-0,0) -- (p\n);
  \node[below,text height=1.5ex,text depth=1ex,font=\small] at (p\n|-0,0) {$\texto$};
}
% The axes
\draw[->] (-0.5,0) -- (10,0) coordinate (x axis);
\draw[->] (0,-0.5) -- (0,6) coordinate (y axis);
% labels for the axes
\node[below] at (x axis) {$x$};
\node[left] at (y axis) {$y$};
% label for the function
\node[above,text=cyan] at (p11) {$y=f(x)$};
\end{tikzpicture}

\end{document}

enter image description here

  • Hello @Gonzalo, I tried your command but I can’t show in PDF Reader, I use compile PDFTexify , In Execution Modes, I choose dvi >ps> pdf and compile to pdf , and I use WinEdt 6.0 as Text Editor. in my text editor show only : "!package pgf Error : No shape named p10 is known" – Hirwanto Apr 25 '13 at 13:55
  • 1
    @HirWanto did you try with the exact code I posted? – Gonzalo Medina Apr 25 '13 at 17:07
  • i tried more than five times ... and i still can't show in PDF Reader – Hirwanto Apr 26 '13 at 5:53
  • @HirWanto that's weird. The code runs without problems (I asked some friends of mine to also try the code and they had no problems). Could you please report the exact error message that you get? – Gonzalo Medina Apr 26 '13 at 12:46
9

enter image description here

One more option for the fresh eyes - Asymptote approach. trapez.asy:

import graph;
size(400);
texpreamble("\usepackage{lmodern}");

pair[] v={(7,12),(10,12.5),(15,10),(20,10),(25,13.5),(30,15),(35,13),(40,14),(45,15.5),(50,14),(53,15),};

int n=v.length;     // number of points

pen curvePen=blue+opacity(0.8)+1.2pt;     // definition of the pens to be used
pen axisPen=red+0.618pt;

pen fillA=rgb(0.67,0.91,0.98);
pen fillB=rgb(0.81,0.95,0.99);

real tickWidth=0.5;     
real xaxisTip=2;

guide g=graph(v,join=operator..);   // function curve, joined cubic segments
guide tg=graph(v,join=operator--);  // joined linear segments

fill(subpath(tg,1,n-2)--(v[n-2].x,0)--(v[1].x,0)--cycle,fillB); // basic area 
fill(subpath(tg,5,6)--(v[6].x,0)--(v[5].x,0)--cycle,fillA);     // area between x_{j-1} and x_j

draw(g,curvePen);   // draw a function curve

draw((v[1].x,-tickWidth)--v[1]); // draw a first vertical line
for(int i=2;i<n-1;++i){
  draw((v[i].x,-tickWidth)--v[i]--v[i-1]); // draw next vertical line and a top line 
}


int[] labelInd={1,2,5,6,8,9};   // indices of points with labels
string[] labelStr={             // labels
  "a=x_0","x_1","x_{j-1}","x_j","x_{n-1}","b=x_n"
};

string baselineTemplate="$";              // construction of the baseline template,
for(int i=0;i<labelStr.length;++i){       //   which contains all labels 
  baselineTemplate+=labelStr[i];          //   for the labels be placed on the same baseline
}
baselineTemplate+="$";

string s;
for(int i=0;i<labelInd.length;++i){         
  s=baseline("$"+labelStr[i]+"$"          // fixing baseline to the template
    ,template=baselineTemplate);
  label(s,(v[labelInd[i]].x,-tickWidth),S);  // placment of the label, "S" mean "to the South of"
}

label("$y=f(x)$",v[n-1],N,curvePen);      // one more label

xaxis(-2,v[n-1].x+xaxisTip,Arrow(HookHead,size=2),above=true,p=axisPen);
yaxis("$y$",-2,v[n-1].y,Arrow(HookHead,size=2),p=axisPen);

label(
  baseline("$x$",template=baselineTemplate),  // fixing the x-axis label to the same baseline,
  (v[n-1].x+xaxisTip,-tickWidth),S,axisPen    //   as the other labels along the axis
);

To get a standalone trapez.pdf, run asy -f pdf trapez.asy.

  • Thank you for all ... i'm so happy for all your solution , thank you... thank you – Hirwanto Apr 25 '13 at 11:46
1

With PSTricks.

enter image description here

\documentclass[pstricks]{standalone}
\usepackage{pst-plot,pst-node}
\psset{algebraic,linejoin=2}

\def\f[#1]{sin(3*#1)/2+#1/3+1}
\def\Tick(#1)#2{%
    \rput[b](#1|0,-12pt){\small$#2$}
    \psline(#1|0,0)(#1|0,-2pt)
}
\begin{document}
\begin{pspicture}(-.25,-.5)(8.5,4.5)
    \fnpnodes[plotpoints=8]{.75}{7.5}{\f[x]}{P}
    \multido{\iL=0+1,\iR=1+1}{\Pnodecount}{\pspolygon[fillstyle=solid,fillcolor=cyan!15](P\iL|0,0)(P\iL)(P\iR)(P\iR|0,0)}
    \pspolygon[fillstyle=solid,fillcolor=cyan!35](P3|0,0)(P3)(P4)(P4|0,0)
    \psplot[plotpoints=100,linecolor=cyan!75!blue]{0.5}{7.75}{\f[x]}
    \psaxes[ticks=none,labels=none]{->}(0,0)(-.25,-.5)(8,4)[$x$,0][$y$,90]
    \Tick(P0){a=x_0}
    \Tick(P1){x_1}
    \Tick(P3){x_{j-1}}
    \Tick(P4){x_j}
    \Tick(P\the\numexpr\Pnodecount-1){x_{n-1}}
    \Tick(P\Pnodecount){b=x_n}
    \uput[90](*7.5 {\f[x]+.5}){\color{cyan!75!blue}$y=f(x)$}
\end{pspicture}
\end{document}

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.