MTH 229 Projects

This is a collection of notes for exploring calculus concepts with the Julia programming language. Such an approach is used in MTH 229 at the College of Staten Island.

These notes are broken into different sections, where most all sections have some self-grading questions at the end that allow you to test your knowledge of that material. The code should be copy-and-pasteable into a julia session. The code output is similar to what would be shown if evaluated in an IJulia cell, our recommended interface while learning julia.

The notes mostly follow topics of a standard first-semester calculus course after some background material is presented for learning julia within a mathematical framework.

Basics of types, order of operations, assignment and variables.


An assignment for this material: ipynb view

Shows how to define and call a function. Technical parts include ternary operator, multiple arguments, and return values (tuples).


An assignment for this material: ipynb view

This demonstrates the use of the Gadfly package for plotting. This package has a very simple plot interface for graphing one or more functions.

As well, a discussion about arrays and mapping a function over an array is given. This will be useful later on with limits, ...


An assignment for this material: ipynb view

Finding zeros for polynomials, graphically finding zeros, and using the bisection method.

The add-on Roots package provides some convenient functionality.


An assignment for this material: ipynb view

Basics of limits.

Discussion on floating point representation and potential issues (subtracting like-sized objects!)


An assignment for this material: ipynb view

Explore forward difference and central difference with a bit on error analysis.

We end with a brief discussion on automatic differentiation, as implemented in the PowerSeries add-on package via the Roots package.


An assignment for this material: ipynb view

Basics of Newton's method with a copy-and-paste function to do the work after the student explores a bit.

Discusses iterative algorithms, approximation, some analysis.

The fzero function of the Roots package is discussed.


An assignment for this material: ipynb view

A look at the relationship between a function and its first and second derivatives.


An assignment for this material: ipynb view

A project on minimization and maximization. Some standard applied problems are presented.


An assignment for this material: ipynb view

Basics of integration with applications including rectangle, trapezoid, Simpson's, and the quaggk function. Applications to volumes of solids of revolution.


An assignment for this material: ipynb view

Calculus II material

Discusses how to do some symbolic math in julia through the SymPy package.


An assignment for this material: ipynb view

  • Applications of the integral: area between two curves, volume of solids of revolution, other volumes


An assignment for this material: ipynb view

  • Techniques of integration: substitution, integration by parts, partial fractions


An assignment for this material: ipynb view

  • Taylor polynomials


An assignment for this material: ipynb view

  • Parametric equations and polar coordinates


An assignment for this material: ipynb view

Basic ideas

Julia makes an excellent choice for this material as its syntax is very similar to standard mathematical syntax. The ability to define mathematical functions using the familiar f(x) = ... notation makes getting started really easy. Further, the fact that functions are first-class objects means that it is possible to create higher-order julia functions that mirror the standard operators of calculus. The following pattern is used throughout:

action(function_object, args...)

For example, the notes use:

  • plot(f, a, b) to plot f over [a,b] (from Gadfly)
  • plot([f,g], a, b) to plot both f and g over the interval [a,b]
  • roots(f) to find the roots of a polynomial function, f (from Polynomials)
  • fzeros(f) to find the real roots of a polynomial function f (from Roots)
  • fzero(f, [a,b]) to find a root inside the bracketing interval [a,b] (from Roots)
  • limit(f, c) to find the limit of f at c (from SymPy)
  • D(f) to return a function that computes the derivative of f (from the Roots package)
  • fzero(f, a) or [fzero(f, x) for x in [x1,x2, ...]] to find root(s) of f starting at a or each of the x's
  • quadgk(f, a, b) to find the numeric integral of f over (a,b) (from base julia)
  • integrate(f) to find the symbolic integral of f (from the SymPy package)
  • integrate(f, a, b) to find the definite integral over [a,b] symbolically

With just this basic set of actions, akin to buttons on the calculator, a rich variety of problems can be addressed.

Some additional resources

Julia is a young language, with the bulk of its development being done since its initial announcement. It has relatively few online resources. Some are compiled here. Many of these are linked to from a julia web brain.

  • The Julia manual provides a comprehensive overview

  • MIT Professor Steven G Johnson has some notes on using julia here and a cheat sheet here.

  • some blog posts are collected here.

  • At forio.com a tutorial is provided here.

  • A tutorial in IJulia format by Isaiah Norton is here, with the original file found here.

Before starting out with Julia it must be available.

Downloading julia

In order to get started with Julia it needs to be installed. If this is not done already, you have a bit of work to do to get julia and the notebook interface provided by IJulia.

First to install julia you can download a copy or install it from source. Likely a download is easiest. Official releases are available from julialang.org but it is best to download a cutting-edge release from status.julialang.org. Installation is hopefully similar to what you do for other software on your system.

Starting julia

