Graphics, Figures & TablestikZ | Function Plots

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Henryy
Posts: 4
Joined: Sat Sep 01, 2012 9:34 pm

tikZ | Function Plots

Post by Henryy »

Hello,

first of all, I'm new in this "world" of LaTeX.

I have been required to turn in a simple assignment for my calculus class that consist of plotting a function. However, after two days of trying I'm still not able of plotting it in a correct way.

It's hard for me to get plotting skills in a few days so I would like some of your help in order to do this. The function I must plot is x^3*ln(x) in the interval [1:2]. I don't know what command use for the natural logarithm.

So far I have this:

Code: Select all

%& --shell-escape
\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds}

\begin{document}
  \[
    \int\limits_{1}^{2} \bigl( x^3 \ln x \bigr)  dx
  \]

  \bigskip
  \begin{center}
    \begin{tikzpicture}[samples=200,domain=0:4]
      \filldraw[color=gray,domain=1:2]  plot(\x,\x^3)--(2,0);
      \filldraw[color=white,domain=1:2] plot(\x,{cos(\x r)})--(2,0);
      \draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
      \draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
      \draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
      \draw[color=red,->]  plot (\x,\x) node[right] {$f(x)=x^3 \ln x$};
    \end{tikzpicture}
  \end{center}
\end{document}
By the way I would like to make the graph bigger. How can I do that?

Thanks in advance!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

tikZ | Function Plots

Post by feuersaenger »

Hi Henryy,

while TikZ is a powerful graphics tool, its plotting capabilities are rather limited: in particular, the data range and precision is not as one expects and one has to create the axis along with the complete scaling and all descriptions manually.

You may want to consider using the pgfplots library which is based on tikz - it has the full data range of a double and enough precision, does the complete scaling automatically, computes limits, determines tick positions, handles legends, labels, tick labels, and has much more plot handlers (ranging from simple 1d plots like line and scatter plots up to complex plots like quiver, 3d mesh plots, 3d surface plots, contour plots etc) and simplifies the input of data tables.

I hope that I got what you asked for: I understand that you want to plot x^3 ln(x) in the domain x=[1,2] and you want to illustrate the area which is used to make up the integral over that function, right?

If that is correct, your example would look like

Code: Select all

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\pgfplotsset{compat=1.5}

\begin{document}
\thispagestyle{empty}

\begin{tikzpicture}
	\begin{axis}[
		ymin=0,
		grid=major, % activate major grid lines
		xlabel=$x$,
		ylabel=$y$,
		title={Illustration of $\int_1^2 x^3 \ln(x)\, \text{dx}$},
		axis on top, % descriptions over filled area
		legend pos=outer north east, % customize legend
	]

	% this samples the natural log in [1,2].
	% \closedcycle causes a properly filled plot
	\addplot[blue,fill,domain=1:2]
		{x^3 * ln(x)} \closedcycle;
	\addlegendentry{$x^3 \cdot \ln(x)$}
	\end{axis}
\end{tikzpicture}
\end{document}
I added also a legend, although that might be superfluos.

You may want to browse through the numerous examples in the manual of pgfplots at http://pgfplots.sourceforge.net/ to see if it fits your needs.

DISCLAIMER: I am author of the package. This is both an answer and an advertisement.
Attachments
P.png
P.png (13.73 KiB) Viewed 19167 times
Henryy
Posts: 4
Joined: Sat Sep 01, 2012 9:34 pm

Re: tikZ | Function Plots

Post by Henryy »

Looks Perfect!

Thank you a lot. I have spent hours and hours trying to and finally looks so cool.
Just one question, I installed the library pgfplots, and I am running the whole program from a usb, do I need to install every library again if I open the program in another PC?
feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

Re: tikZ | Function Plots

Post by feuersaenger »

Hi Henryy,

I'm glad that proved to be useful

Concerning your question about the USB stick: I suppose you are using the MikTex portable distribution...? I believe that this thing takes care that everything works well if you transport the USB stick to your next PC.

Of course, if you do not have a TeX distribution which has been tailored for portability (in the sense of USB portability), you would need to install the library again on another PC.

Best regards

Christian
Post Reply