1;	# Not a function file

# Load the package.

pkg load pgfplot;

# 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
    )
);
