You are on page 1of 73

Training Workshop On

MATLAB / Simulink
RYAN D. REAS
Training Program
Day 1 Morning – Introduction to Matlab
◦ Fundamentals In Matlab
◦ Basic Plotting
◦ Matrix / Array Generation and Handling
◦ Array Operations and Linear Functions
◦ M-File Scripts and Functions
◦ Control Flow and Operators
◦ Debugging M-Files
Day 1 Afternoon – Numerical Analysis using Matlab
◦ Mathematical functions in Matlab
◦ Root finding algorithm in Matlab
◦ Optimization in Matlab
◦ Gauss-Seidel Solution of Systems of Linear Equation in Matlab
◦ Optimization & Curve Fitting Matlab Application
Training Program
Day 2 Morning – Digital Signal Processing with Matlab/Simulink
◦ Digital Systems Analysis in Matlab
◦ Signal Analysis in Matlab
◦ Development of Digital System Simulation Model in Simulink
◦ Time and Frequency Analysis of Digital Systems in Simulink
◦ Filter Designer and Filter builder tool
◦ Realtime Signal Analysis in Simulink
◦ Acoustic Noise Cancellation in Simulink
Day 2 Afternoon – Control System Analysis and Design in Matlab/Simulink
◦ Systems Analysis in Matlab
◦ Development of Continuous System Simulation Model in Simulink
◦ System Identification tool and system validation
◦ Linear System Analyzer
◦ Control System Designer and Control System Tuner
◦ PID Tuner tool
Introduction to Matlab
Matlab
Toolbars

Matlab
Directory MatScript
Workspace

Matlab Command Window


Matlab Command Window
The MATLAB Command Window is the main window where you type commands
directly to the MATLAB interpreter.

For example
>> 1+2*3
ans =
7
>> x = 1+2*3
x=
7
Matlab Editor
The MATLAB Editor Window is a simple text editor where you can
load, edit and save complete MATLAB programs.
Getting Started With Matlab
Creating MATLAB variables
MATLAB variables are created with an assignment statement. The syntax of variable assignment
is
variable name = a value (or an expression)

>> x = expression
where expression is a combination of numerical values, mathematical operators, variables, and
function calls. On other words, expression can involve:
1. manual entry
2. built-in functions
3. user-defined functions
Getting Started With Matlab
Overwriting variable
Once a variable has been created, it can be reassigned. In addition, if you do not wish to see the
intermediate results, you can suppress the numerical output by putting a semicolon

Controlling the hierarchy of operations or precedence


The order in which MATLAB performs arithmetic operations is exactly that taught in high school
algebra courses. Exponentiations are done first, followed by multiplications and divisions, and
finally by additions and subtractions.
Getting Started With Matlab
Hierarchy of arithmetic operations
Matlab Mathematical Functions
MATLAB offers many predefined mathematical functions for technical computing which contains
a large set of mathematical functions. Typing help elfun and help specfun calls up full lists of
elementary and special functions respectively
Matlab Mathematical Functions
Example
𝑦 = 𝑒 −𝑎 sin 𝑥 + 10 𝑦 for a = 5; x = 2; y = 8

ln 142

log10 142
Basic Plotting
The basic MATLAB graphing procedure, for example in 2D, is to take a vector of xcoordinates, x =
(x1, . . . , xN ), and a vector of y-coordinates, y = (y1, . . . , yN ), locate the points (xi, yi), with i = 1,
2, . . . , n and then join them by straight lines.
Basic Plotting
EXAMPLE
Multiple data sets in one plot
Plot Attirbutes
Matrix in Matlab
Matrices are the basic elements of the MATLAB environment. A matrix is a two-
dimensional array consisting of m rows and n columns. Special cases are column
vectors (n = 1) and row vectors (m = 1).
Creating a Matrix
A vector is a special case of a matrix. The purpose of this section is to show how to create
vectors and matrices in MATLAB. As discussed earlier, an array of dimension 1 ×n is called a row
vector, whereas an array of dimension m × 1 is called a column vector. The elements of vectors
in MATLAB are enclosed by square brackets and are separated by spaces or by commas. For
examples,
Row vector Column Vector
Steps in creating Matrix in Matlab
Matrix indexing
Use parenthesis to access elements of matrix