Starting julia varies amongst the different operating systems. All have a console where commands are typed for julia to interpret and execute. This is known as the command line and though a long familiar means of interacting with computers, it is generally not familiar to the average student. We will need to learn to like the command line. Once done, you may think it is great, but it can a bit frustrating getting to that attitude.

Here is what the command line looks like on startup from a mac book pro within the terminal:

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" to list help topics
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.3.0-prerelease+3692 (2014-06-16 11:54 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 4f69de4* (2 days old master)
|__/                   |  x86_64-apple-darwin13.2.0

julia> 

The command line is the last line: a prompt beginning with julia>. Here is where you type an expression and then the enter key to ask julia to evaluate it.

A simple command is then typed into the computer followed by the enter key. This is then sent to julia's interpreter and an answer returned:

2 + 2
4

If you get 4, you are able to use julia.

IJulia An enhanced interface for using julia interactively

The command line is not the most comfortable learning experience for julia, rather it is suggested that the IJulia notebook interface be used. In the IJulia notebook, the command line is replaced by a cell where commands can be entered and executed in batches. The editing of commands is much easier and some features for integrated help are available.

Embedded Image

The above graphic is from the main web page for julia (julialang.com) and shows the IJulia notebook with some graphics provided by the Gadfly package.

Using IJulia will require a few additional installation steps:

  • download anaconda (https://store.continuum.io/cshop/anaconda/). It is big, but free. Install it, then within a terminal (or Windows' command propmpt) enter these commands:
conda update conda
conda update ipython
  • Start julia then enter these commands to install the packages we use:
Pkg.update()
Pkg.add("IJulia")
Pkg.add("Gadfly")
Pkg.add("Roots")

The above commands form the basics of julia's package system. Like most computer languages, julia can be extended by user-contributed packages. The complete list of available packages is kept on the computer you are using julia at. This list is updated by the command Pkg.update(). New packages are made available for use by installing or adding them to your system via Pkg.add. Adding packages will automatically install any dependent packages. As well, external libraries should also be installed for you. This magic attempts to automatically identify what your computer system needs and acts accordingly.

The above commands need only be done when new packages are being installed. However, each time you wish to actual use an external package in a session, it must be added. This is done with the using command, as using Gadfly

Afterwards those commands are successful, the following command will start the notebook interface:

run(`ipython notebook --profile=julia`)

After all the installation, you can start the IJulia interface by simply starting julia, then issuing the above command or you can run just the command ipython notebook --profile=julia from the command line.

Online versions of julia

You can use julia online (for now) at forio.

You can use a julia terminal at https://cloud.sagemath.com.

Extending Julia with packages

Julia can be extended through external packages. Although a relatively young language, there are already over 300 add-on packages readily available for Julia through its package manager.

For example, there are numerous packages that provide means to draw graphs. To list a few: Gadfly, Winston, PyPlot, Gaston, ASCIIPlots, Plotly, GoogleCharts, ... The first few are the main ones. In these notes we use Gadfly. The PyPlot package provides an interface to the feature-rich pyplot functionality of Python, so requires some external programs. The Winston package is more MATLAB-like than Gadfly. All three work well within the IJulia framework (which itself is provided through an add-on package).

Installing an add-on pacakge

In the julia world, a package author may publish his or her package so that it is easy for an end user to use and install. For the end user there are just a handful of important commands to install a package:

  • Call Pkg.udpate() to update the currently installed external packages and to update the list of available packages to install. Though this command can be a bit slow, it is a good idea to run it periodically.

  • To add a new package, call Pkg.add("package_name"), where you have to put the appropriate package name in. For example, the command Pkg.add("Gadfly") will install the Gadfly package. In the process, any external dependencies will be resolved. These include installing any packages that the one you want depends on and in some cases, additional software.

  • There are other useful commands, but those two are basically it: Pkg.update() to update and Pkg.add() to add a new package.

Using a package

External packages must be loaded into a session. This need only be done once. The easiest way is to use the keyword using, a in using Gadfly. This must be done before you try to use any functionality related to the package. For interactive use, it is a good idea to just pull in familiar packages at the outset.

This has some cost, as some packages are slow to load. (In particular Gadfly, which is a large package.) Over time, julia will incorporate some tricks to speed this up considerably, but for now that isn't the case.

So, to make a plot using Gadfly, the sequence might go like:

using Gadfly
f(x) = x^2 - 2x
plot(f, -2, 1)          # plot is in the Gadfly package
x -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 -5.0 -4.9 -4.8 -4.7 -4.6 -4.5 -4.4 -4.3 -4.2 -4.1 -4.0 -3.9 -3.8 -3.7 -3.6 -3.5 -3.4 -3.3 -3.2 -3.1 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 -5 0 5 -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 -12.0 -11.5 -11.0 -10.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 -20 -10 0 10 20 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 f(x)

To make a plot using Winston, the only difference for this example would be calling using Winston, though there are other significant differences.

The manual has some more information.