# MATLAB tutorial script learning goals

1. Definitions and Operators

By completing this script the student should be able to reproduce the operations from the script and be able to extend them to simple new cases. The purpose of this script is to provide an extremely simple overview of the basic functionality of MATLAB. While we have assumed that the student has some prior MATLAB knowledge (we assume basic competency) this script quickly goes over what we expect the student to know before continuing onto the more advanced scripts. We specifically show how to use MATLAB as a simple calculator, how to create row and column vectors, how to use the vectors to perform matrix operations, how to search or perform operations on vectors (and the disadvantage of using for loops in MATLAB), and how to create functions within MATLAB.

1. Differentiation and Integration

By completing this script the student will have seen how to perform simple numeric differentiation and integration in MATLAB and be able to apply this method to a new case. This script uses the basic functionality shown in the Definitions and Operators script to create grids and create functions which operate on the grids. It then uses basic numerical methods (first order finite difference and the trapezoid rule) to compute the derivate and integral.

1. Sparse and Full Matrices

By completing this script the student should know what sparse matrices are, how to use and create them, and why operations on them can be more efficient than the same operations on full matrices. This script shows the basic functionality of the built in sparse matrix MATLAB commands, specifically how to create a sparse identity matrix, how to check if a matrix is sparse, how to make a matrix into a sparse one, and how to use the sparse matrices as normal ones. The script then shows the performance difference in a simple operation between a sparse and full (non-sparse) matrix.

1. Structures

By completing this script the student will learn what structures are, how to create them and how to interact with them. This script shows how to create structures using the “.” syntax, how to interact with structures through field names, and how to check if a field exists within a structure. A link is provided to the MathWorks website that shows what functions can operate on structures.

1. Eigenvalue Problems

By completing this script the student should know how to solve eigenvalue problems both algebraically and numerically using the built-in MATLAB functions. The student should also learn that in general the built-in MATLAB functions are much faster than creating your own solution. This script shows how to use the simple “eig” function to compute the eigenvalues, how to use the characteristic polynomial and root finding functions to solve the problem algebraically, how to extract the eigenvectors along with the eigenvalues from the “eig” function and how to use the “eigs” function to extract a specific eigenvalue pair.

1. Solving Linear Systems

By completing this script the student should know how to efficiently solve linear systems of equations in MATLAB using both iterative methods and the built-in MATLAB “\” command. This script shows the ease with which MATLAB can solve linear systems with using its “\” command, how to solve a system using an iterative method, when using an iterative method is more efficient than the “\” command, and how to turn on the verbosity of the “\” to see the logic tree that it uses to pick the correct method.

1. Solving an ODE (NPZD)

By completing this script the student should know how to solve a simple ODE system using the built-in MATLAB function. While this system is relatively simple, this is the first “real” example that is not trivial. The students will see how to use all the skills that have previously been learned to solve a specific problem. This script shows the student how to set up the initial conditions and parameters for the system, and then how to use the “ode45” function to solve the system for a specific amount of time. Taking advantage of being able to place functions within scripts in MATLAB 2016b and further releases, we also provide the function containing the ODE system. The students should be able to see how the system was organized in order to be accepted by the “ode45” function.

1. Solving the Lotka-Volterra Equations

By completing this script the student should be comfortable solving simple ODE systems and investigating the results. This is another simple system of ODEs which is straightforward to solve using the previous methods. The extension here is that we plot and investigate the resulting data. The student should realize that functionally solving the equation is only part of the problem and that they must still interpret the results.

1. Solving a Two-Box Lotka-Volterra System

By completing this script the student should see how even simple systems can be extended to lead to interesting dynamics. This script links two LV systems with coupling terms. We first show the dynamics when the systems are independent, and then how they change when the coupling is added. The students are encouraged to play with the parameters themselves and investigate the system further.

1. Solving the Forced Damped Pendulum

By completing this script the student should learn about chaotic systems and how to investigate the chaotic properties through numerical experimentation. The FDP is one of the classical examples of a chaotic system, here we solving the system numerically using the methods that the student should be familiar with. The student should be able to see the irregularity in the path of both the angular velocity and angular position. They should also easily see the sensitivity to initial conditions through the plot of two cases with a tiny initial difference.

1. Time-Series Analysis

By completing this script the student should learn how to use both traditional spectral analysis, and wavelet analysis to investigate the periodicity within a time-series. This script shows the students how to use the built in functions “periodogram” and “cwt” to compute the Fourier spectrum and wavelet spectrum respectively. In both the examples in this script there is a clear period which the student should be able to identify using the provided method.

1. Wavelet Coherence

By completing this script the student should learn how to use the built-in MATLAB “wcoherence” function to compute the wavelet coherence and should have a basic understanding of what different plots correspond to. This script creates a number of simple sine waves with some key differences. These waves are then compared using wavelet coherence so as to discern how specific graphs of the coherence relate to the actual waves.

1. 1D Spectral Analysis

By completing this script the student should learn how to takes the Power Spectral Density of a signal using the FFT. They should also begin to understand how changes in the original waveform can manifest themselves in the PSD. This script uses simple Gaussian packets to show how the period of oscillation is reflected in the PSD and the effects that changing the envelope can have. The advantage of using logarithmic scales for power is also illustrated.

1. 1D Spectral Windowing and Variance Reduction

By completing this script the student should learn how to use spectral windowing to gain localized signal information from a time varying signal. The student should also learn how to use windowing to reduce the theoretical 100% variance from a raw spectrum. This script shows the power of windowing not only for extracting localized signal information but for summing over window sizes to reduce the variance. Interestingly this method is very similar to the procedure for wavelet analysis (wavelet analysis grew out of the windowed Fourier transform).

1. 2D Spectral Analysis

By completing this script the student should learn how to perform 2D spectral analysis, being able to compute both the 2D spectrum and a radially averaged spectrum. This script shows the student how to use the 2D FFT to compute the 2D spectrum, while this can be plotted and provides some information it can be more useful to reduce the spectrum down to 1D to have an easy analogue to 1D spectral analysis. We show the student how to do this by radially averaging the spectrum in shells out from the centre.

1. 3D Graphics

By completing this script the student should know the 3D graphical capability of MATLAB and how to create 3D figures themselves. This script creates a relatively simple 3D data set and then proceeds to use MATLABs 3D graphics to create visually appealing figures. This script shows the students the various commands for modifying the figure, for example, the transparency of the surfaces, the angle of the camera, the colour of the surfaces, the position of the light, etc. … While not exhaustive, controlling these parameters should allow the students to create relatively professional looking figures.

1. Graphics Annotation

By completing this script the student should learn how to annotate figures with arrows, lines and circles. The student should also learn how much longer figures take to render with increasing resolution. This script shows the students the commands to place various shapes, lines and arrows onto their figures. This is shown for both 2D and 3D figures. In addition, the resolution of the figure is modified and the rendering time at each resolution is timed. This is presented in the final plot. The student should see the quadratic growth in time.

1. Graphics Changes

This small script (with no actual code) highlights the changes that were introduced in MATLAB 2014b to graphics. The students should be aware of these changes especially if they are using an older version of MATLAB. A link is also provided to the official MATLAB webpage introducing these changes.

1. Exporting Graphics

By completing this script the student should learn how to export their figures to various formats. This script shows how to print a figure (in this case the same figure as the 3D Graphics script) to either a PDF or JPEG. The script also talks about the advantages and disadvantages of these formats. A few more settings must be set for the PDF as the page parameters matter in this case.

1. Large-Scale Graphics

By completing this script the student should understand some of the difficulties in visualizing large data sets and some of the methods they can use to bring the data to a more manageable level. This script uses an extremely well-resolved version of the figure from the 3D Graphics script to illustrate how creating figures with large datasets can take a huge amount of time. The basic solution we present to the students is skipping, only rendering every N points. While we are losing data, when you just want to investigate a large data set this method is perfectly valid (to make a publishable figure you would just wait the full amount of time, but you only want to do this once).

1. Graphical Structures

By completing this script the student should understand how the graphics properties are organized into a structure and how to edit them. The students are also shown the “hidden” appData properties which can be used. This script shows the students that essentially all of the properties that they want to edit in their figure exist within the graphical object’s properties. Using the syntax shown in the Structures script the student can modify all these values. This is the change that was introduced in MATLAB 2014b to graphics. The previous method (before 2014b) for changing these properties is also shown and still works.

1. 3D Wave Equation

By completing this script the student should understand how to use multi dimensional FFTs to solve the Wave equation (and more generally a PDE in a triply periodic domain) and how to plot the results. This script builds off the introductory knowledge from the Introduction and Graphics sections to solve a more challenging problem. The students are shown how many of the topics from previous scripts can come together in treating a problem.

1. Waves on an Annular Domain

By completing this script the student should understand how to create a solver for a simple equation on a non-Cartesian domain. The numerical scheme for solving the equation should be familiar to the students, (the only difference here is using the Laplacian in polar coordinates) with the new portion being the construction and usage of the annular domain.

1. Burgers Equation and Filtering

By completing this script the student should learn some of the limitations of spectral methods when dealing with sharp changes in the fields (shocks) and some techniques to mitigate these issues. The primary way that spectral methods deal with shocks and other discontinuities is through filtering, this script shows a few different types of filters and how they affect an equation which is known to produce shocks. The students are encouraged to play with the filters and see if or when spectral methods fail.

1. Reaction Diffusion Equation

By completing this script the student should learn how to implement a more complicated numerical scheme for a system of nonlinear PDEs. This script shows off how to implement a Crank-Nicolson scheme to handle diffusion, and RK4 for the reactions, with Strang Splitting allowing for the separate consideration of each. The students should be able to understand the methods used and how to implement them in MATLAB.

1. Spectral Helmholtz Solver

By completing this script the student should know how to solve a Helmholtz problem spectrally. This script shows a simple and quick method for solving the Helmholtz problem using the hopefully now familiar spectral methods. The solution is then plotted.

1. Finite Difference preconditioned Spectral Helmholtz Solver

By completing this script the student should learn how more complicated problems necessitate using a combination of different methods. Specifically the students will learn about preconditioning and how this can speed up convergence of an iterative method such as GMRES. This script builds off the previous one, thus the core should appear familiar to the students. The key difference in this case is that the diffusivity is no longer constant and so solving the matrix problem in spectral space (as in the last script) is no longer trivial. To do this we use an iterative method called GMRES. To speed up this computation we precondition using a finite difference approximation, this makes the matrix have a smaller condition number and so will converge faster. This example is complicated however the students should be able to use this example as a reference should they need to implement something similar in their work.

1. Boundary Conditions with the Diffusion Operator

By completing this script the student should learn how to implement boundary conditions through manipulating the rows and columns of the matrix problem. This simple script based off a version of a similar script in Spectral Methods in Matlab by Trefethen, shows how by modifying rows of the matrix problem (both on the RHS and LHS) you can implement your boundary conditions directly. In addition to this, the script shows how by factoring the problem beforehand you can speed up computation. The difference between the solutions (negligible) is shown.

1. Evolving the Variable Diffusivity Diffusion Equation

By completing this script the student should learn how to solve a variable diffusivity, boundary value problem in time. This is an extension of the previous script with the addition of stepping forward in time. The students should see the process of how a static case from the last script is extended to evolve in time. This example combines dealing with the variable diffusivity and dealing with the boundary conditions.

1. Sturm-Liouville Eigenvalue Problem

By completing this script the student should learn how to solve the classical Sturm-Liouville problem which appears throughout applied mathematics and physics. This script can be considered an extension of the Eigenvalue script from the introduction however now the matrix problem is an operator problem and the variables are functions. This script shows how to solve the problem using “eig” and then how to create the modes that the eigenvalues correspond to. The solution is then used to approximate a sample function that is chosen to satisfy the BCs, thus exploiting the theory of Sturm-Liouville problems.

1. Ito vs. Stratonovich

By completing this script the student should learn how stochastic ODEs are handled numerically and the differences between the two calculi. This script shows how to solve simple stochastic ODEs using the two different calculi. The differences between them are highlighted. An extension is provide at the end with the derivative-free Milstein method provided for both calculi (this method is O(Δt)).

1. Convergence of Ito Additive Noise

By completing this script the student should learn some of the properties of the Euler-Maruyama method for  stochastic ODEs with additive noise. This script computes an ensemble of solutions to a simple stochastic ODE with additive noise. The mean and standard deviation are compared for different time step choices, and the effect of changing the number of ensemble members on statistics is demonstrated.

1. Convergence of Ito Multiplicative

By completing this script the student should learn some of the properties of the Euler-Maruyama method for stochastic ODEs with multiplicative noise. This script computes an ensemble of solutions to a simple stochastic ODE with additive noise. The mean and standard deviation are compared for different time step choices, and the effect of changing the number of ensemble members on statistics is demonstrated.

1. Stochastic Potential Well

By completing this script the student should learn how to solve a stochastic ODE that uses red noise, implemented using Bartosch’s algorithm. This script solves for the position and velocity of a stochastic particle moving in a bi-stable potential well. The student will see why red noise is numerically preferable to white noise and how Bartosch’s algorithm is implemented. In addition a lot of time is spent on analyzing the results of the ODE, looking at realizations, the moments, phase space diagrams and histograms. Additionally, we provide an algorithm for calculating the waiting times for transitions between the two wells which can be used to characterize stochastic resonance, and has uses well beyond this script.

1. Gillespie's Algorithm

By completing this script the student should learn how Gillespie’s algorithm is implemented (specifically how to implement it minimizing the use of for loops). This script is a highly efficient version of Gillespie’s algorithm which only uses one for loop. Each of the different steps in the algorithm is explained and places where efficiency imprevements are made are highlighted. In addition to learning how to implement Gillespie’s algorithm, the students should learn methods for avoiding for loops which can be applied to their own code.

1. Stochastic Pendulum with white noise

By completing this script the student should learn how to solve the stochastic pendulum ODE that uses white noise using the Euler-Maruyama method. This script solves the position and velocity of a stochastic pendulum.  The results of the ODE are then analyzed looking at realizations, the moments, phase space diagrams and histograms.

1. Stochastic Pendulum with red noise

By completing this script the student should learn how to solve the stochastic pendulum ODE that uses red noise using Bartosch’s algorithm. This script solves the position and velocity of a stochastic pendulum.  The results of the ODE are then analyzed looking at realizations, the moments, phase space diagrams and histograms. This script and the previous one can be used together to explicitly show the differences in algorithm depending on the type of noise used.

1. Stochastic Hunter-Prey model in 2D

By completing this script the student should learn how to implement branching Brownian motion as part of a larger more complicated code. This script is one of the more complicated examples as the dynamics of both the predator and the prey are complex. The predator moves and eats along with slowing down if it has just eaten. The prey move, breed and die. The student should be able to understand the various parts of the script and how each of the parts relate back to its physical or biological interpretation.

 Script (or group of scripts) Outcomes Definitions and Operations Learn or review basic MATLAB functionality. Differentiation and Integration Learn how to numerically perform differentiation and integration in MATLAB. Sparse and Full Matrices Learn the difference and use of sparse matrices compared to full ones. Structures Learn how structures are used and controlled in MATLAB. Eigenvalue Problems Learn how to solve eigenvalue problems in MATLAB. Linear Systems Learn the different methods of solving linear systems of equations in MATLAB. ODEs Learn how to solve simple ODEs in MATLAB. Time-Series Analysis Learn how to use time-series analysis to extract information from a dataset. Spectral Analysis Learn how to use Fourier transforms to analyze spectral information. Parallel and GPU Learn how to take advantage of GPU and Multi-processor computing in MATLAB. Graphics Learn how to create, modify, and export graphics in MATLAB 3D Waves Learn how to solve the 3D wave equation using spectral methods. Annular Waves Learn how to solve the wave equation in a non-Cartesian domain. Burgers Equation and Filtering Learn how to using filtering to solve some of the issues with spectral methods and discontinuities. Reaction Diffusion Equation Learn how solve to solve more complicated systems of nonlinear PDEs using splitting techniques. Helmholtz Problem Learn how to use preconditioning to efficiently solve a Helmholtz problem. Boundary Conditions Learn how to implement non-trivial boundary conditions for PDEs  through manipulation of the matrix problem. Sturm-Liouville Learn how to solve Stum-Liouville problems. Ito vs. Stratonovich Learn the difference in numerical implementation between Ito and Stratonovich calculi for stochastic DEs. Ito Convergence Learn about the convergence of numerical methods for Ito calculus stochastic DEs. Double Well Learn how to solve a stochastic DE for dynamics in a potential well with red noise. Gillespie’s Algorithm Learn how to efficiently implement Gillespie’s algorithm. Stochastic Pendulum Learn how to implement white and red noise in a stochastic pendulum, and the difference between the implementations. Hunter with Death Learn how to implement branching Brownian motion and create a dynamically complex predator and prey model.