Menu

First plot

Dirk Krause
← Previous ↑ Tutorial TOC Next →

The first plot

First plot

Example overview

In our first example we want to plot the resulting resistance for a parallel connection of two resistors.
R1 is variable in the range 0≤R1≤10Ω.
R2=10Ω (constant).

The formula to calculate the resulting resistance is:

Rres=R1R2R1+R2

Source file

We use the source file example01.m to plot.

1;  # Not a function file

# Load the package.

pkg load octpgfpl;

# Function to calculate resulting resistance of two
# parallel resistors. The function must be vectorized.

function [result] = parallelResistors(a,b)
  result = (a .* b) ./ (a .+ b);
  result(!(isfinite(result))) = 0;
endfunction

# Function to plot, must be a vectorized function.

function [y] = f(x)
  y = parallelResistors(x, 10);
endfunction

pgfplot(

    # The first three arguments are
    # - output file name
    # - Unit for width and height specification (cm or in)
    # - A vector containing width (10cm) and height (5cm).
    # Here we produce a *.tex file, we can run pdflatex on it.

    'example01.tex', 'cm', [10 5],

    # All remaining arguments are key/value pairs.
    #
    # The x axis is configured using the cell array list returned
    # by the pgfplot_varargs function.
    # This function allows us to write a one dimensional cell array
    # list consisting of key/value pairs in a readable manner.

    'axis.x', pgfplot_varargs(

        # We use linear scaling for the axis, values are in the
        # range 0 <= x <= 10.

        'lin',      [0 10],

        # To draw the grid we divide the x range into 10 intervals.

        'grid',     10,

        # To draw tics we divide the x range into 10 intervals too.

        'tics',     10,

        # The axis label is typeset by LaTeX.
        # For each backslash we want to pass to LaTeX we have to
        # write two backslashes here.

        'label',        "\\(R_1\\)",

        # The unit label is also typeset by LaTeX.

        'unit.text',    "\\(\\Omega\\)"
    ),

    # The y axis is configured similarly.

    'axis.y', pgfplot_varargs(
        'lin',      [0 5],
        'grid',     5,
        'tics',     5,
        'label',        "\\(R_{\\text{res}}\\)",
        'unit.text',    "\\(\\Omega\\)"
    ),

    # Here we configure one item to plot.

    'plot', pgfplot_varargs(

        # The item to plot is the f() function. The function must be
        # vectorized.

        'function',     @f,

        # The line width for the plot is two times the base line width
        # used to draw the grid.

        'lw',       2
    ),

    # Finally we configure options for LaTeX.

    'latex', pgfplot_varargs(

        # We pass the option 12pt to the document class.

        'font-size',    12
    )
);

Source file explained

The x axis is in the range 0 to 10. We did not configure an x range explicitly, so the range 0≤x≤10Ω is used.

We did not explicitly configure the number of plot intervals, so the default 20 is used.

A linspace(0,10,21)' vector (20 intervals have 21 points) is created by pgfplot() and passed to the f() function to obtain the 21 corresponding y values.

A spline is constructed through the 21 points. As we did not explicitly configure a spline type the default "not-a-knot" is used. Constructing the spline results in a piecewise polynomial.
Constructing a piecewise polynomial from a point set requires to create and solve a linear system of equations.

A sequence of Bezier spline segments is constructed from the points and the derivatives. This is used for plotting.

Process the file

Run

octave -q example01.m
pdflatex example01

to produce example01.pdf.

You can use your favourite PDF reader to view the file.

For more complicated plots you will probably need some iteration steps in which you modify the *.m file, run octave and pdflatex.
Some PDF readers like i.e. evince do not lock the PDF file and update the view automatically on changes, so you can keep the reader open.
Other PDF readers lock the file which causes pdflatex to fail or do not update the view automatically.

Once the image is to your liking you can replace the file name "example01.tex" by "example01.pgf" and run octave once again. Now you have a PGF file you can include in a *.tex source.

\documentclass{article}
...
\usepackage{color}
\usepackage{graphicx}
\usepackage{fancybox}
\usepackage{pgfcore}
...
\begin{figure}\%
{\centering\%
\input{example01.pgf}\%
\caption{My example diagram}\label{fig:example01}\%
}\%
\end{figure}

← Previous ↑ Tutorial TOC Next →

Related

Wiki: Tutorial

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.