MTH229 with Julia

Author

CSI Math department

Published

February 8, 2024

Preface

Introduction

MTH229 at the College of Staten Island is a course to introduce a programming language to reinforce concepts of a first-semester calculus course from numerical and graphical points of view.

Some sections use the Julia programming language. For Julia, the computer-lab “projects” are on WeBWorK and there should be sufficient background material therein to work through the details. IN the notes here can be found additional detail for those seeking it. For a more thorough introduction, visit Calculus with Julia.

Installation details for Julia are at the end of this page, but most students will use a departmental web server to access Julia. In a pinch, there are binder instances (resource-constrained) that can be used: Jupyter: Binder; and Pluto: Pluto on Binder.


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, though some may like the Pluto interface as well.

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.

Each topic has a lab project. At CSI, sufficient time is allotted to complete these projects during the lab class. These projects are available as IJulia notebooks or Pluto notebooks. (There are also Pluto notebooks with 5-10 minutes of commentary.) Find links for these near the top of each page of notes.

For example, blank notebooks for test taking, etc. are found by following these links:

ipynb (Pluto html)

Questions and Answers

Question and answers are now presented and completed through WeBWorK. The .ipynb notebooks only contain modest background details.

There are a few idiosyncracies in the WeBWorK pages to be aware of:

  • The code examples are typset in WeBWorK as though they appear in a terminal. A terminal displays the output of each command immediately after execution. In a notebook, when a cell is executed, all the commands are computed and only the last value is shown. (The use of @show or print(...) can be used to display intermediate values in a cell.)

  • Copy and paste from a WeBWorK page into a notebook will usually be unsuccessful, as numbers in the font used to display computer markup do not copy as ASCII numbers into a cell. The numbers can be hand edited though.

  • While Julia is very happy to express its output using scientific notation, WeBWorK is not happy to receive the exact output for an answer. Either replace e with E (as in 1.23e4 would be 1.23E4) or use decimals.

  • For most questions with a numeric answer it is best to copy all 16 digits of output. Several digits are expected to match a correct answer. For numeric questions where an estimate is made, say from a graph, this is significantly relaxed.

  • If the answer is to be a function, the automatic grader is expecting just the rule of the function (an expression), as in for f(x) = sin(x) just enter sin(x).

Basic ideas

Julia makes an excellent choice as a computer supplement 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 operations 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]; plot!(g) to add a curve (using Plots)
  • fzero(f, a, b) or find_zero(f, (a,b)) to find a zero inside the bracketing interval [a,b] (from Roots)
  • fzeros(f, a, b) (or find_zeros(f, (a,b))) to find all the real zeros of a function f in [a,b] (from Roots)
  • fzero(f, a) (or find_zero(f, a)) to find a zero using initial starting oint a (from Roots)
  • limit(f(x), x=>c) to find the limit of f at c (from SymPy)
  • f' to return a function that computes the derivative of f (Added in the MTH229 package based on the derivative function from the ForwardDiff package)
  • diff(f(x),x) to find a symbolic derivative of f (from SymPy)
  • quadgk(f, a, b) to find the numeric integral of f over (a,b) (from the QuadGK package)
  • integrate(f(x), x) to find the symbolic integral of f (from the SymPy package)
  • integrate(f(x), (x, a, b)) to find the definite integral over [a,b] symbolically (from the SymPy package).

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

How to use Julia in MTH 229

Using Julia to complete the projects of MTH 229 can be done in several ways.

Using the juliabox server

CSI students have access to a server that gives access to the jupyter notebooks.

Installing Julia on a personal laptop or computer.

This is not terribly difficult, but does involve a few additional steps:

  • Download and install Julia from julialang.org/. This will install like any other software for you system. You should use the latest released version.

  • Start the application

This opens a terminal, appearing as follows:

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.10.0 (2023-12-25)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

Commands are typed after the prompt (> julia) and executed when the return key is pressed.

Try a few commands and see. Adding 2 and 2 should look like:

julia> 2 + 2
4

julia>

For this class, some external packages must be loaded. First issue the command

using MTH229, Plots

This installs and loads two external packages – one containing functions for this class, the other providing a plotting environments.

Once these are installed and loaded, the projects for the class can be installed following this command:

mth229()

Okay, that should set everything up.

When you want to actually use Julia for this class, you only need to use these two commands from the command line:

using IJulia
notebook()

If not already installed, IJulia will be installed and configured, and then the notebook command will open a browser window allowing the selection of one of the projects. Your work will be within a browser window, not the terminal.

The latter two commands are the only ones needed for subsequent usage, as the packages MTH229 and Plots will be loaded within an IJulia notebook.

Using Pluto notebooks locally

Pluto notebooks are easier to install. At the terminal (assuming version 1.7 or higher) run:

using Pluto
Pluto.run()

This will open a Pluto landing page in a browser tab. The html notebooks linked herein offer a means to “Edit or run this notebook.” Download the notebook (it will make a .jl file) and then open this within Pluto.

Using binder to run the projects remotely:

Binder

Binder

The link above allows Julia for MTH229 to be run through the web.

  • binder is a service that runs interactive notebooks through the web. Each notebook is limited in memory and has a 10 minute inactivity timeout. Binder notebooks are not persistent, though they do have a button to save to local storage.

Binder takes about 10-30 seconds to load. The first cell in the notebook must also be run and that takes another 10-30 seconds. To save memory, the setup does not have SymPy or Plots. The PlotlyLight plotting program is given a stripped down version Plots, enough so the illustrated plotting commands should all work. (If not, please report as an issue). The @symbolic x command will produce a symbolic value (like @syms x in SymPy) that can be used to create expressions that can be used where functions would normally be expected.


This table covers pros and cons for the approaches mentioned above:

                         Using server    Binder       Local Installation
Setup ease                  ✓              ✓                 ×

Speed                       ✓              ×                 ✓

Persistence of work         ×              ×                 ✓

Free                        ✓              ✓                 ✓

Use at home                 ✓              ✓                 ✓