These programs are supplied for students of MAT342. Others may use them as they see fit. Note that most of them are based on material from several different textbooks—see the individual programs for more information.

Instructions

The programs and functions here all work with Octave and have been coded so that they should work with MATLAB.

Clicking on the links below will load the corresponding function or program into your browser's window; use the back button to return to this page.

How you download a function or program to your computer is browser dependent. In Chrome and Firefox you can right-click on a link and select "Save link as". To run the programs (names do not end with ".m") you must make them executable after downloading them. This is done by typing

chmod u+x <program name>

after downloading the program. This need be done only once for each downloaded program. The program can then be run with

./<program name>

Assuming you start Octave in the same directory into which you downloaded a function, you can obtain help on running the function by typing

help <function name>

at the octave prompt.

Demos

Visualizing Taylor polynomials [Program]

This octave program does essentially the same thing as the sequence of MatLab commands given on page 21 of "Numerical Mathematics and Computing" 6th edition, by Cheney and Kincaid, Brooks/Cole, 2008.

Base Conversion [Function]

This function accepts a decimal number and and returns the string representation of the number in the specified base. This function only converts to bases between (inclusive) base-2 and base-36. It uses the convention that hexadecimal numbers use of using alphabetic letters to represent values larger than 9.

For example, in Octave the command convbase(246,2) returns the string 11110110_2.

Round or chop to n significant decimal places [Function]

This function will round or chop a supplied number to a specified number of significant decimal places.

Estimate π with a Monte Carlo Method [Function]

Procedure to estimate π as outlined in the University of Nebraska-Lincoln Physical Chemistry lab's Basics of Monte Carlo Simulations.

Loss of significance [Program]

This program demonstrates the lost of precision that occurs when two numbers very close to each other are subtracted. This example uses x and sin(x) for a small value of x.

Solve f(x)=0 with fsolve [Program]

Demonstrates the use of the Octave function "fsolve" to solve nonlinear equations in the form f(x) = 0 where x is a vector of one or more unknown values.

Root Finding Algorithms

These functions implement a range of root finding algorithms: bisection, Newton's, secant, fixed point, and Steffensen's. The programs good_newton and bad_newton illustrate that while Newton's method normally has a quadratic rate of convergence when finding a root of f(x), the rate of convergence is only linear if the derivative f'(x) = 0.

Divided Differences [Function]

This function computes the divided differences that can be used to construct an interpolating polynomial for the given set of data.

Richardson's Extrapolation [Function]

This function implements Richardson's Extrapolation to determine an approximation of f'(x) at a particular value of x. Based on an algorithm on page 185 in "Numerical Mathematics and Computing" 5th Edition, by Cheney and Kincaid, Brooks-Cole, 2004.

Romberg Integration [Function]

Based on an algorithm on pages 223 and 224 in "Numerical Mathematics and Computing" 5th Edition, by Cheney and Kincaid, Brooks-Cole, 2004.

Adaptive Simpson's Rule [Function]

This function uses an adaptive Simpson's rule to approximate the value of the integral. Notice that this is not very efficient; it is recursive and recomputes many function values that have already been computed. The code in this function is meant to be clear and explain the ideas behind adaptive schemes rather than to be an efficient implementation of one. The program verify_adaptSimp demonstrates that the error is usually within the specified tolerance, but may not be.

Gaussian Quadrature [Function]

Estimates integral using Gaussian quadrature. Needs the function grule.m to compute nodes and weights from Legendre Polynomials.

Gaussian Elimination [Function]

Performs Gaussian elimination with partial pivoting on a rectangular matrix.

Backward Solve [Function]

Performs a backward solve on the system Ax=b where A has already been reduced by Gaussian elimination.

Tridiagonal Solver [Function]

Solves Ax=b where A is a tridiagonal matrix stored in three vectors.

Euler's method for ODEs [Program]

This program demonstrates the use of Euler's Method to solve the initial value problem x'(t) = tsin(t), x(0)=1.

Taylor series for ODEs [Program]

This program demonstrates the use of Taylor Series to solve the initial value problem x'(t) = tsin(t), x(0)=1.

2nd Order Runge-Kutta (Burden-Faires) [Function]

This function implements a 2nd order Runge-Kutta algorithm to solve the initial value problem

dy/dt = f(t,y),      y(t0) = y0.

This particular version is based on the algorithm presented in "Numerical Analysis", 6th Edition, by Burden and Faires, Brooks-Cole, 1997.

2nd Order Runge-Kutta (Cheney-Kincaid) [Function]

This function implements a 2nd order Runge-Kutta algorithm to solve the initial value problem

dy/dt = f(t,y),      y(t0) = y0.

This particular version is based on pages 459-461 in "Numerical Mathematics and Computing" 5th Edition, by Cheney and Kincaid, Brooks-Cole, 2004.

4th Order Runge-Kutta for systems of equations [Function]

This function implements 4th order Runge-Kutta to solve

dy/dt = f(t,y),      y(t(1)) = y0.

at the t values stored in the t array (so the interval of solution is given by first and last entries in the array for t: [t(1), t(N)]). Notice that it works equally well for scalar functions f(t,y) (in the case of a single 1st order ODE) or for vector functions f(t,y) (in the case of multiple 1st order ODEs).

Runge-Kutta-Fehlberg [Function]

This function implements 4th-5th order Runge-Kutta-Fehlberg Method to solve the initial value problem

dy/dt = f(t,y),      y(a) = y0.

on the interval [a,b]. Based on pseudocode presented in "Numerical Analysis", 6th Edition, by Burden and Faires, Brooks-Cole, 1997.

Shooting Method [Function]

This function implements the shooting method to solve second-order boundary value problems. It assumes that the second order equation has been converted to a first order system of two equations and uses the built-in Octave function lsode() to solve the initial value problems. The secant method is used to refine the initial values of y' used for the initial value problems.

Finite Difference Method [Function]

Compute finite difference solution to the boundary value problem

x'' = u(t) + v(t)x + w(t)x'
x(t(1)) = a,    x(t(n)) = b

on the interval [t(1), t(n)].

Forward Time, Centered Space (FTCS) scheme applied to the Heat Equation [Program]

Use the FTCS scheme to solve the heat equation in a thin rod.

Crank Nicolson applied to the Heat Equation [Program]

Use Crank-Nicolson scheme to solve the heat equation in a thin rod. Needs the tridiagonal solver.

Finite Difference Solution of Wave Equation [Program]

Solves the one-dimensional wave equation.

Finite Difference Solution of Elliptic Helmholtz Problem [Program]

Solves the two-dimensional Helmholtz equation on the unit square.

Fibonacci Search [Program]

Demonstrates the use of the Fibonacci Search to find the minimum of the unimodal function |sin x| on the interval [2,4]. Needs the Fibonacci search function.

Golden Section Search [Program]

Demonstrates the use of the Golden Section Search to find the minimum of the unimodal function |sin x| on the interval [2,4]. Needs the golden section search function.

Simulated Annealing in One Dimension [Program]

Demonstrates the use of Simulated Annealing to find the minimum of the function x ex sin 55x on the interval [0,5]. Needs the simulated annealing solver.