Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am really new to graphics in LaTeX, I have understood the skills needed to draw basic straight lines and circles; I am now learning how to make simple functions like polynomials, trigonometric, exponential, logarithm, etc., and I am new to this site, so apologize if a similar question had been asked.

The book that I am currently using is The Not So Short Introduction to LaTeX2e, the wikibook, but they all mentioned about the Bezier curve. I understand the basic idea of the Bezier curve through this website http://pomax.github.io/bezierinfo/. But it is still doesn't make my graph drawing easy. Those books that I am reading do not seem to teach us how to find the control points when drawing the Bezier curves.

My questions are:
1. Are there any quick way to draw the bezier curve without calculating the control points, by the way, is it a MUST for us to calculate the control points in order to draw a Bezier curve, or do we do it by trial-and-error?
2. Besides using the Bezier curve, is there any simpler method to draw simple functions for example the command \drawsin(x^2) or something like that or any other methods?
3. I have also read about the TikZ picture environment, but it doesn't seem to be any simpler? What are the advantages and disadvantages of using TikZ compared to Bezier curve? Can we draw simple functions like polynomials using TikZ?
4. It is possible to show me the code (using any methods) needed to draw say the very basic graph like y=x^2 including the documentclass and appropriate packages. I apologise I had not made any useful attempt because this is really new to me.

I am trying to make some notes to my students about functions and I need a quick and neat way to draw graphs. I will appreciate any helps and explanations.

share|improve this question
    
pgfplots is your friend. For baziers, you may use tikzedt with tikz. May be useful: Is there any tool to draw bezier curve and transfer the coordinates to tikz? –  Harish Kumar Feb 1 at 5:12
    
This might be of help for #2, #3 and #4 in plotting functions: Easiest way to plot a function with PGF/TikZ. –  Peter Grill Feb 1 at 5:37

4 Answers 4

More examples using pgfplots

enter image description here

Code

\documentclass[twocolumn]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz,xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\begin{axis}[domain=-4:4,
    restrict y to domain=0:4,
    samples=100,
    grid=major,smooth,
    xlabel=$x$,
    ylabel=$y(x)$, 
    legend pos=north west]
\addplot [color=green,thick]  {exp(x)};
\addplot [color=purple,thick] {exp(-x)}; 
\legend{$e^x$, $e^{-x}$}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[domain=0.001:6,                  
    samples=50,
    grid=major,smooth,
    xlabel=$x$,
    ylabel=$y(x)$,
    legend pos=north east]
\addplot [color=red,thick]    {1/(0.5*x*(2*pi)^0.5)*exp(-ln(x)*ln(x)/0.5)};
\legend{$ {\frac{1}{(0.5x(2\pi)^{0.5})}e^{-\frac{\ln(x)\ln(x)}{0.5}}}$}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[                 
    samples=100,
    restrict y to domain=-4:4,
    grid=major,smooth,
    xlabel=$x$,
    ylabel=$y(x)$,
    legend pos=north east]
\addplot [color=red,thick,domain=-180:180]      {sin(x)};
\legend{$sin(x)$,$x*sin(1/x)$}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[                 
    samples=200,
    restrict y to domain=-1:1,
    grid=major,
    xlabel=$x$,
    ylabel=$y(x)$,
    legend pos=north east]
\addplot [color=red,thick,domain=-0.02:0.02 ]     {x*sin(1/x)};
\legend{$x*sin(1/x)$}
\end{axis}
\end{tikzpicture}

\vspace{2cm}

\begin{tikzpicture}
\begin{semilogyaxis}[                
    log basis y=10,
    grid=major,smooth,
    xlabel=$x$,
    ylabel=$y(x)$,
    legend pos=north east]
\addplot [color=red,thick]  {10^x};
\legend{$10^x$}
\end{semilogyaxis}
\end{tikzpicture}

\vspace{3cm}

\begin{tikzpicture}
\begin{loglogaxis}[
grid=major,
xlabel=$x$,
ylabel=$y(x)$,
legend pos=north west
]
\addplot[only marks, mark size=4pt,mark=triangle,fill,black] coordinates{
(1 , 10)
(10 , 100)
(100 ,  1000)};
\legend{discrete type}
\end{loglogaxis}
\end{tikzpicture}

\end{document}
share|improve this answer

Plotting is an easy task for PSTricks.

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot}
\psset
{
    xunit=\pstRadUnit,% 3 cm represents π = 3.1415926535
    algebraic=true,% infix notation enabled
    plotpoints=100,% default 50
}
\begin{document}
\begin{pspicture}(0,-2)(6,2)
    \psplot{0}{TwoPi}{2*sin(2*x)}
\end{pspicture}
\end{document}

enter image description here

share|improve this answer

Since you're new to LaTeX and "...need a quick and neat way to draw graphs" you're probably better off using a tool that generates LaTeX quality graphs while avoiding the LaTeX code. This site will do that. Type in the function(s) and plotting parameters and after pressing preview, you get the graph. When you've got the picture you want, just download the picture in 1 of many formats which can then be inserted into your LaTeX document. In addition to the standard 2D graphs there's a tab for producing parametric, polar, implicit, 3D graphs and more. enter image description here

share|improve this answer

I'm much more fluent in the MetaPost and Asymptote languages, but it seems you are better up with tikz, and in that case the hobbypackage is the most direct answer to your first question. It implements Hobby's algorithm for tikz: this algorithm allows to compute (third-order) Bézier curves without specifying the control points, which Tikz is not able to do on its own. Hobby's algorithm was designed by John Hobby, first for use in Knuth's Metafont drawing-fonts program, and later for Hobby's own Metafont-inspired MetaPost drawing program. Asymptote also uses it and even extended it for 3D drawings.

A short example of the Hobby package, picked up from its documentation:

\documentclass{article}
     \usepackage{tikz}
     \usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}[scale =.5]
    \draw (0, 0) to [curve through = {(6, 4) .. (4, 9 ) .. (1, 7)}] (3, 5);
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.