w(1) w(1,1)
v(1) v(2) v(3) v(4) v(5) w(2) w(2,1)
v(1,1) v(1,2) v(1,3) ….
w(3) w(3,1)

w(4) w(4,1)

w(5) w(5,1)
Matrix indexing
We select elements in a matrix just as we did for vectors, but now we need two indices. The
element of row i and column j of the matrix A is denoted by A(i,j). Thus, A(i,j) in MATLAB refers
to the element A(i,j) of matrix A. The first index is the row number and the second index is the
column number.
A(1,1) A(1,3)

A(3,1) A(3,3)
Accessing Block of Elements
To access blocks of elements, we use MATLAB’s colon notation
Accessing Block of Elements
To access blocks of elements, we use MATLAB’s colon notation

A(:,1) A(1,2:3)

A(2:end,3)
Colon operator in a matrix
Deleting rows and columns
To delete a row or column of a matrix, use the empty vector operator, [ ]. Can not be deleted

Deleted
Some elementary matrix operator
Matrix & Array Operators
Matrix Operators

Array Operators
Matrix & Array Operators

Matrix Operators Array Operators


Matrix & Array Operators
Solving Systems of Linear Equation
Other Matrix functions
Matlab Functions (M-File)
Are programs (or routines) that accept input arguments and return output arguments. Each M-
file function (or function or M-file for short) has its own area of workspace, separated from the
MATLAB base workspace.
Matlab Functions vs MatScript
Matlab Functions Input and Output Arguments

Function file can have none, one, or several output arguments. Table 4.3
illustrates some possible combinations of input and output arguments.
Input to a script file
When a script file is executed, the variables that are used in the calculations within the file must
have assigned values. The assignment of a value to a variable can be done in three ways.

Input(PROMPT) displays the PROMPT string on the screen, waits for input from the keyboard,
evaluates any expressions in the input, and returns the value in RESULT.
>>> [variable] = input [‘ STRING ‘ [value]]
Input to a script file
Example
Input to a script file
Example
Control flow and operators
Like other computer programming languages, MATLAB has some decision making structures for
control of command execution. These decision making or control flow structures include for
loops, while loops, and if-else-end constructions. Control flow structures are often used in script
M-files and function M-files.

MATLAB has four control flow structures: the if statement, the for loop, the while loop, and the
switch statement.
▪If-else statement
▪For Loop
▪While Loop
▪Switch
The ‘‘if...end’’ structure
• if ... end • if ... elseif ... else ... end

• if ... else ... End


The ‘‘if...end’’ structure
It should be noted that:
Relational and logical operators
The ‘‘for...end’’ loop
In the for ... end loop, the execution of a command is repeated at a fixed and
predetermined number of times. The syntax is
The ‘‘for...end’’ loop
Multiple for loops can be nested, in which case indentation helps to improve the
readability. The following statements form the 5-by-5 symmetric matrix A with
(i,j) element i/j for j ≥ i:
The ‘‘while...end’’ loop
This loop is used when the number of passes is not specified. The looping continues until a
stated condition is satisfied. The while loop has the form:
Other flow structures
Practice Problem
1. Develop a matlab function that determines the Fibonacci number of an nth
element.
NUMERICAL ANALYSIS
WITH MATLAB
RYAN D. REAS
User-Defined Anonymous Mathematical Functions
If we wish to deal with a function that is a combination of the built-in functions, Matlab has a
couple of ways for the user to define functions. One that we will use a lot is the anonymous
function, which is a way to define a function in the command window. The following is a typical
anonymous function:

This produces the function f(x) = 2x2 − 3x + 1. To obtain a single value of this function enter
User-Defined Anonymous Mathematical Functions
Just as for built-in functions, the function f as we defined it can operate not only on single
numbers but on vectors. Try the following:
Taylor’s Series
Taylor series of a function is an infinite sum of terms that are expressed in terms of the
function's derivatives at a single point. For most common functions, the function and the sum of
its Taylor series are equal near this point. Taylor's series are named after Brook Taylor, who
introduced them in 1715
Parameters of Taylor’s Series in Matlab
Taylor’s Series Example
Taylor’s Series Example
Newton - Raphson’s Method Root Finding Algorithm
In numerical analysis, Newton's method, also known as the Newton–Raphson method, named
after Isaac Newton and Joseph Raphson, is a root-finding algorithm which produces successively
better approximations to the roots (or zeroes) of a real-valued function. The most basic version
starts with a single-variable function f defined for a real variable x, the function's derivative f ′,
and an initial guess x0 for a root of f. If the function satisfies sufficient assumptions and the
initial guess is close, then

