Plotting functions
A function is a relation or rule that defines a relationship between one variable (the independent variable) and another variable (the dependent variable). A function is linear if it can be expressed in the form f(x)=mx + b. Its graph is a straight line, m is the slope, and b is the value of the function where x equals zero (it is also the point where the graph crosses the y-axis).
Gnuplot is a free, command-driven, interactive plotting program. The set grid command allows grid lines to be drawn on the plot. The set xzeroaxis command draws a line at y = 0. set xrange [-5:5] and set yrange [-60:40] set the horizontal and vertical ranges that will be displayed. plot and splot are the primary commands in Gnuplot, e.g., plot sin(x)/x, splot sin(x*y/20), or plot [-5:5] x**4 -6*x**3 +5*x**2 +24*x -36, where x**4 is x4 and -6*x**3 is -6*x3.
- You can plot functions in Google’s search engine. It is an easy and simple way to plot functions. You can also graph multiple functions using commas. All Google’s graphs can be panned and zoomed.
- You can plot functions with Gnuplot, too, but it is a little more complicated. Open an editor gedit gnuplot.gnu and type:
set terminal wxt size 400,250 enhanced font 'Verdana,12' persist
# Use _set terminal_ to tell gnuplot what kind of output to generate. The wxt terminal device generates output in a separate window.
set border linewidth 1.5 # _set border_ controls the display of the graph borders for the plot and splot commands.
set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 2 # _set style line_ defines a set of line types and widths and point types and sizes so that you can refer to them later by an index instead of repeating all the information at each invocation.
set style line 2 linecolor rgb '#dddd1f' linetype 1 linewidth 2
set xzeroaxis linetype 3 linewidth 2.5 # It draws the x axis.
set xlabel 'x' # Set the label for the x-axis
set ylabel 'y' # Set the label for the y-axis
set key top left # The set key enables a key (or legend) describing plots on a plot.
f(x)=3*x+5 # We define two linear functions
g(x)=4*x
plot f(x) title '3*x+5' with lines linestyle 1, \ # We plot f(x) using linestyle 1
g(x) title '4*x' with lines linestyle 2 # and g(x) using linestyle 2
Gnuplot can be run interactively (user@pc:~$ gnuplot. The environment opens in the console and you can add your commands after gnuplot>. For instance, type load ‘myPlot.gnu’) or from script files (ascii files that have commands written out just as you would enter them interactively): gnuplot myPlot.gnu. 
These are concurrent lines (they intersect each other exactly at one point) and the point of concurrence is (5, 20) (3*x +5 = 4*x; -x=-5; x = 5).
- GNU Octave is software featuring a high-level programming language, primarily intended for numerical computations. When plotting in Octave the plot points are required to have their x-values stored in one vector and the y-values in another vector. Obviously, both vectors must be of the same size.
- We use a x-vector to store the x-values: x=-10:0.1:10; (start -10:step 0.1:end 10, it defines a sequence from start to end by adding increments of step).
- Define our two functions: y = x.*2; z=x.*2 + 10; We use element by element operations (x.*2, x.*2 + 10) on the x-vector to store the functions values in y-vector and z-vector. 3. Finally, we use the command plot(x, y) to plot x*2 or plot(x, y, x, z) to plot both functions.

Observe that both lines are parallel because they have the same slope. They have different y-intercepts (or y-value, an (x,y) point with x=0) so they are not coincident lines.
- We are going to use wxMaxima to plot the linear functions 2*x +1, -1/2*x +1: wxplot2d([2*x +1, -1/2*x +1], [x,-60, 60], [y,-60, 60]); Their graphs are perpendicular lines (they intersect at right angles) because their slopes are negative reciprocals of each other: 2, -1/2.

Graphs of Quadratic Functions
A quadratic function is a type of polynomial function. It has the form f(x) = ax2 + bx + c where a, b, and c are numbers with “a” not equal to zero. Its graph is a symmetric U-shaped curve called a parabola.
- We are going to use ZeGrapher, an open source, free, and easy to use math plotter. Install it, click on Windows, Functions or use the shortcut Ctrl + F, and type: x^2 -4*x +4.
- A parabola can be folded so that the two sides match exactly. The line that divides the parabola into two matching halves or sides is called the axis of symmetry (x = 2). The highest or lowest point of a parabola is its vertex: (2, 0). A parabola intersects its axis of symmetry at the vertex. The sign of the coefficient “a” determines the direction of the parabola. If a > 0, the parabola opens upward (a smile) and the vertex is the minimum or lowest point of the parabola. If a < 0, the parabola opens downward (a frown) and the vertex is the maximum or highest point of the parabola.

