Introduction to Programming and Numerical Methods in MATLAB - Comprehensive Notes

  • This text introduces programming and numerical methods using MATLAB, emphasizing hands-on experience and practical application.
  • It is intended as a reference for MATLAB commands and examples.
  • Focuses on numerical methods applicable in science and engineering undergrad courses.
  • Uses MATLAB for rapid prototyping and testing of numerical methods and learning to program.
  • Methods rely on material generally covered before the first year of undergraduate study.
  • Exercises at the end of each chapter allow exploration of topics; harder tasks are marked with an asterisk (*).
  • MATLAB commands appear in a specific font.
  • Offers a glossary of MATLAB commands with examples (Appendix B).
  • Emphasizes MATLAB's built-in commands for numerical tasks while teaching the inner workings of the methods.

Why Numerical Methods are Needed

  • Numerical methods provide approximate solutions when analytical solutions do not exist or are impractical.
  • They reduce problems to a finite number of unknowns solvable by computers.
  • Obtaining solutions can be intricate and require finesse.
  • MATLAB syntax is used to express ideas.

Text Structure

  • Designed as a reference for MATLAB commands and examples.
  • Introduces numerical methods and manipulation of quantities like matrices.
  • Contains short codes for immediate experimentation.
  • Longer code examples:
  >> commands to be entered
  Results
  • Codes to be saved in a file appear in boxes:
  a = 1;

(Wider boxes for codes with longer lines).

  • Chapters include tasks with solutions in Appendix C.

Chapter Overview

  • Chapter 2: Programming aspects, high-quality data plots, codes and functions to augment MATLAB's vocabulary, and error analysis (numerical and coding-related).
  • Debugging programs is an essential skill emphasized.
  • Chapter 3: Looping and logical structures developed in tandem with mathematical concepts. Examples of how MATLAB can be used to solve problems using just algebra.
  • Chapters 4 & 5: Classical numerical methods: root finding, interpolation, and extrapolation. Detailed discussions even though MATLAB has intrinsic functions (fzero and polyfit). Description of Newton forward differences.
  • Chapters 6 & 7: Numerical analysis of integration and differentiation.
  • Chapter 6: Matrices, rank, linear independence, eigenvalues, eigenvectors, and their interpretation for 2-by-2 matrices.
  • Chapter 7: Numerical integration.
  • Chapter 8: Numerical solution of ordinary differential equations.
  • Chapter 9: Basic statistical quantities and exploration of maps, including chaotic behavior.

Appendices

  • Introduction to matrix algebra.
  • Glossary of useful terms.
  • Solutions to all tasks.

Prerequisites

  • Mathematical literacy: solving algebraic equations, integrating and differentiating simple functions, and solving separable differential equations.
  • Exposure to complex numbers and matrices is useful but not necessary, as these concepts are introduced.

Acknowledgements

  • Thanks to students of the University of Birmingham for their feedback.