xr
Newton - Raphson’s Method Root Finding Algorithm
Newton - Raphson’s Method Root Finding Algorithm
Bisection Method Root Finding Algorithm
The bisection method is a root-finding method that applies to any continuous functions for
which one knows two values with opposite signs. The method consists of repeatedly bisecting
the interval defined by these values and then selecting the subinterval in which the function
changes sign, and therefore must contain a root. It is a very simple and robust method, but it is
also relatively slow. Because of this, it is often used to obtain a rough approximation to a
solution which is then used as a starting point for more rapidly converging methods. The method
is also called the interval halving method, the binary search method, or the dichotomy method.
Bisection Method Root Finding Algorithm
Newton’s Method of Optimization
Newton's method can be applied to the derivative f ′ of a twice-differentiable function f to find
the roots of the derivative (solutions to f ′(x) = 0), also known as the critical points of f. These
solutions may be minima, maxima, or saddle points; see section "Several variables" in Critical
point (mathematics) and also section "Geometric interpretation" in this article. This is relevant
in optimization, which aims to find (global) minima of the function f.
Newton’s Method of Optimization
Newton’s Method of Optimization
Golden Section Search Optimization
The golden-section search is a technique for finding an extremum (minimum or maximum) of a
function inside a specified interval. For a strictly unimodal function with an extremum inside the
interval, it will find that extremum, while for an interval containing multiple extrema (possibly
including the interval boundaries), it will converge to one of them. If the only extremum on the
interval is on a boundary of the interval, it will converge to that boundary point. The method
operates by successively narrowing the range of values on the specified interval, which makes it
relatively slow, but very robust. The technique derives its name from the fact that the algorithm
maintains the function values for four points whose three interval widths are in the ratio 2-
φ:2φ-3:2-φ where φ is the golden ratio
Golden Section Search Optimization
Gauss-Seidel Solution to Systems of Linear Equation
Iterative or approximate methods provide an alternative to the elimination methods described
to this point. Such approaches are similar to the techniques we developed to obtain the roots of
a single equation
Gauss-Seidel Solution to Systems of Linear Equation
Curve Fitting Toolbox in Matlab
Curve Fitting Toolbox™ provides an app and functions for fitting curves and surfaces to
data. The toolbox lets you perform exploratory data analysis, preprocess and post-
process data, compare candidate models, and remove outliers. You can conduct
regression analysis using the library of linear and nonlinear models provided or specify
your own custom equations. The library provides optimized solver parameters and
starting conditions to improve the quality of your fits. The toolbox also supports
nonparametric modeling techniques, such as splines, interpolation, and smoothing.
Curve Fitting Toolbox in Matlab
Curve Fitting Toolbox in Matlab
polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1

Polyfit in Matlab
polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit
(in a least-squares sense) for the data in y. The coefficients in p are in descending
powers, and the length of p is n+1
polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1

Polyfit in Matlab
polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1

Polyfit in Matlab
polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1

Polyfit in Matlab
Fit a curve or surface to data
polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1

Polyfit in Matlab
polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1

Fit in Matlab
polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1

Exercise / Practice Problem


1. Using Taylor’s series, determine the minimum degree of approximation to
approximate a one complete cycle of a sinusoidal waveform

2. Modify the codes shown in determining the roots of the equation using Newton’s
Raphson method in order to avoid having an approximate of xi that will result to f’(xi) =
0.

3. Modify the codes shown in Golden Section Search in way that a user may choose
what to locate, either the minima or maxima of the function f(x)

4. Forecast the electric consumption of a certain household for the next month, using
the historical data collected for the past fifty months

You might also like