The quadratic formula provides the solution(s) to a quadratic equation:
x = -b ± √b2 -4*a*c⁄2*a. The discriminant is the part of the quadratic formula underneath the square root symbol: b²-4ac. The discriminant tells us whether there are two solutions, one solution, or no solutions.
- The discriminant of x2 -4*x + 4 is zero (42 -4*1*4 = 0), so it has two real, identical roots: x = 4, and the equation can be factorized as, x2 -4*x +4 = (x-4)2.
- We are also going to plot -x2 -5*x + 6 and x2 +2*x + 5 with Google and Gnuplot. The discriminant of -x2 -5*x + 6 is greater than zero ((-5)2 -4*(-1)*6 = 49 > 0), so it has two real, distinct (different) roots (-6, 1); a < 0, the parabola opens downward, and the equation can be factorized as -(x + 6) (x - 1).

- The discriminant of x2 +2*x + 5 is less than zero (22 -4*1*5 = -16 < 0), so it has no real roots (-1-2i, -1+2i); a > 0, the parabola opens upward, and the equation can be factorized as (x + (1 - 2 i)) (x + (1 + 2 i)).
- Let’s use Gnuplot to plot them: gedit gnuplot.gnu: (later on, you will plot the two functions with the command: gnuplot gnuplot.gnu).
set terminal wxt size 400,250 enhanced font 'Verdana,11' persist
# Use set terminal to tell gnuplot what kind of output to generate. The wxt terminal device generates output in a separate window.
set border linewidth 1.5 # set border controls the display of the graph borders for the plot and splot commands.
set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 2 # set style line defines a set of line types and widths and point types and sizes so that you can refer to them later by an index instead of repeating all the information at each invocation.
set style line 2 linecolor rgb '#dddd1f' linetype 1 linewidth 2
set xzeroaxis linetype 3 linewidth 2 # It draws the x axis.
set xlabel 'x' # Set the label for the x-axis
set ylabel 'y' # Set the label for the y-axis
set yrange [-40:40] The set yrange command sets the vertical range that will be displayed on the y axis.
set xrange [-10:10] # The set xrange command sets the horizontal range that will be displayed on the x axis.
set key top left # The set key enables a key (or legend) describing plots on a plot.
f(x)=-x**2-5*x+6 # We define two quadratic functions
g(x)=x**2+2*x+5
plot f(x) title '-x^2-5*x+6' with lines linestyle 1, \ # We plot f(x) using linestyle 1
g(x) title 'x^2+2*x+5' with lines linestyle 2 # and g(x) using linestyle 2
Trigonometric functions
Trigonometric or circular functions are functions related to angles. The basic trigonometric functions include the following functions: sine (sin(x)), cosine (cos(x)), tangent (tan(x)), cotangent (cot(x)), secant (sec(x)) and cosecant (csc(x)).
- We use Geogebra to draw a triangle to illustrate these concepts. You may want to go back and read our article Learn Geometry with Geogebra.

An angle which is less than 90° is called an acute angle. Given a right triangle and an acute angle α in the triangle.
cos(α) = cos(33.69°) = the ratio of the length of the adjacent side of the angle divided by the length of the hypotenuse = 3⁄3.61 = 0.83.
sin(α) = sin(33.69°) = the ratio of the length of the opposite side of the angle divided by the length of the hypotenuse = 2⁄3.61 = 0.55.
tan(α) = tan(33.69°) = the ratio of the length of the opposite side of the angle divided by the length of the adjacent side = 2⁄3 = 0.66.
csc(α) = cos(33.69°) = the ratio of the hypotenuse to the opposite side, it is the reciprocal of the sine = 3.61⁄2 = 1.805.
sec(α) = sin(33.69°) = the ratio of the hypotenuse to the adjacent side, it is the reciprocal of the cosine = 3.61⁄3 = 1.20.
cotangent(α) = cot(33.69°) = the ratio of the adjacent side to the opposite side, it is the reciprocal of the tangent = 3⁄2 = 1.5.