Contents

  • Chapter 1: Simple Calculations with MATLAB
    • Introduction and a Word of Warning
    • Scalar Quantities and Variables
    • Rules for Naming of Variables
    • Precedence: The Order in Which Calculations Are Performed
    • Mathematical Functions
    • Format: The Way in Which Numbers Appear
    • Vectors in MATLAB
    • Initialising Vector Objects
    • Manipulating Vectors and Dot Arithmetic
    • Setting Up Mathematical Functions
    • Some MATLAB Specific Commands
    • Looking at Variables and Their Sizes
    • Accessing Elements of Arrays
    • Tasks
  • Chapter 2: Writing Scripts and Functions
    • Creating Scripts and Functions
    • Functions
    • Brief Aside
    • Plotting Simple Functions
    • Evaluating Polynomials and Plotting Curves
    • More on Plotting
    • Functions of Functions
    • Errors
    • Numerical Errors
    • User Error
    • Tasks
  • Chapter 3: Loops and Conditional Statements
    • Introduction
    • Loops Structures
    • Summing Series
    • Sums of Series of the Form Σjp,p∈N\Sigma j^p, p \in N
    • Summing Infinite Series
    • Summing Series Using MATLAB Specific Commands
    • Loops Within Loops (Nested)
    • Conditional Statements
    • Constructing Logical Statements
    • The MATLAB Command switch
    • Conditional loops
    • The break Command
    • MATLAB Specific Commands
    • Error Checking
    • Tasks
  • Chapter 4: Root Finding
    • Introduction
    • Initial Estimates
    • Fixed Point Iteration
    • Bisection
    • Newton-Raphson and Secant Methods
    • Derivation of the Newton-Raphson Method
    • Repeated Roots of Functions(*)
    • Zeros of Higher-Dimensional Functions(*)
    • MATLAB Routines for Finding Zeros
    • Roots of a Polynomial
    • The Command fzero
    • Tasks
  • Chapter 5: Interpolation and Extrapolation
    • Introduction
    • Saving and Reading Data
    • Which Points to Use?
    • Newton Forward Differences and Lagrange Polynomials
    • Linear Interpolation/Extrapolation
    • Calculating Interpolated and Extrapolated Values
    • Splines
    • Curves of Best Fit
    • Interpolation of Non-Smooth Data
    • Insufficient Data Points
    • Minimisation of Functions and Parameter Retrieval
    • Parameter Retrieval
    • Using fmins for Parameter Retrieval
    • Tasks
  • Chapter 6: Matrices
    • Introduction
    • Initialising Matrices Within MATLAB
    • Matrix Operations
    • Operations on Elements of Matrices
    • More on Special Matrices
    • Matrices Containing Strings
    • Properties of Matrices and Systems of Equations
    • Determinants of Matrices
    • Elementary Row Operations
    • Solving Many Equations at Once
    • Matrix Decomposition
    • Eigenvalues and Eigenvectors
    • Specific MATLAB Commands
    • Characteristic Polynomials
    • Exponentials of Matrices
    • Tasks
  • Chapter 7: Numerical Integration
    • Introduction
    • Integration Using Straight Lines
    • Errors in the Trapezium Method
    • Integration Using Quadratics
    • Integration Using Cubic Polynomials
    • Integrating Using MATLAB Commands
    • Specific Examples of Integrals
    • Infinite Integrals and Removable Singularities
    • Indefinite Integrals
    • Tasks
  • Chapter 8: Solving Differential Equations
    • Introduction
    • Euler's Method and Crank-Nicolson
    • Analytical Comparisons
    • Banded Matrices
    • Runge-Kutta Methods
    • Higher-Order Systems
    • Second-Order Systems
    • Bessel's equation
    • Airy's Equation
    • Shooting Methods
    • Boundary-Value Problems
    • Population Dynamics
    • Eigenvalues of Differential Systems
    • Tasks
  • Chapter 9: Simulations and Random Numbers
    • Introduction
    • Statistical quantities
    • Averages
    • Other Statistical Measures
    • Random Numbers and Distributions
    • Normal Distribution
    • Calculating Probabilities
    • Permutations
    • Maps and White Noise
    • Modelling Discrete Systems
    • Periodicity and Chaos
    • Random Motion
    • Tasks

Simple Calculations with MATLAB

Introduction and a Word of Warning
  • MATLAB is a powerful tool. You need to understand how it works and be precise when entering commands.
  • Small changes in commands can alter their meaning.
  • Learn the language and grammar/syntax to understand responses.
  • Formulate ideas clearly before translating them into MATLAB or any other language.
  • Begins with mathematical operations on scalars.
Scalar Quantities and Variables
  • MATLAB commands can be typed at the prompt.

  • MATLAB acts like a calculator starting with scalar operations.

  • Commands:

    • >> a = 3 (sets variable 'a' to 3)
    • >> b = 4; (sets variable 'b' to 4 and suppresses output)

    These can be read as:

    • "set a equal to 3"

    • "set b equal to 4 (and suppress output)"

    • Cannot have commands like 7 = x but can have x = 7.

    • Variables can be used again, e.g.,:

    • >> a = 3;

    • >> b = a+1;

    • >> x = a+b;

    • MATLAB operations:

    • Unary (operate on one quantity)

    • Binary (operate on two quantities)

    • Simple arithmetic operations are binary, e.g., 3*4.

    • MATLAB stores the result in the variable ans.

    • Commands can be typed on one line: a = 3; b = 4; x = a*b.

    • Division works similarly: 3/4 returns 0.75.

Rules for Naming of Variables
  1. Must start with a letter, up to 31 characters long. Trailing characters can be numbers, letters, or underscores.
  2. Case-sensitive (a and A are different).
  3. Use meaningful variable names.
  4. Variable names should not coincide with predefined MATLAB commands or user-defined subroutines. Use type variable_name or which variable_name to check if a variable name is already in use.
Precedence: The Order in Which Calculations Are Performed
  • A common source of errors.
  • a*(b+c) is the correct MATLAB command for a(b+c)a(b + c), whereas a*b+c is equal to ab+cab+c.
  • Brackets force MATLAB to perform calculations first, always appearing in pairs.
    • Examples:
    • (3+4/5) and (3+4)/5 are different.
    • a/b*c is not equal to abc\frac{a}{b c}. Use a/(b*c).
    • Example: Determine the value of the expression a(b+c(c+d))aa(b+c(c+ d))a, where a=2, b = 3, c = -4, and d = -3.
      >> a = 2; b = 3; c = −4; d = −3; >> a*(b+c*(c+d))*a
  • Intrinsic constants:
    • pi (π ≈ 3.14159265)
    • eps (distance from unity to the next real number in MATLAB).
    • Numbers can be entered in exponent-mantissa form:
    • Number = mantissa × 10^exponent
    • e.g., 789.34 = 7.8934e2
    • Intrinsic constants realmin (smallest positive number) and realmax (largest number).
Mathematical Functions
  • Arithmetic functions: +, -, /, and *.
  • Trigonometric functions: sin, cos, tan (inverses: asin, acos, atan). Use radians; results of inverse functions are in radians.
  • Exponential functions: exp, log (natural logarithm), log10, and ^.
  • Other functions:
    • round(x): Rounds to nearest integer.
    • ceil(x): Rounds up to nearest integer.
    • floor(x): Rounds down to nearest integer.
    • fix(x): Rounds towards zero.
    • rem(x,y): Remainder after division.
    • mod(x,y): Signed remainder after division.
    • abs(x): Absolute value.
    • sign(x): Sign of x.
    • factor(x): Prime factors of x.
  • Arguments for functions should be in round brackets, e.g., cos(x).
Format: The Way in Which Numbers Appear
S = [1/2 1/3 pi sqrt(2)];
format short; s
format long; s
format rat; s
format; s
  • Options:
    • short: 5 digits.
    • long: 15 digits.
    • rat: Represents the answer as a rational.
Vectors in MATLAB
  • Vectors are manipulated as MATLAB objects.
Initialising Vector Objects
  • Using the colon symbol:
    • r = 1:5; (sets 'r' to [1 2 3 4 5])
    • size(r) returns [1 5] (1 row, 5 columns).
    • r = a:b creates a vector from 'a' to 'b' in steps of 1.
    • r = a:h:b creates a vector from 'a' to 'b' in steps of 'h'.
  • Typing individual entries: t = [1 4 20 27 10];.
  • Using linspace:
    • s = linspace(0,1); (100 elements from 0 to 1).
    • t = linspace(0,1,10); (10 elements from 0 to 1).
Manipulating Vectors and Dot Arithmetic
  • Dot arithmetic manipulates vectors element-wise.
a = [1 2 3];
2*a;
% Output:
% ans =
% 2 4 6
a = [1 2 3];
b = [4 5 6];
a.*b
% Output:
% ans =
% 4 10 18
a = [1 2 3];
b = [4 5 6];
a./b
% Output:
% ans =
% 0.2500 0.4000 0.5000
Setting Up Mathematical Functions

