0% found this document useful (0 votes)
3 views

MEU 07254 Technical Computing With Matlab

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

MEU 07254 Technical Computing With Matlab

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

2022-05-12

MEU 07254: TECHNICAL


COMPUTING WITH
MATLAB
INSTRUCTOR: DR. NGONGI, W. E.
LECTURE 1

Overview
• Engineering/Scientific calculations are: Involving,
Repetitive and Complex.
– So we need a powerful tool to deal with it.
• Tools include:
– Mathcad: (https://www.mathcad.com/)
– Maple: (https://www.maplesoft.com/)
– Mathematica: (https://www.wolfram.com/)
– MATLAB: (https://www.mathworks.com/)
– Etc
• Matlab performs complex calculations (advanced
calculator), has a programming environment,
graphical data plotting and other useful features.
• MATLAB is developed by MathWorks and stands for
MATrix LABoratory.

1
2022-05-12

MATLAB's Power of Computational


Mathematics
• MATLAB is very flexible and usable
for solving problems in various
fields including:
– Applied Mathematics
– Physics
– Chemistry
– Engineering
– Medicine
– Finance
– Complicated numerical calculations

Features of MATLAB …
• Matlab features a family of applications
specific calculations called Toolboxes.
• Toolboxes are comprehensive collection of
MATLAB functions that extend MATLAB
environment to solve particular problems.
• MATLAB software comes in versions.
Particularly there are version 5 (R12), 6 (R13),
7 (R14), R2006a, R2006b, etc.

2
2022-05-12

MATLAB Toolboxes
• Toolboxes include:
• Control System Toolbox
• Communication Toolbox
• Filter Design Toolbox
• Financial Toolbox
• Fuzzy Logic Toolbox
• Image Processing Toolbox
• Simulink
• LMI Control Toolbox
• Statics Toolbox

MATLAB Toolboxes …
• Neural Network Toolbox
• Signal Processing Toolbox
• Wavelet Toolbox
• Optimization Toolbox
• Robust Control Toolbox
• Mapping Toolbox
• Etc

3
2022-05-12

MATLAB - Environment Setup


• Local Environment Setup
• Setting up MATLAB environment is a matter of
few clicks.
• MathWorks provides the licensed product, a trial
version and a student version as well.
• You need to log into the site and wait a little for
their approval.
• After downloading the installer the software can
be installed through few clicks.

Local Environment Setup

4
2022-05-12

Local Environment Setup …

Understanding the MATLAB


Environment
• MATLAB can be launched from the icon
created on the desktop during installation.
• The main working window in MATLAB is called
the desktop.
• When MATLAB is started, the desktop
appears in its default layout.
• Possible windows include: Command window,
current directory, launch pad, command
history and workspace.

5
2022-05-12

Understanding the MATLAB


Environment

The desktop has the following


panels:
• Current Folder − This panel allows you to
access the project folders and files.

6
2022-05-12

• Command Window − This is the main area


where commands are entered at the command
line. It is indicated by the command prompt
(>>).

• Workspace − The workspace shows all the


variables created and/or imported from files.

7
2022-05-12

• Command History − This panel shows or


return commands that are entered at the
command line.

MATLAB - Basic Syntax


• You can enter commands at the >> command
prompt.
• MATLAB provides some special expressions for
some mathematical symbols, like pi for π, Inf
for ∞, i (and j) for √-1 etc. Nan stands for 'not
a number'.
• MATLAB commands are case sensitive, eg CLC
is not the same as clc.

8
2022-05-12

Use of Semicolon (;) in MATLAB


• 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.
• For example,
>>x = 3;
>>y = x + 5
y= 8

Colon (:) operator


• Colon can be used to create a vector. For
example: x=[1:1:10]; creates a row vector with
elements 1,2,3,…10.
– Syntax: vectorname =
[firstvalue:increament:lastvalue].
• Increment can be +ve or –ve.
• If increment is omitted then the increment by 1 is
assumed.
• You can create a matrix from vectors of the same
length. Example: x=[0:2:20]; y=[x’, sin(x*pi/180)’]

9
2022-05-12

Colon (:) operator


y=

0 0
2.0000 0.9971
4.0000 0.1525
6.0000 -0.9737
8.0000 -0.3015
10.0000 0.9276
12.0000 0.4434
14.0000 -0.8598
16.0000 -0.5750
18.0000 0.7718
20.0000 0.6931

Adding Comments
• The percent symbol (%) is used to indicate a
comment line. For example,
• x=9 % assign the value 9 to x
• ver: displays version of MATLAB installed
other toolboxes and their versions.
• version: displays version of MATLAB only.
• help: to get help topics. It gives list of help
topics
• helpwin: gives details of each help topic.

10
2022-05-12

Numbers
• Can be integer or real numbers.
• Power of 10 is represented by using e or E (eg.
1.45x103 use 1.45e3 or 1.45E3). For negative
power use negative sign (eg. 1.45e-3).
• Numbers are represented to approximately
16 significant figures.
• Use realmax and realmin to determine the
max and min numbers.

Commonly used Operators and


Special Characters
• MATLAB supports the following commonly
used operators and special characters −
Operator Purpose
+ Plus; addition operator.
- Minus; subtraction operator.
* Scalar and matrix multiplication operator.
.* Array multiplication operator.
^ Scalar and matrix exponentiation operator.
.^ Array exponentiation operator.

11
2022-05-12

Commonly used Operators and


Special Characters …
Operator Purpose

\ Left-division operator.

/ Right-division operator.

.\ Array left-division operator.

./ Array right-division operator.

: Colon; generates regularly spaced elements and


represents an entire row or column.
() Parentheses; encloses function arguments and array
indices; overrides precedence.

Commonly used Operators and


Special Characters …
Operator Purpose
[] Brackets; enclosures array elements.
. Decimal point.
, Comma; separates statements and elements in
a row
; Semicolon; separates columns and suppresses
display.
% Percent sign; designates a comment and
specifies formatting.

12
2022-05-12

Special Variables and Constants


• MATLAB supports the following special
variables and constants −
Name Meaning
ans Most recent answer.
eps Accuracy of floating-point precision.
i,j The imaginary unit √-1.
Inf Infinity.
NaN Undefined numerical result (not a number).
pi The number π

MATLAB special Functions


• Sqrt(x) – square root of x
• abs(x) – absolute of x (return magnitude in case of
complex number)
• exp(x) – ex
• log(x) – natural logarithm of x (i.e. lnx)
• log10(x) – log10(x)
• max(x), min(x), mean(x), std(x) (where x is matrix.
• rem(x,y) – returns reminder when x is divided by
y.
• sin(x), cos(x), tan(x), asin(x), acos(x), etc.

13
2022-05-12

Naming Variables
• Variable names consist of a letter followed by
any number of letters, digits or underscore.
• MATLAB is case-sensitive.
• Variable names can be of any length, however,
MATLAB uses only first N characters, where N
is given by the function namelengthmax.

Saving Your Work


• The save command is used for saving all the
variables in the workspace, as a file with .mat
extension, in the current directory.
• For example,
save myfile
• You can reload the file anytime later using the
load command.
load myfile

14
2022-05-12

MATLAB - Variables
• In MATLAB environment, every variable is an array or
matrix.
• You can assign variables in a simple way. For example,
x = 3 % defining x and initializing it with a value
• MATLAB will execute the above statement and return
x=3
• It creates a 1-by-1 matrix named x and stores the value
3 in its element.
x = sqrt(16) % defining x and initializing it with
an expression

Rules about variable names


• Is a case sensitive (eg X is not the same as x)
• It must be a single name containing no space. Use
underscore (_) instead of space.
• It must start with a letter (small or capital)
followed by numbers or letter (digits 0 – 9).
• Can contain up to 63 characters (use
namelengthmax).
• Can not be reserved words such as if, for, elseif,
end, while, function, return, etc). Error if
reserved words are used.
• use iskeyword to get list of reserved words.

15
2022-05-12

Multiple Assignments
• You can have multiple assignments on the same
line. For example,
a = 2; b = 7; c = a * b
c = 14
• The clear command deletes all (or the specified)
variable(s) from the memory.
clear x % it will delete x, won't display
anything
clear % it will delete all variables in the
workspace

MATLAB Commands
• Now we will provide lists of commonly used
general MATLAB commands.

Command Purpose
clc Clears command window.
clear Removes variables from memory.
exist Checks for existence of file or variable.
global Declares variables to be global.
help Searches for a help topic.

16
2022-05-12

MATLAB Commands …

Command Purpose

lookfor Searches help entries for a


keyword.
quit Stops MATLAB.

who Lists current variables.

whos Lists current variables (long


display).

Commands for Working with the


System
Command Purpose
cd Changes current directory.
date Displays current date.
delete Deletes a file.
dir Lists all files in current directory.
load Loads workspace variables from a file.
save Saves workspace variables in a file.
type Displays contents of a file.

17
2022-05-12

Input and Output Commands


• MATLAB provides the following input and
output related commands: −
Command Purpose
disp Displays contents of an array or string.
fscanf Read formatted data from a file.
format Controls screen-display format.
fprintf Performs formatted writes to screen or file.
input Displays prompts and waits for input.
; Suppresses screen printing.

Input and Output Commands …


• The fscanf and fprintf commands behave like
C scanf and printf functions.
• They support the following format codes −
Format Purpose
Code
%s Format as a string.

%d Format as an integer.

%f Format as a floating point value.

%e Format as a floating point value in scientific


notation.

18
2022-05-12

Input and Output Commands …

Format Code Purpose


%g Format in the most compact
form: %f or %e.
\n Insert a new line in the output
string.
\t Insert a tab in the output
string.

Vector, Matrix and Array Commands


• The following table shows various commands
used for working with arrays, matrices and
vectors −
Command Purpose
find Finds indices of nonzero elements.
length Computes number of elements.
linspace Creates regularly spaced vector.
logspace Creates logarithmically spaced vector.
max Returns largest element.

19
2022-05-12

Vector, Matrix and Array Commands …


Command Purpose
min Returns smallest element.
prod Product of each column.
reshape Changes size.
size Computes array size.
sort Sorts each column.
sum Sums each column.
eye Creates an identity matrix.
ones Creates an array of ones.
zeros Creates an array of zeros.

Vector, Matrix and Array Commands …


cross Computes matrix cross
products.
dot Computes matrix dot
products.
det Computes determinant of an
array.
inv Computes inverse of a
matrix.
pinv Computes pseudoinverse of
a matrix.
rank Computes rank of a matrix.

20
2022-05-12

Vector, Matrix and Array Commands



rref Computes reduced row echelon
form.
cell Creates cell array.
celldisp Displays cell array.
cellplot Displays graphical representation of
cell array.
num2cell Converts numeric array to cell array.
deal Matches input and output lists.
iscell Identifies cell array.

Plotting Commands
• MATLAB provides several commands for
plotting graphs.
• The following table shows some of the
commonly used commands for plotting −
Command Purpose
axis Sets axis limits.
fplot Intelligent plotting of functions.
grid Displays gridlines.

21
2022-05-12

Plotting Commands …
Command Purpose
plot Generates xy plot.
print Prints plot or saves plot to a
file.
title Puts text at top of plot.
xlabel Adds text label to x-axis.
ylabel Adds text label to y-axis.
axes Creates axes objects.
close Closes the current plot.

Plotting Commands …
Command Purpose
close all Closes all plots.
figure Opens a new figure window.
gtext Enables label placement by
mouse.

hold Freezes current plot.


legend Legend placement by mouse.

22
2022-05-12

Plotting Commands …
Command Purpose
subplot Creates plots in subwindows.
text Places string in figure.
bar Creates bar chart.
loglog Creates log-log plot.
polar Creates polar plot.

MATLAB - Operators
• An operator is a symbol that tells the compiler to
perform specific mathematical or logical
manipulations.
• Operators in MATLAB work both on scalar and
non-scalar data.
• MATLAB allows the following types of elementary
operations −
– Arithmetic Operators
– Relational Operators
– Logical Operators
– Bitwise Operations
– Set Operations

23
2022-05-12

Arithmetic Operators
• MATLAB allows two different types of
arithmetic operations −
– Matrix arithmetic operations
– Array arithmetic operations
• Matrix arithmetic operations are same as
defined in linear algebra.
• Array operations are executed element by
element, both on one-dimensional and
multidimensional array.

Arithmetic Operators …
Sr. No. Operator & Description
1 +
Addition or unary plus.
A+B adds the values stored in variables A and B.
A and B must have the same size, unless one is a scalar.
A scalar can be added to a matrix of any size.
2 -
Subtraction or unary minus.
A-B subtracts the value of B from A.
A and B must have the same size, unless one is a scalar.
A scalar can be subtracted from a matrix of any size.

24
2022-05-12

Arithmetic Operators …
Sr. Operator & Description
No.
3 *
Matrix multiplication.
C = A*B is the linear algebraic product of the matrices A
and B. For non-scalar A and B, the number of columns of
A must be equal to the number of rows of B.
A scalar can multiply a matrix of any size.
4 .*
Array multiplication.
A.*B is the element-by-element product of the arrays A
and B. A and B must have the same size, unless one of
them is a scalar.

Arithmetic Operators …
Sr. No. Operator & Description
5 /
Slash or matrix right division.
B/A is roughly the same as B*inv(A).
More precisely, B/A = (A'\B')'.

6 ./
Array right division.
A./B is the matrix with elements A(i,j)/B(i,j). A
and B must have the same size, unless one of
them is a scalar.

25
2022-05-12

Arithmetic Operators …
Sr. No. Operator & Description

7 \
Backslash or matrix left division.
If A is a square matrix, A\B is roughly the same as inv(A)*B, except
it is computed in a different way.
If A is an n-by-n matrix and B is a column vector with n
components, or a matrix with several such columns, then X = A\B
is the solution to the equation AX = B. A warning message is
displayed if A is badly scaled or nearly singular.

8 .\
Array left division.
A.\B is the matrix with elements B(i,j)/A(i,j).
A and B must have the same size, unless one of them is a scalar.

Arithmetic Operators …
Sr. No. Operator & Description
9 ^
Matrix power.
X^p is X to the power p, if p is a scalar.
If p is an integer, the power is computed by repeated squaring.
If the integer is negative, X is inverted first.

10 .^
Array power.
A.^B is the matrix with elements A(i,j) to the B(i,j) power.
A and B must have the same size, unless one of them is a scalar.

26
2022-05-12

Arithmetic Operators …
Sr. No. Operator & Description
11 '
Matrix transpose.
A' is the linear algebraic transpose of A.
For complex matrices, this is the complex conjugate
transpose.
12 .'
Array transpose.
A.' is the array transpose of A.
For complex matrices, this does not involve
conjugation.

Complex Numbers
• sqrt(-1) = i or j.
• Use x = 2+2i or x = 2+2*i or x=2+2*sqrt(-1)
• x = 2+sin(0.5)*j
• Termination with i and j works with numbers and
not expression.
• For expression use *i or *j such as x = 2+sin(0.5)*j
• To find magnitude and angle (in radian) use abs
and angle functions.
• To extract real and imaginary part use real and
imag functions.
• To find conjugate of x use conj function.

27
2022-05-12

Complex Numbers …
• Example:
– Z=3+4j;
– Find magz = abs(z),
– anglez_radians = angle(z),
– anglez_degrees=angle(z)*180/pi,
– real_partz=real(z)
– imaginary_partz=imag(z).
• Results:
– z = 3.0000 + 4.0000i
– magz = 5
– anglez_radians = 0.9273
– anglez_degrees = 53.1301
– real_partz = 3
– imaginary_partz = 4

Creating Vectors
• A vector is a one-dimensional array of
numbers.
• MATLAB allows creating two types of vectors:
– Row vectors
– Column vectors
• Row vectors are created by enclosing the set
of elements in square brackets, using space or
comma to delimit the elements.

28
2022-05-12

Creating Vectors …
• For example,
r = [7 8 9 10 11]
• MATLAB will execute the above statement and
return the following result:
r
= 7 8 9 10 11
• Another example,
r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t

Creating Vectors …
• MATLAB will execute the above statement and
return the following result:-
res =
9 11 13 15 17
• Column vectors are created by enclosing the
set of elements in square brackets, using
semicolon(;) to delimit the elements.
c = [7; 8; 9; 10; 11]

29
2022-05-12

Creating Vectors …
• MATLAB will execute the above statement and
return the following result: −
c=
7
8
9
10
11

Creating Matrices
• A matrix is a two-dimensional array of
numbers.
• 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.
• For example, let us create a 3-by-3 matrix as −
m = [1 2 3; 4 5 6; 7 8 9]

30
2022-05-12

Creating Matrices …
• MATLAB will execute the above statement and
return the following result: −
m=
1 2 3
4 5 6
7 8 9

Vector Operations
• To carryout vector operations:−
– Addition and Subtraction of Vectors
– Scalar Multiplication of Vectors
– Transpose of a Vector
– Appending Vectors
– Magnitude of a Vector
– Vector Dot Product
– Vectors with Uniformly Spaced Elements

31
2022-05-12

Addition and Subtraction of Vectors


• Example (you can also write a script file)
A = [7, 11, 15, 23, 9];
B = [2, 5, 13, 16, 20];
C = A + B;
D = A - B;
disp(C);
disp(D);

Addition and Subtraction of Vectors …


• When you run the file, it displays the following
result −
9 16 28 39 29
5 6 2 7 -11

32
2022-05-12

Scalar Multiplication of Vectors


• Example
• Create a script file with the following code −
v = [ 12 34 10 8];
m=5*v
• When you run the file, it displays the following
result −
m=
60 170 50 40

MATLAB - Transpose of a Vector


• Example
• Create a script file with the following code −
r = [ 1 2 3 4 ]; %row vector
t_r = r';
v = [1;2;3;4]; %column vector
t_v = v';
disp(t_r);
disp(t_v);

33
2022-05-12

MATLAB - Transpose of a Vector …


• When you run the file, it displays the following
result −
1
2
3
4

1 2 3 4

MATLAB - Appending Vectors


• If you have two row vectors r1 and r2 with n and
m number of elements, you can create a row
vector r of n plus m elements, by appending
these vectors:
r = [r1,r2]
• You can also create a matrix r by appending
these two vectors, the vector r2, will be the
second row of the matrix:
r = [r1;r2]
both the vectors should have same number of
elements.

34
2022-05-12

MATLAB - Appending Vectors …


• Similarly, you can append two column vectors c1
and c2 with n and m number of elements.
• To create a column vector c of n plus m elements,
by appending these vectors, you write:
c = [c1; c2]
• You can also create a matrix c by appending
these two vectors; the vector c2 will be the
second column of the matrix:
c = [c1, c2]
• However, to do this, both the vectors should
have same number of elements.

MATLAB - Appending Vectors …


• Example
• Create a script file with the following code −
r1 = [ 1 2 3 4];
r2 = [5 6 7 8];
r = [r1,r2]
rMat = [r1;r2]
c1 = [ 1; 2; 3; 4 ];
c2 = [5; 6; 7; 8 ];
c = [c1; c2]
cMat = [c1,c2]

35
2022-05-12

MATLAB - Appending Vectors …


• When you run the file, it displays the following result −
r=
Columns 1 through 7:
1 2 3 4 5 6 7
Column 8:
8
rMat =
1 2 3 4
5 6 7 8
c=
1
2
3
4
5
6
7
8
cMat =
1 5
2 6
3 7
4 8

MATLAB - 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 −
– Take the product of the vector with itself, using array
multiplication (.*). This produces a vector s_v, whose elements
are squares of the elements of vector v.
s_v = v.*v;
– Use the sum function to get the sum of squares of elements of
vector v. This is also called the dot product of vector v.
d_p= sum(s_v);
– Use the sqrt function to get the square root of the sum which is
also the magnitude of the vector v.
mag = sqrt(d_p);

36
2022-05-12

MATLAB - Magnitude of a Vector …


• Example
• Create a script file with the following code −
v = [1: 2: 20];
s_v = v.* v; %the vector with elements
% as square of v's elements
d_p = sum(s_v); % sum of squares -- the dot product
mag = sqrt(d_p); % magnitude
disp('Magnitude:');
disp(mag);
• When you run the file, it displays the following result −
Magnitude:
36.469

MATLAB - Vector Dot Product


• Dot product of two vectors a and b is calculated using the
dot function.
dot(a, b);
• Example
• Create a script file with the following code:
v1 = [2 3 4];
v2 = [1 2 3];
d_p = dot(v1, v2);
disp('Dot Product:');
disp(d_p);
When you run the file, it displays the following result:
Dot Product:
20

37
2022-05-12

MATLAB - Matrix
• A matrix is a two-dimensional array of
numbers.
• In MATLAB, you create a matrix by entering
elements in each row as comma or space
delimited numbers and using semicolons to
mark the end of each row.
• For example, let us create a 4-by-5 matrix a −
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]

MATLAB - Matrix
• MATLAB will execute the above statement and
return the following result −
a=
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8

38
2022-05-12

Referencing the Elements of a Matrix


(matrix indexing)
• To reference an element in the mth row and nth
column, of a matrix m_x, we write −
m_x(m, n);
• For example, to refer to the element in the 2nd
row and 5th column, of the matrix A, we type −
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
A(2,5)
• MATLAB will execute the above statement and
return the following result:
ans = 6

Referencing the Elements of a


Matrix (matrix indexing) …
• Example 2

39
2022-05-12

Referencing the Elements of a Matrix (matrix


indexing) …
• To reference all the elements in the mth column we type
A(:,m).
• Let us create a column vector A, from the elements of the
4th row of the matrix A
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
V = A(:,4)
• MATLAB will execute the above statement and return the
following result:
V=
4
5
6
7

Referencing the Elements of a Matrix (matrix


indexing) …
• You can also select the elements in the mth through nth columns, for
this we write −
A(:,m:n)
• Let us create a smaller matrix taking the elements from the 2nd and
3rd columns:
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; A(:, 2:3)
• MATLAB will execute the above statement and return the following
result:
ans =
2 3
3 4
4 5
5 6

40
2022-05-12

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. Principally, [] denotes an empty array.
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
A( 4 , : ) = []
• MATLAB will execute the above statement and return:
A=
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7

Deleting a Row or a Column in a


Matrix …
• To delete the fifth column of A:
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(: , 5)=[]
• MATLAB will execute the above statement and
return the following result:
A=
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

41
2022-05-12

Addition & Subtraction of Matrices


• You can add or subtract matrices.
• Both the operand matrices must have the
same number of rows and columns.
• Example
A = [ 1 2 3 ; 4 5 6; 7 8 9];
B = [ 7 5 6 ; 2 0 8; 5 7 1];
C=A+B
D=A-B

Addition & Subtraction of Matrices …


• When you run the file, it displays the following
result −
C=
8 7 9
6 5 14
12 15 10
D=
-6 -3 -3
2 5 -2
2 1 8

42
2022-05-12

Division (Left, Right) of Matrices


• (/) Slash or matrix right division.
• B/A is roughly the same as B*inv(A).
• More precisely, B/A = (A'\B')'.

• (\) Backslash or matrix left division.


• A\B is the same as inv(A)*B,
• If A is an n-by-n matrix and B is a column vector
with n components, or a matrix with several such
columns, then X = A\B is the solution to the
equation AX = B.

Division (Left, Right) of Matrices …


• Example
• Create a script file with the following code:
A = [ 1 2 3 ; 4 5 6; 7 8 9];
B = [ 7 5 6 ; 2 0 8; 5 7 1];
C=A/B
D=A\B

43
2022-05-12

Division (Left, Right) of Matrices …


• When you run the file, it displays the following
result −
c=
-0.52542 0.68644 0.66102
-0.42373 0.94068 1.01695
-0.32203 1.19492 1.37288
d=
-3.27778 -1.05556 -4.86111
-0.11111 0.11111 -0.27778
3.05556 1.27778 4.30556

Scalar Operations of Matrices


• Example
• Create a script file with the following code −
A = [ 10 12 23 ; 14 8 6; 27 8 9];
b = 2;
C=A+b
D=A-b
E=A*b
F=A/b

44
2022-05-12

Scalar Operations of Matrices


c=
12 14 25
16 10 8
29 10 11
d=
8 10 21
12 6 4
25 6 7
e=
20 24 46
28 16 12
54 16 18
f=
5.0000 6.0000 11.5000
7.0000 4.0000 3.0000
13.5000 4.0000 4.5000

45
2022-05-12

Linspace and logspace commands


• Syntax:
– t=linspace(n1,n2); t=linspace(n1,n2,n);
– w=logspace(n1,n2); w=logspace(n1,n2,n);
• linspace creates a row vector linearly spaced from
n1 to n2 with length n (number of points n is
optional, its default value is 100).
• Example: t1=linspace(1,10); creates 100 points
from 1 to 10. t2=linspace(1,10,20) =>20 points.
• Logspace(n1,n2,n) creates a logarithmic spaced
row vector from 10n1 to 10n2 with n points
(number of points n is optional, its default value is
50).

Linspace and logspace commands …


• Example: x=logspace(-1,2); creates 50 points
from 0.1 to 100.
• Row vectors are created using linspace and
logspace.
• To get column vector use transpose. Example:
t=linspace(1,10,20)’.

46
2022-05-12

Relational operators
• < less than
• <= less than or equal
• > greater than
• >= greater than or equal
• == equal to
• ῀= not equal

Logical operations
• & AND
• | OR

47
2022-05-12

Find function
• find function finds indices and values of
elements which satisfies the logical condition.
• Syntax:
• k=find(x); returns the indices of array x that
points to nonzero elements.
• It is equivalent to k=find(x ῀=)
• Example:
– x=[1 0 4 -3 0 0 0 8 6]

Find function …
• indices=find(x);
• Returns indices for the nonzero entries of x:
• Indices=1 3 4 8 9
• To get elements use: x(indices) it will return 1 4 -3
8 6.
• [r, c]=find(x); returns the row and column indices
of nonzero entries in the matrix x.
• Example:
– z=[8 0 6; 3 5 7; 4 9 0]

48
2022-05-12

Find function …
• [r, c]=find(z)
• [r, c] %this shows actual row and column
combination.

49

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy