0% found this document useful (0 votes)
2 views110 pages

Presentation MatlabModified

The document provides an overview of MATLAB, a high-level programming language for numerical computation and visualization, highlighting its functions, environment, and basic syntax. It covers various mathematical operations, including matrix manipulations, plotting, and decision-making structures, as well as commands for managing sessions and creating scripts. Additionally, it explains the creation and manipulation of vectors and matrices, including operations like addition, subtraction, and matrix multiplication.

Uploaded by

hagos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views110 pages

Presentation MatlabModified

The document provides an overview of MATLAB, a high-level programming language for numerical computation and visualization, highlighting its functions, environment, and basic syntax. It covers various mathematical operations, including matrix manipulations, plotting, and decision-making structures, as well as commands for managing sessions and creating scripts. Additionally, it explains the creation and manipulation of vectors and matrices, including operations like addition, subtraction, and matrix multiplication.

Uploaded by

hagos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 110

MATLAB Basic For

Advanced Power System


Analysis
Prepared by: Engr. Mylane A. Farolan
MATLAB (Matrix Laboratory)
 A fourth-generation high-level programming language and
interactive environment for numerical computation,
visualization and programming.
 MATLAB is developed by MathWorks.
 Functions:
◦ Matrix manipulations
◦ Plotting of functions and data
◦ Implementation of algorithms
◦ Creation of user interfaces
◦ Interfacing with programs written in other languages,
including C, C++, Java, and Fortran
◦ Analyze data; develop algorithms; and create models and
applications.
MATLAB's Power of Computational
Mathematics
 Mathematical calculations where MATLAB is used most
commonly:
 Dealing with Matrices and Arrays
 2-D and 3-D Plotting and graphics
 Linear Algebra
 Algebraic Equations
 Non-linear Functions
 Statistics
 Data Analysis
 Calculus and Differential Equations
 Numerical Calculations
 Integration
The MATLAB Environment - Desktop
Panels in the Desktop
 Current Folder - Allows you to access your project folders
and files.
 Command Window - This is the main area where you
enter commands at the command line, indicated by the
command prompt (>>).
 Workspace - The workspace shows all the variables you create
and/or import from files.
 Command History - This panels shows or rerun commands
that you entered at the command line.
MATLAB as a Super Complex Calculator: Basic Syntax
Basic Syntax: The use of Semicolon (;)
 Semicolon (;) indicates end of statement. However, if you
want to suppress and hide the MATLAB output for an
expression, add a semicolon after the expression.
Basic Syntax: Adding Comments
 The percent symbol (%) is used for indicating a
comment line.

 Some special expressions for some mathematical symbols, like pi


for π, Inf for ∞, i (and j) for √-1 etc. Nan stands for 'not a
number'.
Commonly used Operators and Special Characters
Commonly used Operators and Special Characters
Special Variables and Constants
Variables

• Once a variable is entered into the system, you can refer to


it later.
• Variables must have values before they are used.
Variables: The ‘who” and ‘clear’ commands
• The who command displays all the variable names you
have used.
• The clear command deletes all (or the specified)
variable(s) from the memory.
Long Assignments: The use of ‘ellipses (...)’

• Long assignments can be extended to another line by using


an ellipses (...).
The format Command
• By default, Matlab displays numbers with four decimal place
values. This is known as short format.
• However, if you want more precision, you need to use the
format command.
• The format long command displays 16 digits after decimal.
• The format bank command rounds numbers to two decimal
places.
• The format short e command allows displaying in exponential
form with four decimal places plus the exponent.

• The format long e command allows displaying in exponential


form with four decimal places plus the exponent.
• The format rat command gives the closest rational expression
resulting from a calculation.
Commands Used in Matlab
You can enter a command by typing it at the MATLAB prompt '>>'
on the Command Window.

Commands for Managing a Session:


Commands for Working with the System:
Commands for saving the current work in the workspace as a file
and loading the file later, displaying date, listing files in the
directory, displaying current directory, etc.
Input and Output Commands
Input and Output Commands
Vector, Matrix and Array Commands
Vector, Matrix and Array Commands
Vector, Matrix and Array Commands
Plotting Commands used for plotting graphs.
Plotting Commands
Plotting Commands
Plotting Commands
M-Files
Allows you to write series of commands into a file and execute the
file as complete unit.

Two kinds of program files:


• Scripts - script files are program files with .m extension.
• In these files, you write series of commands, which you want
to execute together.
• Scripts do not accept inputs and do not return any outputs.
• Functions - function files are also program files with .m
extension.
• Functions can accept inputs and return outputs.
Creating and Running Script File:
• To create scripts files, you need to use a text editor.
• You can open the Matlab editor in two ways:
• Using the command prompt
• Using the IDE

Creating and Running Script File Using the Command prompt:


• Type edit in the command prompt. This will open the editor. You
can directly type edit and then the filename (with .m extension)
Creating and Running Script File using the IDE:
• choose NEW -> Script.
• This opens the editor and creates a file named Untitled. You
can name and save the file after typing the code.
Example
Operators: Matlab allows the following types of operations:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operations
• Set Operations

Arithmetic Operators: Matlab allows two different types of


arithmetic operations:
• Matrix arithmetic operations
• Array arithmetic operations

• The matrix operators and array operators are differentiated


by the period (.) symbol.
• Addition and subtraction operation is same for matrices and
arrays, thus operator is same for both cases.
Relational Operators:
• Relational operators for arrays perform element-by-element
comparisons between two arrays and return a logical array of the
same size, with elements set to logical 1 (true) where the relation
is true and elements set to logical 0 (false) where it is not.
Relational Operators:
• Relational operators for arrays perform element-by-element
comparisons between two arrays and return a logical array of the
same size, with elements set to logical 1 (true) where the relation
is true and elements set to logical 0 (false) where it is not.
Logical Operators: Matlab offers two types of logical operators
and functions:
• Element-wise - these operators operate on corresponding
elements of logical arrays.
• Element-wise logical operators operate element-by-
element on logical arrays.
• The symbols &, |, and ~ are the logical array operators
AND, OR, and NOT.
• Short-circuit - these operators operate on scalar, logical
expressions.
• Short-circuit logical operators allow short-circuiting on
logical operations.
• The symbols && and || are the logical short-circuit
operators AND and OR.
Example
Bitwise Operations
• Bitwise operator works on bits and performs bit-by-bit operation.
• The truth tables for &, |, and ^ are as follows:
Example
Set Operations
• Set operations are union, intersection and testing for set
membership, etc.
Creating Vectors: Row vectors and Column vectors

Row vectors are created by enclosing the set of elements in square


brackets, using space or comma to delimit the elements.
Creating Vectors: Row vectors and Column vectors

Column vectors are created by enclosing the set of elements in


square brackets, using semicolon(;) to delimit the elements.
Creating Matrices: Row vectors and Column vectors

In Matlab, a matrix is created by entering each row as a sequence of


space or comma separated elements, and end of a row is demarcated
by a semicolon.
Referencing the Elements of a Vector: vectors can be
referenced in several ways.
A. The ith component of a vector v is referred as v(i).

B. Referencing a vector with a colon, such as v(:), all the


components of the vector are listed.
Selecting a range of elements from a vector:

In the example below, a row vector rv of 9 elements is


created. Then, elements 3 to 7 are referenced by writing rv
(3:7) and a new vector named sub_rv is created.
Addition and Subtraction of Vectors
Both the operand vectors must be of same type and have same
number of elements.
Example:
Transpose of a Vector
The transpose operation changes a column vector into a row
vector and vice versa. The transpose operation is represented
by a single quote(').
Example:
Transpose of a Vector
The transpose operation changes a column vector into a row
vector and vice versa. The transpose operation is represented
by a single quote(').
Example:
Magnitude of a Vector
Magnitude of a vector v with elements v1, v2, v3, …, vn, is given
by the equation:
|v| = √(v12 + v22 + v32 + … + vn2)
You need to take the following steps to calculate the magnitude of
a vector:
1. Take the product of the vector with itself, using array
multiplication (.*). The product is written as v.*v
2. Use the sum function to get the sum of squares of elements of
vector v also called the dot product of vector v. Written as
sum(v.*v)
3. Use the sqrt function to get the square root of the sum which is
also the magnitude of the vector v. Written as sqrt(sum(v.*v))
Example
Vector Dot Product
• Dot product of two vectors a = (a1, a2, …, an) and b = (b1,
b2, …, bn) is given by: a.b = Σ(ai.bi)
• Dot product of two vectors a and b is calculated using the
dot function: dot(a, b);
Example:
Vectors with Uniformly Spaced Elements
• It allows you to create a vector with uniformly spaced
elements.
• To create a vector v with the first element f, last element l,
and the difference between elements is any real number n,
is written as: v = [f : n : l]
Example:
Matrices
• A matrix is a two-dimensional array of numbers.
• To create a matrix, enter elements in each row as comma or
space delimited numbers and use semicolons to mark the
end of each row.
Example: Let us create a 4-by-5 matrix a
Referencing the Elements of a Matrix
• To reference an element in the mth row and nth column, of
a matrix, it is written as (m, n);

Example: For example, to refer to the element in the 2nd row


and 5th column, of the matrix a, it must be written as
Referencing the Elements of a Matrix
• To reference all the elements in the mth column, it is
written as (:,m).

Example: Let us create a column vector v, from the elements


of the 4th row of the matrix a:
Referencing the Elements of a Matrix
• In the same way, you can create a sub-matrix taking a sub-
part of a matrix.
Example: Create a sub-matrix sa taking the inner subpart of a:
Deleting a Row or a Column in a Matrix
• You can delete an entire row or column of a matrix by
assigning an empty set of square braces [] to that row or
column. Basically, [] denotes an empty array.
Example: Let us delete the fourth row of a:
Addition and Subtraction of Matrices
• You can add or subtract matrices. Both the operand
matrices must have the same number of rows and columns.
Example:
Division of Matrices
• You can divide two matrices using left (\) or right (/)
division operators. Both the operand matrices must have
the same number of rows and columns.
Example:
Scalar Operations of Matrices
• When you add, subtract, multiply or divide a matrix by a
number, this is called the scalar operation.
Example:
Transpose of a Matrix
• The transpose operation switches the rows and columns in
a matrix.
• It is represented by a single quote(').
Example:
Concatenating Matrices
• Concatenate two matrices to create a larger matrix. The pair
of square brackets '[]' is the concatenation operator.
• Two types of concatenations:
• Horizontal concatenation
• Vertical concatenation
Example:
Matrix Multiplication
• Two matrices A and B, if A is an m x n matrix and B is an n
x p matrix, they could be multiplied together to produce an
m x n matrix.
Example:
Determinant of a Matrix
• Determinant of a matrix is calculated using the det function.
• Determinant of a matrix A is given by det(A).
Example:
Inverse of a Matrix
• The inverse of a matrix A is denoted by such that the
following relationship holds
• Inverse of a matrix is calculated using the inv function.
Example:
Decision-Making Structures
 Figure on the right is the general form
of a typical decision making structure
found in most of the programming
languages.
 The programmer specify the
following:
◦ conditions to be evaluated or
tested,
◦ statement(s) to be executed if the
condition is determined to be true,
◦ and (optional) other statements to
be executed if the condition is
determined to be false.
Decision-Making Statements
 if … end statement
◦ If the expression evaluates to
true, then the block of code
inside the if statement will be
executed. If the expression
evaluates to false, then the
first set of code after the end
statement will be executed.
 if … else … end statement
◦ If the Boolean expression
evaluates to true, then the if
block of code will be
executed, otherwise else
block of code will be
executed.
 if…elseif…elseif…else…end statements

◦ An if statement can be followed by an (or more) optional


elseif... and an else statement, which is very useful to test
various condition.
◦ When using if... elseif...else statements, there are few points
to keep in mind:
 An if can have zero or one else's and it must come after
any elseif's.
 An if can have zero to many elseif's and they must come
before the else.
 Once an else if succeeds, none of the remaining elseif's or
else's will be tested.
 Nested if statements
◦ It is always legal in MATLAB to nest if-else statements
which means you can use one if or elseif statement inside
another if or elseif statement(s)
◦ You can nest elseif...else in the similar way as you have
nested if statement.
 Switch Statement

◦ A switch block conditionally executes one set of statements


from several choices. Each choice is covered by a case
statement.
◦ An evaluated switch_expression is a scalar or string.
◦ An evaluated case_expression is a scalar, a string or a cell array
of scalars or strings.
◦ The switch block tests each case until one of the cases is true.
◦ When a case is true, MATLAB executes the corresponding
statements and then exits the switch block.
◦ The otherwise block is optional and executes only when no
case is true.
Nested switch statements
It is possible to have a switch as part of the statement sequence of
an outer switch. Even if the case constants of the inner and outer
switch contain common values, no conflicts will arise.
Loops
 There may be a situation
when a block of code is
executed several number of
times.
 The first statement in a
function is executed first,
followed by the second, and
so on.
 A loop statement allows to
execute a statement or group
of statements multiple times
and following is the general
form of a loop statement in
most of the programming
languages
 While loop
◦ The while loop repeatedly executes program statement(s) as
long as the expression remains true.
◦ An expression is true when the result is nonempty and
contains all nonzero elements (logical or real numeric).
Otherwise, the expression is false.
 for loop
◦ A for loop is a
repetition control
structure that
allows you to
efficiently write a
loop that needs to
execute a specific
number of times.
 Nested loops
◦ MATLAB allows to use one loop inside another loop.
Following section shows few examples to illustrate the
concept.
 Loop Control Statements
◦ Loop control statements change execution from its normal
sequence.
◦ When execution leaves a scope, all automatic objects that were
created in that scope are destroyed.
◦ Break Statement and Continue statement are Loop controls.

 1. Break Statement
◦ The break statement terminates execution of for or while
loop. Statements in the loop that appear after the break
statement are not executed.
◦ In nested loops, break exits only from the loop in which it
occurs. Control passes to the statement following the end of
that loop.
 2. Continue Statement
◦ The continue statement is
used for passing control to
next iteration of for or
while loop.
◦ The continue statement in
MATLAB works
somewhat like the break
statement. Instead of
forcing termination,
however, 'continue' forces
the next iteration of the
loop to take place,
skipping any code in
between.

You might also like