Construct a vector x which contains the values from zero to one in steps of one tenth.

x = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0];
x = 0:0.1:1.0;
x = linspace (0,1,11);

Set up a simple mathematical function, say for instance y=x2y = x^2.

y=x. ^2

Example:

x = -1:0.1:1;
f = x+2;
g = x. ^3+1;
y = (f.^2).*(g);

Example:

x = 1:0.01:2;
f = x.^2;
g = x.^3+1;
y = f./g;
Some MATLAB Specific Commands

polyval takes coefficients of a polynomial and values at which to evaluate. Example:

x = 1:6;
c = [1 3 -1 -1];
y = polyval (c,x);

It is important that you remember to enter the coefficients of the polynomial starting with the one associated with the highest power and that zeros are included in the sequence.

Plotting is simple using the plot command:

x = linspace (-2,2,50);
c = [1 0 1 0 -1];
y = polyval (c,x);
plot(x,y)

roots finds roots of a polynomial.
poly takes the roots and generates the coefficients of the polynomial having those roots (which is monic, that is the coefficient of the highest term is unity).

Looking at Variables and Their Sizes

whos lists defined variables.
length gives the size of one variable.

Accessing Elements of Arrays

Elements of array x are recalled using x(1) through to x(11).

MATLAB command f(j) refers to the index within the array rather than the function f(.) evaluated at the value j!

Expression end is useful for referring to the final element.

Writing Scripts and Functions

To invoke the MATLAB editor type edit at the prompt.
The MATLAB editor also has the feature that once a piece of code has been run the values of variables can be displayed by placing the mouse close to the variable's location within the editor.
Scripts: simply a file containing the sequence of MATLAB commands which we wish to execute to solve the task at hand

The following code can be entered at the prompt, but that would defeat our purpose of writing script files. We shall therefore create our first script and save it in a file named twonums.m.

a = input('First number ');
b = input('Second number ');
disp([' Their sum is ' num2str(a+b)])
disp([' Their product is 'num2str(a*b)])

It is very important you give your files a meaningful name and that the files end with .m. You should avoid using filenames which are the same as the variables you are using and which coincide with MATLAB commands. Make sure you do not use a dot in the body of the filename and that it does not start with a special character or a number.

Functions

The first line of xsq.m tells us this is a function called xsq which takes an input called input and returns a value called output. The input is contained in round brackets, whereas the output is contained within square brackets.It is crucial for good practice that the name of the function xsq corresponds to the name of the file xsq.m (without the .m extension), although there is some flexibility here.

The second line of this function actually performs the calculation, in this case squaring the value of the input, and storing this result in the variable output. Notice that the function uses dot arithmetic. ^ so that this function will work with both vector and matrix inputs (performing the operation element by element).

Brief Aside

For those of you familiar with matrices we pause here and note that the command in the previous example will also work with matrices:
Also used the command sum (A,2)

Plotting Simple Functions

One of the most powerful elements of MATLAB is its excellent plotting facilities which allow us to easily and rapidly visualise the results of calculations.

Numerical Errors

Numerical methods are usually not exact (that is, they are approximate methods). In fact it is very hard to get computers to perform exact calculations.

Errors can be expressed as two basic types:

  1. Absolute error: This is the difference between the exact answer and the numerical answer.
  2. Relative error: This is the absolute error divided by the size of the answer (either the numerical one or the exact one), which is often converted to a percentage.

(In each case, we take the modulus of the quantity.)

Example: Estimate the error associated with taking 1.6 to be a root of the equation x2—x−1=0x^2 — x − 1 = 0.

User Error

The elimination of user errors is critical in achieving accurate results and more critical than numerical errors. The most severe user errors will often cause MATLAB to generate error messages.

  1. An incorrect use of variable names.
  2. An incorrect use of operators.
  3. Syntactical errors which still produce feasible MATLAB code.
  4. Mathematical errors incorporated into the numerical scheme the code seeks to implement.
  5. Logical errors in the algorithm.