- We are going to plot sin(x) using Octave. The graphs of sin x, cos x, and tan x are periodic. A periodic function is a function that repeats its values at regular intervals, f(x+P) = f(x) for all values of x in the domain, and P is the period of the function. The trigonometric functions repeat at intervals of 2π radians.
octave:1> x=-10:0.1:10; # We use a x-vector to store the x-values: x=-10:0.1:10; (start -10:step 0.1:end 10, it defines a sequence from start to end by adding increments of step).
octave:2> y= sin(x); # Define our sin function. When plotting in Octave the plot points are required to have their x-values stored in one vector and the y-values in another vector.
octave:3> plot(x, y); # We use the command plot to plot functions in Octave
octave:4> title("Sin(x)") # We can add titles to an existing plot.
octave:5> xlabel("x") # We can add labels to an existing plot, too.
octave:6> ylabel("y")
octave:7>
- Let’s use wxMaxima to plot sin(x), cos(x), and tan(x) (wxplot2d([cos(x), sin(x), tan(x)],[x, -2*%pi, 2*%pi], [y, -2, 2]); where -2*%pi = -2*Pi), and Google to plot sec(x) and csc(x).

The tangent function has vertical asymptotes whenever cos(x)=0 (the function is undefined): -Pi⁄2, Pi⁄2, 3*Pi⁄2, etc.
Plotting more functions
The exponential function is the function f(x)=ex, where e = 2.71828… is Euler’s constant. More generally, an exponential function is a mathematical function of the following form: f(x)=abx where b is a positive real number (b > 0), b is not equal to 1 (b ≠ 1), and the argument x occurs as an exponent.
If b > 1, then the curve will be increasing and represent exponential growth. In other words, the values of the function get quickly very large. Notice that a larger base (b) makes the graph steeper. If 0 < b <1, then the curve will be decreasing and represent exponential decay. The function values “decay” or decrease as x values increase. They get closer and closer to 0. 
octave:1> x=-10:0.1:20; # We use a x-vector to store the x-values: x=-10:0.1:10; (start -10:step 0.1:end 20, it defines a sequence from start to end by adding increments of step).
octave:2> clf; # clear the current figure window.
octave:3> plot(x, exp(x), 'r;Exponential;'); # We use the command plot to plot ex in Octave
octave:4> title("The exponential function") # We add a title
Plotting functions in Google is as easy as it gets. Observe that 2^x (b > 1) doubles every time when we add one to its input x (exponential growth). On the contrary, (1⁄2)^x (0 < b < 1) shrinks in half every time we add one to its input x (exponential decay). An exponential growth curve is rarely seen in nature. Exponential growth is not sustainable because it depends on infinite amounts of resources which do not exist in the real world.

