MATLAB Guide for Numerical Methods
Numerical Methods for Engineers (ENG-308)
February 16, 2025
1 Introduction
MATLAB (Matrix Laboratory) is a powerful programming language widely used
in engineering, mathematics, and numerical computing. This guide will help you
quickly start using MATLAB for numerical methods.
2 Getting Started with MATLAB
2.1 Launching MATLAB
To start MATLAB, open the application and navigate to the Command Win-
dow. You can execute commands directly or create scripts using the Editor.
2.2 MATLAB online
If MATLAB is not installed on your computer, you can use MATLAB Online,
a cloud-based version accessible through a web browser. Simply visit MATLAB
Online and sign in with a MathWorks account. This allows you to run scripts,
perform computations, and access MATLAB features without requiring local
installation, making it a convenient freeware option.
2.2.1 How to use MATLAB online?
Step 1: Create a MathWorks Account
• Go to https://www.mathworks.com/login
• Click ”Create Account” and sign up using your email.
• Verify your email and log in.
Step 2: To use MATLAB online
• Open the link: https://matlab.mathworks.com
• Login to your Mathworks account and start using MATLAB.
1
2.3 Getting started
The MATLAB interface consists of:
• Command Window: Directly execute commands.
• Editor: Write and save scripts (“.m“ files).
• Workspace: View stored variables.
• Figure Window: Displays plots and visualizations.
2.4 Helpful Commands to Start
• Run the demo program: demo
• Get help on a command: help <command>
• Search for related commands: lookfor <keyword>
2.5 Basic Commands
The command window is used to perform calculations, such as:
1 >> a = 5 + 3 ; % Addition
2 >> b = s q r t ( 1 6 ) ; % Square r o o t
3 >> c = s i n ( p i / 4 ) ; % T r i g o n o m e t r i c f u n c t i o n
The following commands may be used to clear command window/variables:
>> clc to clear the command window.
>> clear to remove variables.
3 Scripts and Functions
3.1 Writing a function on the command line
You may define a function in the command line in the following way: ” =@(x)
”, for example, to define h(x) = x2 + 1 we can write:
1 >>h=@( x ) x ˆ2+1;
and use it for example as:
1 >>h ( 2 )
2
3.2 Defining a Function as a script
Functions in MATLAB are defined using the function keyword. Create a script
in the Editor and save it as myFunction.m:
1 function y = myFunction ( x )
2 y = x ˆ2 + 2∗ x + 1 ;
3 end
Call it in the command window:
1 >> r e s u l t = myFunction ( 3 ) ;
2 >> d i s p ( r e s u l t ) ;
4 Plotting and Visualization
MATLAB provides powerful plotting tools. Example:
1 x = 0 : 0 . 1 : 2 ∗ pi ;
2 y = sin (x );
3 plot (x , y ) ;
4 t i t l e ( ’ S i n e Wave ’ ) ;
5 xlabel ( ’x ’ ); ylabel ( ’ sin (x) ’ );
or more general:
1 x = 0:0.1:10;
2 y1 = s i n ( x ) ;
3 y2 = c o s ( x ) ;
4 p l o t ( x , y1 , ’ r ’ )
5 h o l d on
6 p l o t ( x , y2 , ’ b−− ’ ) ;
7 legend ( ’ sin (x) ’ , ’ cos (x) ’ ) ;
8 x l a b e l ( ’ x ’ ) ; y l a b e l ( ’ Function v a l u e s ’ ) ;
9 t i t l e ( ’ S i n e and C o s i n e F u n c t i o n s ’ ) ;
10 g r i d on ;
In fact, MATLAB has several built-in functions such as:
exp(x), cos(x), sqrt(x), log(x)
5 Debugging in MATLAB
To debug:
• Use dbstop if error to pause at errors.
• Add breakpoints in the Editor.
• Step through code using the Debug toolbar.
3
6 Number Precision and Formatting
MATLAB operates in double precision by default. Formatting options:
• Default: format short
• Full precision: format long
• Scientific notation: format short e or format long e
7 Arrays in MATLAB
MATLAB works efficiently with arrays. Example: plotting sin(x) and cos(x)
over 0 ≤ x ≤ 10:
1 t = 0:0.1:10;
2 x = cos ( t ) ; y = sin ( t ) ;
3 plot ( t , x , t , y ) ;
7.1 Vectors/arrays
A row vector is created using:
1 >> R = 1 : 5
which results in:
R= 12345
It is transformed to a vertical array (column) using the transpose operation:
1 >> V = n ’
which results in:
1
2
3
V =
4
5
7.2 Matrix Creation
A matrix is input as:
1 A = [1 2 3; 4 5 6; 7 8 9 ] ;
which produces:
1 2 3
A = 4 5 6
7 8 9
4
8 Array Operations
• Addition: C = A + B
• Scalar multiplication: D = 2 ∗ A
• Matrix multiplication: G = E ∗ F
Component-wise operations use a dot (.):
1 a .∗ b % Element−w i s e m u l t i p l i c a t i o n
2 a ./ b % Element−w i s e d i v i s i o n
3 a .ˆ 3 % Element−w i s e e x p o n e n t i a t i o n
9 Special Arrays
MATLAB provides built-in functions for generating special matrices:
1 zeros ( 2 , 3 ) % 2 x3 z e r o m a t r i x
2 ones ( 2 , 3 ) % 2 x3 m a t r i x f i l l e d w i t h ones
3 eye ( 3 ) % 3 x3 i d e n t i t y m a t r i x
10 Array Functions
MATLAB has functions that operate on arrays:
• max(x) - Maximum element of x
• min(x) - Minimum element of x
• abs(x) - Absolute values
• sum(x) - Sum of elements
• norm(x) - Euclidean norm
5
11 Numerical Methods in MATLAB
11.1 Bisection Method
Finding the root of f (x) = 0 by iteratively dividing an interval in half.
Listing 1: Bisection Method Implementation
1 function r o o t = b i s e c t i o n ( f , a , b , t o l )
2 i f f (a) ∗ f (b) > 0
3 e r r o r ( ’No s i g n change d e t e c t e d . B i s e c t i o n method r e q u i r e s a r o o t . . .
4 in the i n t e r v a l . ’ ) ;
5 end
6 while ( b − a ) / 2 > t o l
7 c = (a + b) / 2;
8 i f f ( c ) == 0
9 break ;
10 elseif f (a) ∗ f (c) < 0
11 b = c;
12 else
13 a = c;
14 end
15 end
16 root = (a + b) / 2;
17 end
11.2 Fixed-Point Iteration
Fixed-point iteration to solve x = g(x) using:
xn+1 = g(xn )
Listing 2: Fixed-Point Iteration Implementation
1 function r o o t = f i x e d p o i n t ( g , x0 , t o l , m a x i t e r )
2 x = x0 ;
3 for i = 1 : max iter
4 x new = g ( x ) ;
5 i f abs ( x new − x ) < t o l
6 r o o t = x new ;
7 return ;
8 end
9 x = x new ;
10 end
11 e r r o r ( ’ Fixed−p o i n t i t e r a t i o n d i d not c o n v e r g e . ’ ) ;
12 end
6
11.3 Newton-Raphson Method
Given an initial guess x0 , keep refining the root iteratively as:
f (xn )
xn+1 = xn −
f ′ (xn )
Listing 3: Newton-Raphson Method Implementation
1 function r o o t = newton ( f , df , x0 , t o l )
2 x = x0 ;
3 for i = 1:100
4 x new = x − f ( x ) / d f ( x ) ;
5 i f abs ( x new − x ) < t o l
6 r o o t = x new ;
7 return ;
8 end
9 x = x new ;
10 end
11 e r r o r ( ’ Newton−Raphson d i d not c o n v e r g e . ’ ) ;
12 end
12 Other possible codes (Exercise)
Try implementing the following:
1. Modify the Newton-Raphson method to include a stopping condition based
on |f (xn )| < tolerance.
2. Implement the Secant Method for solving f (x) = 0.
7
13 Practice Exercises
13.1 Basic MATLAB Commands
Exercise 1: Compute the following using MATLAB.
√
1. Compute 25 + 50.
2. Find the sine and cosine of 30◦ (convert degrees to radians).
3. Define a variable x = 10 and compute y = x2 + 3x − 5.
13.2 Working with Arrays
Exercise 2: Create the following arrays and perform operations.
1. Define a row vector v = [1, 2, 3, 4, 5].
2. Define a column vector u = [5; 4; 3; 2; 1].
1 2 3
3. Create a 3 × 3 matrix A = 4 5 6.
7 8 9
4. Extract the second row of A.
5. Compute the element-wise multiplication of v and u (hint: use .*).
13.3 Plotting in MATLAB
Exercise 3: Generate a plot using MATLAB.
1. Define x as values from 0 to 3 with a step size of 0.1.
2. Compute y = 5 ln(x + 1) and z = x2 − 1.
3. Plot both functions on the same graph using different colors and line styles.
4. Add labels for the x-axis and y-axis, a title, and a legend.
13.4 Writing MATLAB Functions
Exercise 4: Write a MATLAB function to evaluate a quadratic polynomial.
1. Create a function quad eval that takes a, b, c, x as inputs and computes
f (x) = ax2 + bx + c.
2. Test the function for a = 1, b = −3, c = 2, x = 4.