The natural logarithm of x is the power to which e would have to be raised to equal x, e.g., ln 3.4 = 1.22377…, because e^1.22377… = 3.39998… It is the inverse function of the exponential function: elnx = x. It slowly grows to +∞ as x increases, and slowly grows to -∞ as x approaches 0, so the y-axis is a vertical asymptote.
In other words, the distance between the graph of the natural logarithm and x = 0 approaches zero as x gets closer to zero from the right.
The logarithm of a given number x is the exponent to which another fixed number, the base b, must be raised, to produce that very number x: logb(x) = y exactly if by = x. Let’s plot logarithmic functions with base e and other bases with wxMaxima.
log(x) represents the natural (base e) logarithm of x. However, Maxima does not have a built-in function for the base 10 logarithm or other bases. Do not worry my dear reader, for every problem there is a solution which is simple, clean and wrong -just joking.
We will use the change-of-base formula: logax = logbx⁄logba so the base-2 logarithm (also called the binary logarithm) is equal to the natural logarithm divided by log(2). Therefore, we could use log10(x):= log(x) / log(10); and log2(x):=log(x)/log(2); as useful definitions (radcan(log2(8)); = 3, radcan(expr); simplifies expr). 
We are going to plot |x|, √x, and sgn(x) using ZeGrapher, an open source, free, and easy to use math plotter. Install it, click on Windows, Functions or use the shortcut Ctrl + F, and type: sqrt(x) in the input box named “f(x)”, abs(x) in the input box named “g(x)”, and x/abs(x) in the input box named “h(x)”. 
A square root of a number x is a number “y” whose square is x: y2 = x. The domain of the square root function (y = sqrt(x)= √x) is all values of x such that x ≥ 0. The curve above looks like half of a parabola lying on its side.
The absolute value or modulus of x (it is represented by either abs(x) or |x| and read as “the absolute value of x”) is the non-negative value of x without regarding to its sign. It is its distance from zero on the number line. Namely, |x| = x if x is positive, |x| = −x if x is negative, and |0| = 0.
The sign of a real number, also called sgn or signum, is -1 for negative numbers, 0 for the number zero, or +1 for positive numbers. Any real number can be expressed as the product of its absolute value and its sign function: x=|x| * sgn(x), that’s why we use the formula x/abs(x) to plot it. It is an odd (the following equation holds for all x: -f(x) = f(-x)) mathematical function. Besides, it is not continuous at x = 0. 
Finally, we are going to plot the cube root using Matplotlib. The cube root of a number x is a number “y” that we multiply by itself three times to get x or, more formally, is a number y such that y3 = x. It is and odd function (its plot is symmetric with respect to the coordinate origin).
import numpy as np # NumPy is a library for array computing with Python
import matplotlib.pyplot as plt # Matplotlib is a plotting library for the Python programming language
X = np.arange(-10, 10, 0.01) # Return evenly spaced numbers within the interval [-10, 10] as the values on the x axis.
def p(x): # This is the function that we are going to plot.
return np.cbrt(x) # Return the cube-root of an array, element-wise.
F = p(X) # The corresponding values on the y axis are stored in F.
plt.ylim([-4, 4]) # Set the y-limits on the current axes.
plt.plot(X, F, color='r') # These values x, y are plotted using the mathlib's plot() function.
plt.xlabel('x') # Set the label for the x-axis.
plt.ylabel('y') # Set the label for the y-axis.
plt.title('Cube root') # Set a title for the plot
plt.axvline(0) # Add a vertical line across the Axes
plt.axhline(0) # Add a horizontal line across the Axes
plt.show() # The graphical representation is displayed by using the show() function.
Piecewise-defined functions
A piecewise-defined function is a function defined by two or more sub-functions, where each sub-function applies to a different interval in the domain.
For example, the absolute value (|x| = -x if x < 0, |x| = x if x ≥ 0) and sign (sgn(x) = -1 if x < 0, sgn(x) = 0 if x = 0, sgn(x) = 1 if x > 0) functions are two examples of piecewise-defined functions.
Let’s plot a piecewise-defined function in Maxima. We are going to use Maxima’s command charfun(p); It returns 0 when the predicate p evaluates to false; and 1 when the predicate evaluates to true.
f(x):= charfun(x<2)*x^2 + charfun(x>=2)*4; is how we define piecewise-define functions in Maxima. For all values of x less than two, the first function (x2) is used. For all values of x greater than or equal to two, the second function (4) is used. 
Our function is continuous (it is continuous at every point in its domain; in other words, it does not have any abrupt changes in value -a function without breaks or gaps) because its constituent functions (x2, 4) are continuous on their corresponding intervals or subdomains ( -∞, 2), [2, +∞) and there is no discontinuity at 2.
Now we will plot another piecewise defined function with Octave:
$f(x) = \begin{cases} -x, &x \le -1 \\ x^2, &-1 \lt x \le 0 \\ \sqrt{x}, &x \gt 0 \end{cases}$
x=-5:0.01:-1; y=-x; plot(x,y, "linewidth", 4), hold on
- We use a x-vector to store the x-values: x=-5:0.01:-1.
- Define our sub-function: y = -x for x in [-5, -1]
- Plot it.
- And hold on, so we ask Octave to retain plot data and settings so that subsequent plot commands are displayed on a single graph.
x=-1:0.01:0; y=x.^2; plot(x,y, "linewidth", 4),
We define the sub-function: y = x2 for x in [-1, 0] and plot it. We set line width to 4.
x=0:0.01:5; y=sqrt(x); plot(x,y, "linewidth", 4)
We define the sub-function: $y = \sqrt{x}$ for x in [0, 5] and plot it.

Finally, we will plot the following piecewise-defined function with Python using the library Mathplotlib.
$f(x) = \begin{cases} -x + 2, &x \le -1 \\ x^2, &-1 \lt x \le 0 \\ \log(x) &x \gt 0 \end{cases}$
#!/usr/bin/env python
import numpy as np # NumPy is a library for array computing with Python
import matplotlib.pyplot as plt # Matplotlib is a plotting library for the Python programming language
import math
def piecewise (x): # This is our piecewise-defined function
if x <= -1:
return -x+2
elif -1 <= x <= 0:
return pow(x, 2)
else:
return math.log(x)
vpiecewise = np.vectorize(piecewise) # The purpose of np.vectorize is to transform functions which are not numpy-aware into functions that can operate on (and return) numpy arrays.
x = np.linspace(-5, 5, 10000) # Return evenly spaced numbers over [-5, 5] as the values on the x axis.
y = vpiecewise(x) # Y values on the y axis.
plt.axvline(0) # Add a vertical line across the Axes
plt.axhline(0) # Add a horizontal line across the Axes
plt.plot(x, y, color='r') # These values x, y are plotted using the mathlib's plot() function.
plt.show() # The graphical representation is displayed by using the show() function. The function is continuous at all points except where x=0.


