SlideShare une entreprise Scribd logo
1  sur  148
Télécharger pour lire hors ligne
MATLAB Basics-Part1
Learning The Basics Of Matlab
Eng. Elaf Ahmed Saeed
Introduction to Matlab
What is MATLAB?
 Matlab is basically a high level language which has many
specialized toolboxes for making things easier for us.
 How high?
Matlab
High level
languages such as
C, Pascal etc.
Assembly
What is MATLAB?
 Matlab stands for MATrix LABoratory.
 The first version of MATLAB was produced in the mid 1970s as a
teaching tool. MATLAB started as an interactive program for doing
matrix calculations.
 MATLAB has now grown to a high level mathematical language that
can solve integrals and differential equations numerically and plot a
wide variety of two and three Dimensional graphs.
 The expanded MATLAB is now used for calculations and simulation
in companies and government labs ranging from aerospace, car
design, signal analysis through to instrument control and financial
analysis.
 In practice, it provides a very nice tool to implement numerical
method.
MATLAB Environment
 When you start MATLAB, the desktop appears in its default layout.
MATLAB Environment
 The desktop includes these panels:
• Current Folder — Access your files.
• Command Window — Enter commands at the command line,
indicated by the prompt (>>).
• Workspace — Explore data that you create or import from files.
MATLAB Startup Window Workspace
• View program variables
• Double Click on a variable
Command Window
Type Command
Current folder
View folders and m-files
Command History
• View past command
• Save a whole session using
diary.
MATLAB Components
MATLAB Workspace
Handle graphic Simulink
Toolboxes MATLAB Compiler
Command Window
 After starting MATLAB, the Command window will open with the
Command prompt being displayed: >>
 The calculator mode of MATLAB operates in a sequential fashion as
you type in Commands line by line. For each command, you get a
result.
 For example, if you type >> 55 – 16
MATLAB will display the result
ans=
39 (Try >> ans + 10)
Tip
 Using the up-arrow key, you can go back to any command that you
entered. Press the up-arrow until you get back the line.
>> b * a
 Alternatively, you can type b and press the up-arrow once and it will
automatically bring up the last command beginning with the letter b. the
up-arrow shortcut is a quick way to fix errors without having to retype the
entire line.
MATLAB Installation
Version 2018
Files of installation
Files of installation
Files of installation
Files of installation
Files of installation
Step1
 Select (install manually…..)  Next
Step 2
 Select (yes)  Next
Step 3
 Select (I have file installation key)
Step 4
 Copy the from the text file.
Copy
Key
Step 5
 Paste the Key
Step 6
 Select Typically  Next
Step 7
 Select path to installation  Next
Step 8
 Click install
Step 9
 Download window. This step takes some time to install the program.
Step 10
 After install complete select (Activate manually without internet)
Next
Step 11
 Enter the path of file license.
Step 11
 The file license with the folder installation.
Step 12
 Click finish.
Step 13
 If the shortcut not found you can found it from:
C  program files  MATLAB  R2018a  bin  matlab
Step 13
 If the short cu not found you can found it from:
C  program files  MATLAB  R2018a  bin  matlab
Step 13
 If the short cu not found you can found it from:
C  program files  MATLAB  R2018a  bin  matlab
Step 13
 If the short cu not found you can found it from:
C  program files  MATLAB  R2018a  bin  matlab
Step 13
 If the short cu not found you can found it from:
C  program files  MATLAB  R2018a  bin  matlab
Step 14
 If the MATLAB not open do the following:
Copy the (license_standalone.lic) and paste it in the folder (licences)
In MATLAB folder in C.
Step 14
 If the MATLAB not open do the following:
Copy the (license_standalone.lic) and paste it in the folder (licences)
In MATLAB folder in C.
Assignment
What is Assignment?
 Assignment refers to assign values to variable
names. This results in the storage of the values in
the memory location corresponding to the
variables name.
>> a = 5
Scalars
 In MATLAB a scalar is a variable with one row and one column.
Scalars are the simple variables that we use and manipulate in
simple algebraic equations.
 Creating scalars
To create a scalar you simply introduce it on the left hand side of an
equal sign.
>> x = 1;
>> y = 2;
>> z = x + y;
Scalars
 Scalar operations
MATLAB supports the standard scalar operations using an obvious
notation. The following statements demonstrate scalar addition,
subtraction, multiplication and division.
>> u = 5;
>> v = 3;
>> w = u + v
>> x = u - v
>> y = u * v
>> z = u/v
Scalars
 >> a = 4
Note how the assignment echo prints to confirm what you have done:
a =
4
 Echo printing is a characteristic of MATLAB. It can be suppressed by
terminating the command line with the semicolon (;)
>> A = 6;
 You can type several commands on the same line by separating them
with commas or semicolons. For example,
>> a = 4, A = 6; x = 1;
a =
4
Note
 MATLAB Treats names in a case-sensitive
manner. That is, the variable a is not the same as
A. To illustrate this, enter
>> a
And then enter
>> A
Complex Number
 We can assign Complex Values to variables, since MATLAB handles
complex arithmetic automatically. The unit imaginary number −𝟏 is
preassigned to the variable i.
>> x = 2+i*4
x =
2.0000 + 4.0000i
 It should be noted that MATLAB allows the symbol j to be used to
represent the unit imaginary number for input. However, it always uses
an i for display. For example,
>> x = 2+j*4
x=
2.0000+4.0000i
Complex Number
 Function with complex numbers:
Complex Number
 Examples:
>> x=3+4i
ans=
3.0000 + 4.0000i
>> real(x)
ans=
3
>> imag(x)
ans=
4
>> abs(x)
ans=
5
Complex Number
 Examples:
>> angle(x)
ans=
0.9273
>> conj(x)
ans=
3-4i
Predefined variables
 There are several predefined variables, for examples, pi.
>> pi
ans=
3.1416
 >> exp(1)
ans=
2.7183
Format type
 Note how MATLAB displays four decimal places. If you desire
additional precision, enter the following:
>> format long
 Now when pi is entered the result is displayed to 15 significant
figures:
>> pi
ans=
3.14159263558979
Format type
 If we want to go back to the four digits after coma write:
>> Format short
>> pi
ans=
3.1416
 The following is a summary of the format commands you will employ
routinely in engineering and scientific calculations. They all have the
syntax: format type
Format type
Type Result Example
short Scaled fixed-point format with 5 digits. 3.1416
long Scaled fixed-point format with 15 digits
for double and 7 digits for single.
3.14159265358979
Short e Floating point format with 5 digits. 3.1416e+000
Long e Floating point format with 15 digits for
double and 7 digits for single.
3.141592653589793e+000
Short g Best of fixed or floating point format with
5 digits.
3.1416
Long g Best of fixed or floating point format with
15 digits for double and 7 digits for
single.
3.14159265358979
Short eng Engineering format with at least 5 digits
and a power that is a multiple of 3.
3.1416e+000
Long eng Engineering format with at least 16 digits
and a power that is a multiple of 3.
3.14159265358979e+000
bank Fixed dollars and cents 3.14
Exercise
 Given x=2
y=x+2
z=x+y
1/ Type these simple statements to find the value of y and z.
2/ Try to put the variables in one row and get the answer.
3/ Hide the result of x , y and get only the result of z.
Operations in MATLAB
Basic Math Operations
 Mathematical operations in MATLAB can be performed on both
scalars and arrays.
 The common operators, in order of priority, are:
Symbol Meaning Example
^ Exponentiation 4^2=8
- Negation (unary operation) -8=-8
*
/
Multiplication and Division 2*pi = 6.2832
Pi/4 = 0.78554
 Left Division 62 = 0.3333
+
-
Addition and
Subtraction
3+5=8
3-5 = -2
Basic Math Operations
 Order of Operations
• The order of operations is set first by parentheses, then by the default order
given above:
 Y= -4 ^ 2 gives y= -16
Since the exponentiation happens first due to its higher default priority, but
 Y = (-4) ^ 2 gives y = 16
Since the negation operation on the 4 takes place first.
Basic Math Operations
 Examples
1. Compute (2+3-9)*7^2/4
ans = -49
2.
1
2+32 +
4
5
x
6
7
In MATLAB it becomes
>> 1/(2+3^2) + 4/5 * 6/7
ans = 0.7766
Or, if parentheses are missing,
>> ½+3^2 + 4/5 * 6/7
ans = 10.1857
3. >> -5 / (4.8) + 5.32) ^ 2
ans = -0.0488
Basic Math Operations
 Calculations can also involve complex quantities. x = (2+4i) and y = (16):
>> 3 * x
ans = 6.0000 + 12.0000i
>> x ^ 2
ans = -12.0000 + 16.0000i
>> x + y
ans = 18.0000 + 4.0000i
>> (3 + 4i) * (3 – 4i)
ans = 6.1230e-017
Relational Operations
 A > B The result is true if A is greater than B, and is false otherwise.
 A < B The result is true if A is less than B, and is false otherwise.
 A >= B The result is true if A is grater than or equal to B, and is false otherwise.
 A <= B The result is true if A is less than or equal to B, and is false otherwise.
 A == B The result is true if A is equal to B, and is false otherwise.
 A ~= B The result is true if A is not equal to B, and is false otherwise.
Example:
>> a = 5; b = 2;
>> x = a > b ;
x = 1 (true)
>> x = (a == b);
x = 0 (false)
Relational Operations
 Note
Many users confuse the double equality sight (==) used in relational
tests with the equality sign (=) used in assignments. When a user uses
(=) instead (==), MATLAB usually reports that an expected relational
operator wasn’t found.
Relational Operations
 The table summary of relational operators in MATLAB.
Example Operator Relational
X == 0 == Equal
Unit ~= ‘m’ ~= Not equal
A < 0 < Less Than
S > t > Greater Than
3.9 <= a/3 <= Less than or equal to
R >= 0 >= Greater than or equal to
Logical Operations
 The table that summarize the logical operation in MATLAB.
Operator Operation
& Logical AND
&& Logical AND with shortcut evaluation
| Logical OR
|| Logical OR with shortcut evaluation
xor Logical exclusive OR
~ Logical NOT
Logical Operations
 A truth table summarize the possible outcomes for logical operators
employed in MATLAB. The order of priority is shown at the top of
the table.
Highest Lowest
X Y ~X X&Y X|Y
T T F T T
T F F F T
F T T F T
F F T F F
Logical Operations
• ~x (NOT): true if x is false (or zero); false otherwise.
• x & y (NND): true if both x and y are true (or non-zero).
• x | y (OR): true if either x or y are true (or non-zero).
 Priority can be set using parentheses. After that, mathematical expressions
are highest priority, followed by relational operators, followed by logical
operators. All things being equal, expression are performed from left to
right.
 Not is the highest priority logical operator, followed by AND and finally
OR.
Logical Operations
 Examples:
>> 1 & 1
ans = 1
>> 1 | 0
ans = 1
>> 0 & 0
ans = 0
>> ~1
ans = 0
Built-in Functions in MATLAB
Built-in Functions
 MATLAB and its toolboxes have a rich of collection of built-In
functions. You can use online help to find out more about them. For
example, if you want to learn about the log function:
>> help log
LOG Natural logarithm.
LOG(x) is the natural logarithm of the elements of x.
Complete results are produced if x is not positive.
See also log2, log10, exp, logm.
Built-in Functions with vector and
Matrix
 One of the important properties of MATLAB’S built-in functions is
that they will operate directly on vector and matrix quantities. For
example, try
>> A = [1 2 3; 4 5 6; 7 8 9]
>> log(A)
ans =
0 0.6931 1.0986
1.3863 1.6094 1.7918
1.9459 2.0794 2.1972
Built-in Functions with vector and
Matrix
 You will see that the natural logarithm function is applied in array
style, element by element, to the matrix A. Most functions, such as
sqrt, abs, sin, acos, tanh, and exp, operate in array fashion.
>> sqrt(A)
ans =
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495
2.6458 2.8284 3.0000
Built-in Functions with vector and
Matrix
 Length and Size functions
 The length and size functions in MATLAB are used to find dimensions of
vector and matrices. The length function returns the number of elements in
a vector. The size function returns the number of rows and columns in a
vector or matrix. For example
>> vec(-2:1)
vec = -2 -1 0 1
>> length(vec)
ans = 4
>> size(vec)
ans = 1 4
Built-in Functions with vector and
Matrix
 Note
The length function will return either the number of rows or the number of
columns, whichever is largest.
 we can create a matrix of zeros with the same size as another matrix. For a
matrix variable vec, the following expression would accomplish this:
zeros(size(vec))
The size function returns the size of the matrix, which is then passed to the
zeros function, which then returns a matrix of zeros with the same size as vec.
 What would be produced by the following command line:
>> rand(size(vec))
Round-off Functions
 There are several functions for rounding. For example, suppose that we
enter a vector:
>> x = [-1.6-1.5-1.41.4 1.5 1.6]
• The round function rounds the elements of x to the nearest integers:
>> round(x)
ans = -2 -2 -1 1 2 2
• The ceil (short for ceiling) function rounds to the nearest integers toward
infinity:
>> ceil(x)
ans = -1 -1 -1 2 2 2
Round-off Functions
• The floor function rounds down to the nearest integers toward minus
infinity:
>> floor(x)
ans = -2 -2 -2 1 1 1
Built-In Help Functions
 The built-in help function can give you information about both what
exists and how those functions are used:
• help elmat will list the elementary matrix creation and manipulation
functions, including functions to get information about matrices.
• help elfun will list the elementary math functions, including trig,
exponential, complex, rounding, and reminder functions.
 The built-in lookfor command will search help files for occurrences
of text and can be useful if you know a function’s purpose but not
its name. (to stop search click (ctrl+c))
Summary of Built-in Functions in
MATLAB
 General Functions
Function Used to
who List current variables.
whos List current variables in details
clear x Clear variable x from memory
open Open new file
figure(n) Opens new figure numbered n
close Closes last figure
Close all Closes all figures
dir List files in directly
format Set output format
help To know any command and
example
lookfor To search for command
exit Quits MATLAB
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Basic Arithmetic
• Addition
• Subtraction
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Basic Arithmetic
• Multiplication
• Division
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Basic Arithmetic
• Powers
• Transpose
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Basic Arithmetic
• Modulo Division and Rounding
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Trigonometry
• Sine
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Trigonometry
• Cosine
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Trigonometry
• Tangent
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Trigonometry
• Cosecant
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Trigonometry
• Secant
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Trigonometry
• Cotangent
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Exponents and
Logarithms
Summary of Built-in Functions in
MATLAB
 Elementary Math (Arithmetic Operations) Complex Numbers
Summary of Built-in Functions in
MATLAB
 Elementary Math (Linear Algebra) Matrix Operations
Summary of Built-in Functions in
MATLAB
 Elementary Math (Random Number Generation)
Summary of Built-in Functions in
MATLAB
 Elementary Math (2-D and 3-D Plots) Line Plots
Summary of Built-in Functions in
MATLAB
 Elementary Math (2-D and 3-D Plots) Function Plots
Summary of Built-in Functions in
MATLAB
 Elementary Math (Labels and Annotations) Labels
Summary of Built-in Functions in
MATLAB
 Elementary Math (Labels and Annotations) Multiple Plots
Summary of Built-in Functions in
MATLAB
 Elementary Math (Help and Support)
Summary of Built-in Functions in
MATLAB
For more learn :
https://uk.mathworks.com/help/matlab/referencelist.html?type=functio
n&s_tid=CRUX_topnav
Vectors and Matrices in MATLAB
Arrays, Vectors, and Matrices
Introduction
 An array is a collection of values that are represented by a single
variable name.
 MATLAB can automatically handle rectangular arrays of data-one-
dimensional arrays are called vectors and two-dimensional arrays are
called matrices.
 Arrays are set off using square bracket [ ] in MATLAB.
 Entries within a row are separated by space or commas.
 Rows are separated by semicolons.
Array Examples
 Row Vector
>> a=[1 2 3 4 5 ]
a=
1 2 3 4 5
 Columns Vector
>> b = [2;3;6;8;10]
b=
2
4
6
8
10
Use of Vector
 MATLAB indexing starts with 1, not 0.
• We will not respond to any emails where this is the problem.
 A(n) returns the 𝑛 𝑡ℎ
element.
A=[13 5 9 10]
 The index argument can be a vector. In this case, each element is
looked up individually, and return as a vector of the same size as
the index vector.
A(1)
A(2) A(3) A(4)
Matrices
 A 2-D array, or matrix, of data is entered row by row, with spaces (or
commas) separating entries with row and semicolons separating the
rows:
>> A= [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
Useful Array Commands
 The transpose operator (apostrophe) can be used to flip an arrays
over its own diagonal. For example, if b is a row vector, b’ is a
column vector containing the complex conjugate of b. (Try b’)
 The command window will allow you to separate rows by hitting the
enter key – script file and functions will allow you to put rows on
new lines as well. (Retype the matrix A as describe above).
 The who command will report back used variable names; whos will
also give you the size, memory, and data types for the arrays.
Accessing Matrix Entries
 Individual entries within array can be both read and set using either
the index of the location in the array or the row and column.
 The index value starts with 1 for the entry in the top left corner of an
array and increases down a column – the following shows the
indices for a 4 row, 3 column matrix:
1 5 9
2 6 10
3 7 11
4 8 12
Accessing Matrix Entries(Cont)
 Accessing some matrix C:
C=
2 4 9
3 3 16
3 0 8
10 13 17
C(2) Would report 3
C(4) Would report 10
C(13) Would report an error!
Accessing Matrix Entries(Cont)
 Entries can also be access using the row and column:
C(2,1) Would report 3
C(3,2) Would report 0
C(5,1) Would report an error!
C(2,1) Would report 3
C(4,:) Would report the whole fourth raw.
C(:,2) Would report the whole second column.
Array & Matrix Creation-Built In
 There are several built-in functions to create arrays and matrices:
• zeros(r,c) Will create an r row by c column matrix of zeros.
• zeros(n) Will create an n by n matrix of zeros.
• ones(r,c) Will create an r row by c column matrix of ones.
• ones(n) Will create an n by n matrix of ones.
Try… rand(n) and rand(r,c)
 help elmat has, among other things, a list of the elementary
matrices.
Try… zeros(3) , ones(2)
Array Creation-Colon Operator
 The colon operator : is useful in several contexts. It can be used to create a
linearly spaced array of points using the notation.
Start:diffval:limit
Where start is the first value in the array, diffval is the difference between
successive values in the array, and limit is the boundary for the last value
(though not necessary the last value).
>> 1:6:3
ans=
1.0000 1.6000 2.2000 2.8000
Array Creation-Colon Operator
 Note
 If diffval is omitted, the default value is 1:
>> 3:6
ans=
3 4 5 6
 To create a decreasing series, diffval must be negative:
>> 5:-1.2:2
ans=
5.0000 3.8000 2.6000
Array Creation-Colon Operator
(Cont)
 If start+diffval>limit for an increasing series or
start+diffval>limit foe decreasing series, an empty matrix is
returned:
>> 5:2
ans=
Empty matrix: 1-by-0
 To create a column, transpose the output of the colon operator, not
the limit value; that is, (3:6)’ not 3:6’
Array Creation- linespace
 To create a row vector with a specific number of linearly spaced
points between two numbers, use the linspace command
 Linspace (x1,x2,n) will create a linearly spaced array of n points
between x1 and x2
>> linspace(1, 1, 6)
ans=
0 0.2000 0.4000 0.6000 0.8000 1.0000
 If n is omitted, 100 points are created.
 To generate a column, transpose the output of the linspace
command.
Array Creation- linspace
(Cont)
>> linspace(0,1,6)’
ans=
0
0.2000
0.4000
0.8000
1.0000
Exercise(1)
 Use the linspace function to create vectors identical to the following
created with colon notation:
 t = 5:6:30
 x = -3:4
 Homework
y = -4:2
Array Creation- logspace
(Cont)
 To create a row vector with a specific number of logarithmically
spaced points between two numbers, use the logspace command.
 Logspace(x1, x2, n) will create a logarithmically spaced array of n
points between 10 𝑥1
and 10 𝑥2
.
>> logspace (-1, 2, 4)
ans=
0.1000 1.0000 10.0000 100.0000
 If n is omitted, 100 points are created.
 To generate a column, transpose the output of the logspace
command.
Character Strings & Ellipsis
 Alphanumeric are enclosed by apostrophes (‘)
>> f= ‘ Mailes‘
>> s = ‘Davis’
Concatenation: passing together of strings
>> x= [f s]
x=
Miles Davis
Ellipsis (…): Used to continue long lines
>> a = [1 2 3 4 5 …
6 7 8]
a=
1 2 3 4 5 6 7 8
Character Strings & Ellipsis
 You cannot use an ellipsis within single quotes to continue a string.
But you can piece together shorter strings with ellipsis.
>> quote = [‘Hello Word’ …
‘I am a Teacher’]
quote =
Hello Word I am a Teacher
Vector-Matrix Calculations
 MATLAB can also perform operations in vectors and matrices.
 The * operator for matrices is defined as the product or what is commonly called
“matrix multiplication.”
• The number of columns of the first matrix must match the number of rows in the
second matrix.
• The size of the result will have as many rows as the first matrix and as many
columns as the second matrix.
• The exception to this is multiplication by a 1x1 matrix, which is actually an array
operation.
 The ^ operator for matrices result in the matrix being matrix-multiplied by itself a
specified number of times.
• Note – in this case, the matrix must be square! Why?
Because, matrix-multiplied by itself .
Element-by-Element Calculations
 At time, you will want to carry out calculation item by item in a matrix or
vector. The MATLAB manual calls these array operations. They are also
often referred to as element-by-element operations.
 MATLAB defines .* and ./ (note the dots) as the array multiplication and
array division operators.
• For array operations, both matrices must be the same size or one of the
matrices must be 1x1.
 Array exponentiation (raising each element to a corresponding power in
another matrix) is performed with .^
 Again, for array operations, both matrices must be the same size or one of
the matrices must be 1x1.
Notes
 To define the matrix using another matrix:
B=[1.5, 3.1]; S=[3.0 B]; This is equal to S=[3.0 1.5 3.1]
 Changing the value in a matrix:
Lets consider that we have matrix S, S(2)=1.5
To change the second value of S from 1.5 to -1.0
We type: S(2) = -1.0;
Simple Practice
 a= 1 2 3
b= 2
4
6
c= 78 9
Try
 a+b {is it possible?!} what about a+c
 a.c
 b.a
 a/pi
 a^2
Examples about Vectors
 1- >> x = [12 13 5 8]
x (2:3); a = [ 13 5];
b = x(1 : end-1);  b = [12 13 5];
 2- >> a[1 : 5]
a =
1 2 3 4 5
 3- >> a = [1 : 2 : 5]
a =
1 3 5
Examples about Vectors
 4- >> b = prod (x)
b =
120
 5- >> a = [5E-1 7E-2 9e-4]
a=
0.5000 0.0700 0.0009
Examples about Special Matrices
 1- >> a = zeros (2,3)
a =
0 0 0
0 0 0
 2- >> a = ones (2,3)
a =
1 1 1
1 1 1
Examples about Special Matrices
 3- >> eye(4)
a = 10 0 0
0 1 0 0
0 0 1 0
0 0 0 1
 4- >> x = [1 2; 1 6]
x =
1 2
1 6
y=
1.5000 -0.5000
-0.2500 0.2500
Examples about Special Matrices
 5- >> r = magic(4)  generate a random matrix according to the
number between Parentheses.
 6- >> x = [1 2; 4 2]
>> max(x)
ans =
4 2
>> max (max(x))
ans =
4
Examples about Special Matrices
 7- >> x = [1 2 3; 4 5 6];
>> reshape(x,2,3)
ans =
1 2 3
4 5 6
>> reshape(x,3,2)
ans =
1 5
4 3
2 6
Examples about Special Matrices
 8- >> fliprl(x)
ans =
3 2 1
6 5 4
 9- >>flipud(x)
ans =
4 5 6
1 2 3
 >> size(x)
ans =
2 3
Matrix Manipulation Functions
Function Used to
zeros Create an matrix of all zeros.
length Length of matrix.
ones Create an matrix of all ones.
eye Identity matrix.
rand Uniformly distributed random number.
size Return matrix dimension.
Fliprl Flip matrices left-right.
Flipud Flip matrices up and down.
size Dimension of matrix.
Examples about operation on
Matrices
 1- Transpose
>> a = [1 2; 3 4]
a=
1 2
3 4
>> a’
ans =
1 3
2 4
Examples about operation on
Matrices
>> transpose (a)
ans =
1 3
2 4
>> rot90 (a)
ans =
2 4
1 3
>> rot90(rot90(a))  180 deg.
ans =
4 3
2 1
Examples about operation on
Matrices
 2- Addition
must be the two matrices have the dimention.
>> a = [1 2; 3 4];
>> b = [1 2; 3 4];
>> a + b
ans =
2 4
6 8
>> a + 2
ans =
3 4
5 6
Examples about operation on
Matrices
 3- Multiplication
>> a = [1 2; 3 4];
>> b = [1 2; 3 4];
>> a * b
ans =
7 10
15 22
>> 2 * a
ans=
2 4
6 8
Examples about operation on
Matrices
>> C = ones(2,4)
C =
1 1 1 1
1 1 1 1
>> a * C
ans =
3 3 3 3
7 7 7 7
Examples about operation on
Matrices
 4- Division
>> a = [1 2; 3 4];
>> b = [1 2; 3 4];
>> a/b
ans =
1 0
0 1
Or: >> a*inv(b)
ans =
1.0000 0.0000
0.0000 1.0000
Examples about operation on
Matrices
 5- Dot Multiplication
>> a = [1 2; 3 4];
>> b = [1 -1; 2 -2];
>> c = a .* b
ans =
1 -2
6 -8
Examples about operation on
Matrices
 6- Dot Division
>> c = a ./ b
 7- Dot Power
>> c = a .^ b
 8- Inverse
>> a = [1 2; 3 4]
>> b = inv (a)
b = -2.0000 1.0000 
1.5000 -0.5000
Or:
>> b = a ^ -1
b =
-2.000 1.000
1.5000 -0.5000
Examples about operation on
Matrices
 9- Determinant
>> a = det (a)
d = 0
 10- Power
>> a = [1 2; 3 4]
a =
1 2
3 4
>> b = a ^2
b =
7 10 
15 22
• The matrix must be square.
• a .^2 is equivalent a *a
Examples about operation on
Matrices
 11- Summation
- >> a = [1 3 5]
a = 1 3 5
>> s = sum(a)
s =
9
- >> a = [12; 3 4];
>> sum(sum(a))
ans =
10
Examples about operation on
Matrices
 12- Rank
1- rank(A) provides an estimate of the number of linearly independent rows or
columns of a matrix A.
2- trace (A) sum of the diagonal elements of A.
>> A = [1 2 5; 0 1 7; 2 3 4]
>> rank (A)
ans =
3
>> trace (A)
ans =
6
Matrix Sub-scripting
 P = pascal(n) returns a Pascal’s Matrix of order n. P is a symmetric
positive definite matrix with integer entries taken from Pascal's triangle.
The inverse of P has integer entries.
 >> p = pascal(5)
a =
1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70
Matrix Sub-scripting (Cont)
>> a(3, 2)
ans =
3
>> a (8)
ans =
3
>> a (: , 3)
1
3
6
10
15
Matrix Sub-scripting (Cont)
>> a (3:4 , 3:4)
ans =
1
3
>> a (1:5, 3)
ans =
1
3
6
10
15
Matrix Sub-scripting (Cont)
>> [a(7) a(17) ; a(9)]
z =
2 4
4 20
Or:
>> z = [a(2,2) a(2,4); a(4,2) a(4,4)]
z =
2 4
4 20
Matrix Sub-scripting (Cont)
>> a (:, [1 2])
ans=
1 1
1 2
1 3
1 4
>> a (: , 5) = [ ]  Delete the rows of the specific column.
1 1 1 1
1 2 3 4
1 3 6 10
1 4 10 20
1 5 15 35
Matrix Sub-scripting (Cont)
>> a (3 , :) = [ ]  delete the third row
a=
1 1 1 1
1 2 3 4
1 4 10 20
1 5 15 35
Concatenation Examples
>> x = [1 3 4 5]
x=
1 3 4 5
>> y = [x 1]
y =
1 3 4 5 1
>> y = [x; 2*x; 3*x]
y =
1 3 4 5
2 6 8 10
3 9 12 15
Concatenation Examples
>> z = [2 3 4 5]
z =
2 3 4 5
>> y = [x; 2*x; 3*x; z]
y =
1 3 4 5
2 6 8 10
3 9 12 15
4 3 4 5
Matrix Manipulation Functions
Function Syntax Description Example
Determinant B=det(A) B(a number) is the
determinant of A (a
square Matrix)
B=det(A)
Inverse D=inv(A) D (a square matrix) is
the inverse of A (a
square matrix)
A=inv(B*C)
Rank N=rank(A) N (a number) is the rank
of A (a matrix)
h= rank(A) - 1
Diagonal A=diag(c) A is the main diagonal
elements of c.
A= 2+diag(8)
Sum A=sum(B) A is the sum of all
elements of B if it is a
vector or sum of column
of B if it is a matrix.
A=sum (x)
Transpose A= transpose(b) A is the non conjugate
transpose of b.
A= transpose(b)
Or
(A = b’)
Exercise (2)
 Write the MATLAB commands that will create the following matrices:
 Now try to find (if the answer exist check that it is correct by hand)
>> b + c ………………………………………………………….
>> b - 2*c ………………………………………………………..
>> a + b ………………………………………………………….
Exercise (2)
 Calculate b*a by hand and then use MATLAB to check your answer.
 Now check that MATLAB gives the same results for b^3 and b*b*b (you don’t
need to do these by hand)
 Does the product a*b make sense?
 How can we display the second raw of matrix a?
 Change the value of c(4) to 10.
Exercise (3)
 Find an efficient way to generate the following matrix:
mat=
10 20 30 40
-6 -4 -2 0
Then, give an expression that will refer to the first two column.
linkden: www.linkedin.com/in/elaf-a-saeed-97bbb6150
facebook: https://www.facebook.com/profile.php?id=100004305557442
github: https://github.com/ElafAhmedSaeed
youtube: https://youtube.com/channel/UCE_RiXkyqREUdLAiZcbBqSg
slideshare: https://www.slideshare.net/ElafASaeed
SlidePlayer: https://slideplayer.com/slide/18030079/
Google Scholar:
https://scholar.google.com/citations?user=VIpVZKkAAAAJ&hl=ar&gmla=
AJsN-F7PIgAjWJ44Hzb18fwPqJaaUmG0XzbLdzx09

Contenu connexe

Tendances

MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkreddyprasad reddyvari
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabTarun Gehlot
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Randa Elanwar
 
Brief Introduction to Matlab
Brief  Introduction to MatlabBrief  Introduction to Matlab
Brief Introduction to MatlabTariq kanher
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to MatlabAmr Rashed
 
Basic matlab and matrix
Basic matlab and matrixBasic matlab and matrix
Basic matlab and matrixSaidur Rahman
 
Matlab on basic mathematics
Matlab on basic mathematicsMatlab on basic mathematics
Matlab on basic mathematicsmonauweralam1
 
Matlab matrices and arrays
Matlab matrices and arraysMatlab matrices and arrays
Matlab matrices and arraysAmeen San
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMohd Esa
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABAshish Meshram
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingDr. Manjunatha. P
 
MATLAB/SIMULINK for engineering applications: day 3
MATLAB/SIMULINK for engineering applications: day 3MATLAB/SIMULINK for engineering applications: day 3
MATLAB/SIMULINK for engineering applications: day 3reddyprasad reddyvari
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introductionAmeen San
 
Matlab simulink introduction
Matlab simulink introductionMatlab simulink introduction
Matlab simulink introductionAmeen San
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersMurshida ck
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabBilawalBaloch1
 

Tendances (20)

MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4Introduction to matlab lecture 1 of 4
Introduction to matlab lecture 1 of 4
 
Brief Introduction to Matlab
Brief  Introduction to MatlabBrief  Introduction to Matlab
Brief Introduction to Matlab
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Basic matlab and matrix
Basic matlab and matrixBasic matlab and matrix
Basic matlab and matrix
 
Matlab on basic mathematics
Matlab on basic mathematicsMatlab on basic mathematics
Matlab on basic mathematics
 
Seminar on MATLAB
Seminar on MATLABSeminar on MATLAB
Seminar on MATLAB
 
Matlab matrices and arrays
Matlab matrices and arraysMatlab matrices and arrays
Matlab matrices and arrays
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
 
MATLAB/SIMULINK for engineering applications: day 3
MATLAB/SIMULINK for engineering applications: day 3MATLAB/SIMULINK for engineering applications: day 3
MATLAB/SIMULINK for engineering applications: day 3
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Matlab simulink introduction
Matlab simulink introductionMatlab simulink introduction
Matlab simulink introduction
 
MATLAB Programming
MATLAB Programming MATLAB Programming
MATLAB Programming
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 

Similaire à MATLAB Basics-Part1

From zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyondFrom zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyondMahuaPal6
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsMukesh Kumar
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Kurmendra Singh
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlabkrajeshk1980
 
Matlab Overviiew
Matlab OverviiewMatlab Overviiew
Matlab OverviiewNazim Naeem
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxprashantkumarchinama
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlabaman gupta
 
matlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsxmatlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsxlekhacce
 

Similaire à MATLAB Basics-Part1 (20)

From zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyondFrom zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyond
 
Matlab tut2
Matlab tut2Matlab tut2
Matlab tut2
 
Matlab
MatlabMatlab
Matlab
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlab
 
Introduction to Matlab.ppt
Introduction to Matlab.pptIntroduction to Matlab.ppt
Introduction to Matlab.ppt
 
EE6711 Power System Simulation Lab manual
EE6711 Power System Simulation Lab manualEE6711 Power System Simulation Lab manual
EE6711 Power System Simulation Lab manual
 
Matlab Overviiew
Matlab OverviiewMatlab Overviiew
Matlab Overviiew
 
Matlab anilkumar
Matlab  anilkumarMatlab  anilkumar
Matlab anilkumar
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
 
Matlab summary
Matlab summaryMatlab summary
Matlab summary
 
Ch1
Ch1Ch1
Ch1
 
Matlab1
Matlab1Matlab1
Matlab1
 
Basic matlab for beginners
Basic matlab for beginnersBasic matlab for beginners
Basic matlab for beginners
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
matlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsxmatlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsx
 

Plus de Elaf A.Saeed

IOT NodeMCU - NodeMCU Webserver
IOT NodeMCU - NodeMCU WebserverIOT NodeMCU - NodeMCU Webserver
IOT NodeMCU - NodeMCU WebserverElaf A.Saeed
 
IOT NodeMCU - Ubidots Platform to Turn on LEDs
IOT NodeMCU - Ubidots Platform to Turn on LEDsIOT NodeMCU - Ubidots Platform to Turn on LEDs
IOT NodeMCU - Ubidots Platform to Turn on LEDsElaf A.Saeed
 
IOT NodeMCU - Thinger Platform to Turn on LEDs
IOT NodeMCU - Thinger Platform to Turn on LEDsIOT NodeMCU - Thinger Platform to Turn on LEDs
IOT NodeMCU - Thinger Platform to Turn on LEDsElaf A.Saeed
 
IOT NodeMCU - IFTTT Templet to send SMS
IOT NodeMCU - IFTTT Templet to send SMSIOT NodeMCU - IFTTT Templet to send SMS
IOT NodeMCU - IFTTT Templet to send SMSElaf A.Saeed
 
Getting date and time from ntp server with esp8266 node mcu
Getting date and time from ntp server with esp8266 node mcuGetting date and time from ntp server with esp8266 node mcu
Getting date and time from ntp server with esp8266 node mcuElaf A.Saeed
 
ESP8266 NodeMCU Server, Client, Station Mode, and Client Control LED
ESP8266 NodeMCU Server, Client, Station Mode, and Client Control LEDESP8266 NodeMCU Server, Client, Station Mode, and Client Control LED
ESP8266 NodeMCU Server, Client, Station Mode, and Client Control LEDElaf A.Saeed
 
IOT NodeMCU - NodeMCU Connection to Internet
IOT NodeMCU - NodeMCU Connection to InternetIOT NodeMCU - NodeMCU Connection to Internet
IOT NodeMCU - NodeMCU Connection to InternetElaf A.Saeed
 
Lesson 10- NodeMCU with LCD I2C
Lesson 10- NodeMCU with LCD I2CLesson 10- NodeMCU with LCD I2C
Lesson 10- NodeMCU with LCD I2CElaf A.Saeed
 
Lesson 9- NodeMCU with Arduino UNO (UART)
Lesson 9- NodeMCU with Arduino UNO (UART)Lesson 9- NodeMCU with Arduino UNO (UART)
Lesson 9- NodeMCU with Arduino UNO (UART)Elaf A.Saeed
 
Lesson 8- NodeMCU with Servo Motor
Lesson 8- NodeMCU with Servo MotorLesson 8- NodeMCU with Servo Motor
Lesson 8- NodeMCU with Servo MotorElaf A.Saeed
 
Lesson 7- NodeMCU with DC Motor
Lesson 7- NodeMCU with DC MotorLesson 7- NodeMCU with DC Motor
Lesson 7- NodeMCU with DC MotorElaf A.Saeed
 
Lesson 6 - NodeMCU with PWM Pin
Lesson 6 -  NodeMCU with PWM PinLesson 6 -  NodeMCU with PWM Pin
Lesson 6 - NodeMCU with PWM PinElaf A.Saeed
 
lesson4 - NodeMCU control led
  lesson4 - NodeMCU control led  lesson4 - NodeMCU control led
lesson4 - NodeMCU control ledElaf A.Saeed
 
lesson2 - Nodemcu course - NodeMCU dev Board
 lesson2 - Nodemcu course - NodeMCU dev Board lesson2 - Nodemcu course - NodeMCU dev Board
lesson2 - Nodemcu course - NodeMCU dev BoardElaf A.Saeed
 
lesson1 - Getting Started with ESP8266
lesson1 -  Getting Started with ESP8266lesson1 -  Getting Started with ESP8266
lesson1 - Getting Started with ESP8266Elaf A.Saeed
 
Embedded system course projects - Arduino Course
Embedded system course projects - Arduino CourseEmbedded system course projects - Arduino Course
Embedded system course projects - Arduino CourseElaf A.Saeed
 
Embedded system introduction - Arduino Course
Embedded system introduction - Arduino CourseEmbedded system introduction - Arduino Course
Embedded system introduction - Arduino CourseElaf A.Saeed
 
Pyton with rasperry pi
Pyton with rasperry piPyton with rasperry pi
Pyton with rasperry piElaf A.Saeed
 
Summary of MATLAB Functions-Part1
Summary of MATLAB Functions-Part1Summary of MATLAB Functions-Part1
Summary of MATLAB Functions-Part1Elaf A.Saeed
 
Python basics_ part1
Python basics_ part1Python basics_ part1
Python basics_ part1Elaf A.Saeed
 

Plus de Elaf A.Saeed (20)

IOT NodeMCU - NodeMCU Webserver
IOT NodeMCU - NodeMCU WebserverIOT NodeMCU - NodeMCU Webserver
IOT NodeMCU - NodeMCU Webserver
 
IOT NodeMCU - Ubidots Platform to Turn on LEDs
IOT NodeMCU - Ubidots Platform to Turn on LEDsIOT NodeMCU - Ubidots Platform to Turn on LEDs
IOT NodeMCU - Ubidots Platform to Turn on LEDs
 
IOT NodeMCU - Thinger Platform to Turn on LEDs
IOT NodeMCU - Thinger Platform to Turn on LEDsIOT NodeMCU - Thinger Platform to Turn on LEDs
IOT NodeMCU - Thinger Platform to Turn on LEDs
 
IOT NodeMCU - IFTTT Templet to send SMS
IOT NodeMCU - IFTTT Templet to send SMSIOT NodeMCU - IFTTT Templet to send SMS
IOT NodeMCU - IFTTT Templet to send SMS
 
Getting date and time from ntp server with esp8266 node mcu
Getting date and time from ntp server with esp8266 node mcuGetting date and time from ntp server with esp8266 node mcu
Getting date and time from ntp server with esp8266 node mcu
 
ESP8266 NodeMCU Server, Client, Station Mode, and Client Control LED
ESP8266 NodeMCU Server, Client, Station Mode, and Client Control LEDESP8266 NodeMCU Server, Client, Station Mode, and Client Control LED
ESP8266 NodeMCU Server, Client, Station Mode, and Client Control LED
 
IOT NodeMCU - NodeMCU Connection to Internet
IOT NodeMCU - NodeMCU Connection to InternetIOT NodeMCU - NodeMCU Connection to Internet
IOT NodeMCU - NodeMCU Connection to Internet
 
Lesson 10- NodeMCU with LCD I2C
Lesson 10- NodeMCU with LCD I2CLesson 10- NodeMCU with LCD I2C
Lesson 10- NodeMCU with LCD I2C
 
Lesson 9- NodeMCU with Arduino UNO (UART)
Lesson 9- NodeMCU with Arduino UNO (UART)Lesson 9- NodeMCU with Arduino UNO (UART)
Lesson 9- NodeMCU with Arduino UNO (UART)
 
Lesson 8- NodeMCU with Servo Motor
Lesson 8- NodeMCU with Servo MotorLesson 8- NodeMCU with Servo Motor
Lesson 8- NodeMCU with Servo Motor
 
Lesson 7- NodeMCU with DC Motor
Lesson 7- NodeMCU with DC MotorLesson 7- NodeMCU with DC Motor
Lesson 7- NodeMCU with DC Motor
 
Lesson 6 - NodeMCU with PWM Pin
Lesson 6 -  NodeMCU with PWM PinLesson 6 -  NodeMCU with PWM Pin
Lesson 6 - NodeMCU with PWM Pin
 
lesson4 - NodeMCU control led
  lesson4 - NodeMCU control led  lesson4 - NodeMCU control led
lesson4 - NodeMCU control led
 
lesson2 - Nodemcu course - NodeMCU dev Board
 lesson2 - Nodemcu course - NodeMCU dev Board lesson2 - Nodemcu course - NodeMCU dev Board
lesson2 - Nodemcu course - NodeMCU dev Board
 
lesson1 - Getting Started with ESP8266
lesson1 -  Getting Started with ESP8266lesson1 -  Getting Started with ESP8266
lesson1 - Getting Started with ESP8266
 
Embedded system course projects - Arduino Course
Embedded system course projects - Arduino CourseEmbedded system course projects - Arduino Course
Embedded system course projects - Arduino Course
 
Embedded system introduction - Arduino Course
Embedded system introduction - Arduino CourseEmbedded system introduction - Arduino Course
Embedded system introduction - Arduino Course
 
Pyton with rasperry pi
Pyton with rasperry piPyton with rasperry pi
Pyton with rasperry pi
 
Summary of MATLAB Functions-Part1
Summary of MATLAB Functions-Part1Summary of MATLAB Functions-Part1
Summary of MATLAB Functions-Part1
 
Python basics_ part1
Python basics_ part1Python basics_ part1
Python basics_ part1
 

Dernier

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Dernier (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

MATLAB Basics-Part1

  • 1. MATLAB Basics-Part1 Learning The Basics Of Matlab Eng. Elaf Ahmed Saeed
  • 3. What is MATLAB?  Matlab is basically a high level language which has many specialized toolboxes for making things easier for us.  How high? Matlab High level languages such as C, Pascal etc. Assembly
  • 4. What is MATLAB?  Matlab stands for MATrix LABoratory.  The first version of MATLAB was produced in the mid 1970s as a teaching tool. MATLAB started as an interactive program for doing matrix calculations.  MATLAB has now grown to a high level mathematical language that can solve integrals and differential equations numerically and plot a wide variety of two and three Dimensional graphs.  The expanded MATLAB is now used for calculations and simulation in companies and government labs ranging from aerospace, car design, signal analysis through to instrument control and financial analysis.  In practice, it provides a very nice tool to implement numerical method.
  • 5. MATLAB Environment  When you start MATLAB, the desktop appears in its default layout.
  • 6. MATLAB Environment  The desktop includes these panels: • Current Folder — Access your files. • Command Window — Enter commands at the command line, indicated by the prompt (>>). • Workspace — Explore data that you create or import from files.
  • 7. MATLAB Startup Window Workspace • View program variables • Double Click on a variable Command Window Type Command Current folder View folders and m-files Command History • View past command • Save a whole session using diary.
  • 8. MATLAB Components MATLAB Workspace Handle graphic Simulink Toolboxes MATLAB Compiler
  • 9. Command Window  After starting MATLAB, the Command window will open with the Command prompt being displayed: >>  The calculator mode of MATLAB operates in a sequential fashion as you type in Commands line by line. For each command, you get a result.  For example, if you type >> 55 – 16 MATLAB will display the result ans= 39 (Try >> ans + 10)
  • 10. Tip  Using the up-arrow key, you can go back to any command that you entered. Press the up-arrow until you get back the line. >> b * a  Alternatively, you can type b and press the up-arrow once and it will automatically bring up the last command beginning with the letter b. the up-arrow shortcut is a quick way to fix errors without having to retype the entire line.
  • 17. Step1  Select (install manually…..)  Next
  • 18. Step 2  Select (yes)  Next
  • 19. Step 3  Select (I have file installation key)
  • 20. Step 4  Copy the from the text file. Copy Key
  • 21. Step 5  Paste the Key
  • 22. Step 6  Select Typically  Next
  • 23. Step 7  Select path to installation  Next
  • 24. Step 8  Click install
  • 25. Step 9  Download window. This step takes some time to install the program.
  • 26. Step 10  After install complete select (Activate manually without internet) Next
  • 27. Step 11  Enter the path of file license.
  • 28. Step 11  The file license with the folder installation.
  • 29. Step 12  Click finish.
  • 30. Step 13  If the shortcut not found you can found it from: C  program files  MATLAB  R2018a  bin  matlab
  • 31. Step 13  If the short cu not found you can found it from: C  program files  MATLAB  R2018a  bin  matlab
  • 32. Step 13  If the short cu not found you can found it from: C  program files  MATLAB  R2018a  bin  matlab
  • 33. Step 13  If the short cu not found you can found it from: C  program files  MATLAB  R2018a  bin  matlab
  • 34. Step 13  If the short cu not found you can found it from: C  program files  MATLAB  R2018a  bin  matlab
  • 35. Step 14  If the MATLAB not open do the following: Copy the (license_standalone.lic) and paste it in the folder (licences) In MATLAB folder in C.
  • 36. Step 14  If the MATLAB not open do the following: Copy the (license_standalone.lic) and paste it in the folder (licences) In MATLAB folder in C.
  • 38. What is Assignment?  Assignment refers to assign values to variable names. This results in the storage of the values in the memory location corresponding to the variables name. >> a = 5
  • 39. Scalars  In MATLAB a scalar is a variable with one row and one column. Scalars are the simple variables that we use and manipulate in simple algebraic equations.  Creating scalars To create a scalar you simply introduce it on the left hand side of an equal sign. >> x = 1; >> y = 2; >> z = x + y;
  • 40. Scalars  Scalar operations MATLAB supports the standard scalar operations using an obvious notation. The following statements demonstrate scalar addition, subtraction, multiplication and division. >> u = 5; >> v = 3; >> w = u + v >> x = u - v >> y = u * v >> z = u/v
  • 41. Scalars  >> a = 4 Note how the assignment echo prints to confirm what you have done: a = 4  Echo printing is a characteristic of MATLAB. It can be suppressed by terminating the command line with the semicolon (;) >> A = 6;  You can type several commands on the same line by separating them with commas or semicolons. For example, >> a = 4, A = 6; x = 1; a = 4
  • 42. Note  MATLAB Treats names in a case-sensitive manner. That is, the variable a is not the same as A. To illustrate this, enter >> a And then enter >> A
  • 43. Complex Number  We can assign Complex Values to variables, since MATLAB handles complex arithmetic automatically. The unit imaginary number −𝟏 is preassigned to the variable i. >> x = 2+i*4 x = 2.0000 + 4.0000i  It should be noted that MATLAB allows the symbol j to be used to represent the unit imaginary number for input. However, it always uses an i for display. For example, >> x = 2+j*4 x= 2.0000+4.0000i
  • 44. Complex Number  Function with complex numbers:
  • 45. Complex Number  Examples: >> x=3+4i ans= 3.0000 + 4.0000i >> real(x) ans= 3 >> imag(x) ans= 4 >> abs(x) ans= 5
  • 46. Complex Number  Examples: >> angle(x) ans= 0.9273 >> conj(x) ans= 3-4i
  • 47. Predefined variables  There are several predefined variables, for examples, pi. >> pi ans= 3.1416  >> exp(1) ans= 2.7183
  • 48. Format type  Note how MATLAB displays four decimal places. If you desire additional precision, enter the following: >> format long  Now when pi is entered the result is displayed to 15 significant figures: >> pi ans= 3.14159263558979
  • 49. Format type  If we want to go back to the four digits after coma write: >> Format short >> pi ans= 3.1416  The following is a summary of the format commands you will employ routinely in engineering and scientific calculations. They all have the syntax: format type
  • 50. Format type Type Result Example short Scaled fixed-point format with 5 digits. 3.1416 long Scaled fixed-point format with 15 digits for double and 7 digits for single. 3.14159265358979 Short e Floating point format with 5 digits. 3.1416e+000 Long e Floating point format with 15 digits for double and 7 digits for single. 3.141592653589793e+000 Short g Best of fixed or floating point format with 5 digits. 3.1416 Long g Best of fixed or floating point format with 15 digits for double and 7 digits for single. 3.14159265358979 Short eng Engineering format with at least 5 digits and a power that is a multiple of 3. 3.1416e+000 Long eng Engineering format with at least 16 digits and a power that is a multiple of 3. 3.14159265358979e+000 bank Fixed dollars and cents 3.14
  • 51. Exercise  Given x=2 y=x+2 z=x+y 1/ Type these simple statements to find the value of y and z. 2/ Try to put the variables in one row and get the answer. 3/ Hide the result of x , y and get only the result of z.
  • 53. Basic Math Operations  Mathematical operations in MATLAB can be performed on both scalars and arrays.  The common operators, in order of priority, are: Symbol Meaning Example ^ Exponentiation 4^2=8 - Negation (unary operation) -8=-8 * / Multiplication and Division 2*pi = 6.2832 Pi/4 = 0.78554 Left Division 62 = 0.3333 + - Addition and Subtraction 3+5=8 3-5 = -2
  • 54. Basic Math Operations  Order of Operations • The order of operations is set first by parentheses, then by the default order given above:  Y= -4 ^ 2 gives y= -16 Since the exponentiation happens first due to its higher default priority, but  Y = (-4) ^ 2 gives y = 16 Since the negation operation on the 4 takes place first.
  • 55. Basic Math Operations  Examples 1. Compute (2+3-9)*7^2/4 ans = -49 2. 1 2+32 + 4 5 x 6 7 In MATLAB it becomes >> 1/(2+3^2) + 4/5 * 6/7 ans = 0.7766 Or, if parentheses are missing, >> ½+3^2 + 4/5 * 6/7 ans = 10.1857 3. >> -5 / (4.8) + 5.32) ^ 2 ans = -0.0488
  • 56. Basic Math Operations  Calculations can also involve complex quantities. x = (2+4i) and y = (16): >> 3 * x ans = 6.0000 + 12.0000i >> x ^ 2 ans = -12.0000 + 16.0000i >> x + y ans = 18.0000 + 4.0000i >> (3 + 4i) * (3 – 4i) ans = 6.1230e-017
  • 57. Relational Operations  A > B The result is true if A is greater than B, and is false otherwise.  A < B The result is true if A is less than B, and is false otherwise.  A >= B The result is true if A is grater than or equal to B, and is false otherwise.  A <= B The result is true if A is less than or equal to B, and is false otherwise.  A == B The result is true if A is equal to B, and is false otherwise.  A ~= B The result is true if A is not equal to B, and is false otherwise. Example: >> a = 5; b = 2; >> x = a > b ; x = 1 (true) >> x = (a == b); x = 0 (false)
  • 58. Relational Operations  Note Many users confuse the double equality sight (==) used in relational tests with the equality sign (=) used in assignments. When a user uses (=) instead (==), MATLAB usually reports that an expected relational operator wasn’t found.
  • 59. Relational Operations  The table summary of relational operators in MATLAB. Example Operator Relational X == 0 == Equal Unit ~= ‘m’ ~= Not equal A < 0 < Less Than S > t > Greater Than 3.9 <= a/3 <= Less than or equal to R >= 0 >= Greater than or equal to
  • 60. Logical Operations  The table that summarize the logical operation in MATLAB. Operator Operation & Logical AND && Logical AND with shortcut evaluation | Logical OR || Logical OR with shortcut evaluation xor Logical exclusive OR ~ Logical NOT
  • 61. Logical Operations  A truth table summarize the possible outcomes for logical operators employed in MATLAB. The order of priority is shown at the top of the table. Highest Lowest X Y ~X X&Y X|Y T T F T T T F F F T F T T F T F F T F F
  • 62. Logical Operations • ~x (NOT): true if x is false (or zero); false otherwise. • x & y (NND): true if both x and y are true (or non-zero). • x | y (OR): true if either x or y are true (or non-zero).  Priority can be set using parentheses. After that, mathematical expressions are highest priority, followed by relational operators, followed by logical operators. All things being equal, expression are performed from left to right.  Not is the highest priority logical operator, followed by AND and finally OR.
  • 63. Logical Operations  Examples: >> 1 & 1 ans = 1 >> 1 | 0 ans = 1 >> 0 & 0 ans = 0 >> ~1 ans = 0
  • 65. Built-in Functions  MATLAB and its toolboxes have a rich of collection of built-In functions. You can use online help to find out more about them. For example, if you want to learn about the log function: >> help log LOG Natural logarithm. LOG(x) is the natural logarithm of the elements of x. Complete results are produced if x is not positive. See also log2, log10, exp, logm.
  • 66. Built-in Functions with vector and Matrix  One of the important properties of MATLAB’S built-in functions is that they will operate directly on vector and matrix quantities. For example, try >> A = [1 2 3; 4 5 6; 7 8 9] >> log(A) ans = 0 0.6931 1.0986 1.3863 1.6094 1.7918 1.9459 2.0794 2.1972
  • 67. Built-in Functions with vector and Matrix  You will see that the natural logarithm function is applied in array style, element by element, to the matrix A. Most functions, such as sqrt, abs, sin, acos, tanh, and exp, operate in array fashion. >> sqrt(A) ans = 1.0000 1.4142 1.7321 2.0000 2.2361 2.4495 2.6458 2.8284 3.0000
  • 68. Built-in Functions with vector and Matrix  Length and Size functions  The length and size functions in MATLAB are used to find dimensions of vector and matrices. The length function returns the number of elements in a vector. The size function returns the number of rows and columns in a vector or matrix. For example >> vec(-2:1) vec = -2 -1 0 1 >> length(vec) ans = 4 >> size(vec) ans = 1 4
  • 69. Built-in Functions with vector and Matrix  Note The length function will return either the number of rows or the number of columns, whichever is largest.  we can create a matrix of zeros with the same size as another matrix. For a matrix variable vec, the following expression would accomplish this: zeros(size(vec)) The size function returns the size of the matrix, which is then passed to the zeros function, which then returns a matrix of zeros with the same size as vec.  What would be produced by the following command line: >> rand(size(vec))
  • 70. Round-off Functions  There are several functions for rounding. For example, suppose that we enter a vector: >> x = [-1.6-1.5-1.41.4 1.5 1.6] • The round function rounds the elements of x to the nearest integers: >> round(x) ans = -2 -2 -1 1 2 2 • The ceil (short for ceiling) function rounds to the nearest integers toward infinity: >> ceil(x) ans = -1 -1 -1 2 2 2
  • 71. Round-off Functions • The floor function rounds down to the nearest integers toward minus infinity: >> floor(x) ans = -2 -2 -2 1 1 1
  • 72. Built-In Help Functions  The built-in help function can give you information about both what exists and how those functions are used: • help elmat will list the elementary matrix creation and manipulation functions, including functions to get information about matrices. • help elfun will list the elementary math functions, including trig, exponential, complex, rounding, and reminder functions.  The built-in lookfor command will search help files for occurrences of text and can be useful if you know a function’s purpose but not its name. (to stop search click (ctrl+c))
  • 73. Summary of Built-in Functions in MATLAB  General Functions Function Used to who List current variables. whos List current variables in details clear x Clear variable x from memory open Open new file figure(n) Opens new figure numbered n close Closes last figure Close all Closes all figures dir List files in directly format Set output format help To know any command and example lookfor To search for command exit Quits MATLAB
  • 74. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Basic Arithmetic • Addition • Subtraction
  • 75. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Basic Arithmetic • Multiplication • Division
  • 76. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Basic Arithmetic • Powers • Transpose
  • 77. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Basic Arithmetic • Modulo Division and Rounding
  • 78. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Trigonometry • Sine
  • 79. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Trigonometry • Cosine
  • 80. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Trigonometry • Tangent
  • 81. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Trigonometry • Cosecant
  • 82. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Trigonometry • Secant
  • 83. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Trigonometry • Cotangent
  • 84. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Exponents and Logarithms
  • 85. Summary of Built-in Functions in MATLAB  Elementary Math (Arithmetic Operations) Complex Numbers
  • 86. Summary of Built-in Functions in MATLAB  Elementary Math (Linear Algebra) Matrix Operations
  • 87. Summary of Built-in Functions in MATLAB  Elementary Math (Random Number Generation)
  • 88. Summary of Built-in Functions in MATLAB  Elementary Math (2-D and 3-D Plots) Line Plots
  • 89. Summary of Built-in Functions in MATLAB  Elementary Math (2-D and 3-D Plots) Function Plots
  • 90. Summary of Built-in Functions in MATLAB  Elementary Math (Labels and Annotations) Labels
  • 91. Summary of Built-in Functions in MATLAB  Elementary Math (Labels and Annotations) Multiple Plots
  • 92. Summary of Built-in Functions in MATLAB  Elementary Math (Help and Support)
  • 93. Summary of Built-in Functions in MATLAB For more learn : https://uk.mathworks.com/help/matlab/referencelist.html?type=functio n&s_tid=CRUX_topnav
  • 94. Vectors and Matrices in MATLAB
  • 95. Arrays, Vectors, and Matrices Introduction  An array is a collection of values that are represented by a single variable name.  MATLAB can automatically handle rectangular arrays of data-one- dimensional arrays are called vectors and two-dimensional arrays are called matrices.  Arrays are set off using square bracket [ ] in MATLAB.  Entries within a row are separated by space or commas.  Rows are separated by semicolons.
  • 96. Array Examples  Row Vector >> a=[1 2 3 4 5 ] a= 1 2 3 4 5  Columns Vector >> b = [2;3;6;8;10] b= 2 4 6 8 10
  • 97. Use of Vector  MATLAB indexing starts with 1, not 0. • We will not respond to any emails where this is the problem.  A(n) returns the 𝑛 𝑡ℎ element. A=[13 5 9 10]  The index argument can be a vector. In this case, each element is looked up individually, and return as a vector of the same size as the index vector. A(1) A(2) A(3) A(4)
  • 98. Matrices  A 2-D array, or matrix, of data is entered row by row, with spaces (or commas) separating entries with row and semicolons separating the rows: >> A= [1 2 3; 4 5 6; 7 8 9] A= 1 2 3 4 5 6 7 8 9
  • 99. Useful Array Commands  The transpose operator (apostrophe) can be used to flip an arrays over its own diagonal. For example, if b is a row vector, b’ is a column vector containing the complex conjugate of b. (Try b’)  The command window will allow you to separate rows by hitting the enter key – script file and functions will allow you to put rows on new lines as well. (Retype the matrix A as describe above).  The who command will report back used variable names; whos will also give you the size, memory, and data types for the arrays.
  • 100. Accessing Matrix Entries  Individual entries within array can be both read and set using either the index of the location in the array or the row and column.  The index value starts with 1 for the entry in the top left corner of an array and increases down a column – the following shows the indices for a 4 row, 3 column matrix: 1 5 9 2 6 10 3 7 11 4 8 12
  • 101. Accessing Matrix Entries(Cont)  Accessing some matrix C: C= 2 4 9 3 3 16 3 0 8 10 13 17 C(2) Would report 3 C(4) Would report 10 C(13) Would report an error!
  • 102. Accessing Matrix Entries(Cont)  Entries can also be access using the row and column: C(2,1) Would report 3 C(3,2) Would report 0 C(5,1) Would report an error! C(2,1) Would report 3 C(4,:) Would report the whole fourth raw. C(:,2) Would report the whole second column.
  • 103. Array & Matrix Creation-Built In  There are several built-in functions to create arrays and matrices: • zeros(r,c) Will create an r row by c column matrix of zeros. • zeros(n) Will create an n by n matrix of zeros. • ones(r,c) Will create an r row by c column matrix of ones. • ones(n) Will create an n by n matrix of ones. Try… rand(n) and rand(r,c)  help elmat has, among other things, a list of the elementary matrices. Try… zeros(3) , ones(2)
  • 104. Array Creation-Colon Operator  The colon operator : is useful in several contexts. It can be used to create a linearly spaced array of points using the notation. Start:diffval:limit Where start is the first value in the array, diffval is the difference between successive values in the array, and limit is the boundary for the last value (though not necessary the last value). >> 1:6:3 ans= 1.0000 1.6000 2.2000 2.8000
  • 105. Array Creation-Colon Operator  Note  If diffval is omitted, the default value is 1: >> 3:6 ans= 3 4 5 6  To create a decreasing series, diffval must be negative: >> 5:-1.2:2 ans= 5.0000 3.8000 2.6000
  • 106. Array Creation-Colon Operator (Cont)  If start+diffval>limit for an increasing series or start+diffval>limit foe decreasing series, an empty matrix is returned: >> 5:2 ans= Empty matrix: 1-by-0  To create a column, transpose the output of the colon operator, not the limit value; that is, (3:6)’ not 3:6’
  • 107. Array Creation- linespace  To create a row vector with a specific number of linearly spaced points between two numbers, use the linspace command  Linspace (x1,x2,n) will create a linearly spaced array of n points between x1 and x2 >> linspace(1, 1, 6) ans= 0 0.2000 0.4000 0.6000 0.8000 1.0000  If n is omitted, 100 points are created.  To generate a column, transpose the output of the linspace command.
  • 108. Array Creation- linspace (Cont) >> linspace(0,1,6)’ ans= 0 0.2000 0.4000 0.8000 1.0000
  • 109. Exercise(1)  Use the linspace function to create vectors identical to the following created with colon notation:  t = 5:6:30  x = -3:4  Homework y = -4:2
  • 110. Array Creation- logspace (Cont)  To create a row vector with a specific number of logarithmically spaced points between two numbers, use the logspace command.  Logspace(x1, x2, n) will create a logarithmically spaced array of n points between 10 𝑥1 and 10 𝑥2 . >> logspace (-1, 2, 4) ans= 0.1000 1.0000 10.0000 100.0000  If n is omitted, 100 points are created.  To generate a column, transpose the output of the logspace command.
  • 111. Character Strings & Ellipsis  Alphanumeric are enclosed by apostrophes (‘) >> f= ‘ Mailes‘ >> s = ‘Davis’ Concatenation: passing together of strings >> x= [f s] x= Miles Davis Ellipsis (…): Used to continue long lines >> a = [1 2 3 4 5 … 6 7 8] a= 1 2 3 4 5 6 7 8
  • 112. Character Strings & Ellipsis  You cannot use an ellipsis within single quotes to continue a string. But you can piece together shorter strings with ellipsis. >> quote = [‘Hello Word’ … ‘I am a Teacher’] quote = Hello Word I am a Teacher
  • 113. Vector-Matrix Calculations  MATLAB can also perform operations in vectors and matrices.  The * operator for matrices is defined as the product or what is commonly called “matrix multiplication.” • The number of columns of the first matrix must match the number of rows in the second matrix. • The size of the result will have as many rows as the first matrix and as many columns as the second matrix. • The exception to this is multiplication by a 1x1 matrix, which is actually an array operation.  The ^ operator for matrices result in the matrix being matrix-multiplied by itself a specified number of times. • Note – in this case, the matrix must be square! Why? Because, matrix-multiplied by itself .
  • 114. Element-by-Element Calculations  At time, you will want to carry out calculation item by item in a matrix or vector. The MATLAB manual calls these array operations. They are also often referred to as element-by-element operations.  MATLAB defines .* and ./ (note the dots) as the array multiplication and array division operators. • For array operations, both matrices must be the same size or one of the matrices must be 1x1.  Array exponentiation (raising each element to a corresponding power in another matrix) is performed with .^  Again, for array operations, both matrices must be the same size or one of the matrices must be 1x1.
  • 115. Notes  To define the matrix using another matrix: B=[1.5, 3.1]; S=[3.0 B]; This is equal to S=[3.0 1.5 3.1]  Changing the value in a matrix: Lets consider that we have matrix S, S(2)=1.5 To change the second value of S from 1.5 to -1.0 We type: S(2) = -1.0;
  • 116. Simple Practice  a= 1 2 3 b= 2 4 6 c= 78 9 Try  a+b {is it possible?!} what about a+c  a.c  b.a  a/pi  a^2
  • 117. Examples about Vectors  1- >> x = [12 13 5 8] x (2:3); a = [ 13 5]; b = x(1 : end-1);  b = [12 13 5];  2- >> a[1 : 5] a = 1 2 3 4 5  3- >> a = [1 : 2 : 5] a = 1 3 5
  • 118. Examples about Vectors  4- >> b = prod (x) b = 120  5- >> a = [5E-1 7E-2 9e-4] a= 0.5000 0.0700 0.0009
  • 119. Examples about Special Matrices  1- >> a = zeros (2,3) a = 0 0 0 0 0 0  2- >> a = ones (2,3) a = 1 1 1 1 1 1
  • 120. Examples about Special Matrices  3- >> eye(4) a = 10 0 0 0 1 0 0 0 0 1 0 0 0 0 1  4- >> x = [1 2; 1 6] x = 1 2 1 6 y= 1.5000 -0.5000 -0.2500 0.2500
  • 121. Examples about Special Matrices  5- >> r = magic(4)  generate a random matrix according to the number between Parentheses.  6- >> x = [1 2; 4 2] >> max(x) ans = 4 2 >> max (max(x)) ans = 4
  • 122. Examples about Special Matrices  7- >> x = [1 2 3; 4 5 6]; >> reshape(x,2,3) ans = 1 2 3 4 5 6 >> reshape(x,3,2) ans = 1 5 4 3 2 6
  • 123. Examples about Special Matrices  8- >> fliprl(x) ans = 3 2 1 6 5 4  9- >>flipud(x) ans = 4 5 6 1 2 3  >> size(x) ans = 2 3
  • 124. Matrix Manipulation Functions Function Used to zeros Create an matrix of all zeros. length Length of matrix. ones Create an matrix of all ones. eye Identity matrix. rand Uniformly distributed random number. size Return matrix dimension. Fliprl Flip matrices left-right. Flipud Flip matrices up and down. size Dimension of matrix.
  • 125. Examples about operation on Matrices  1- Transpose >> a = [1 2; 3 4] a= 1 2 3 4 >> a’ ans = 1 3 2 4
  • 126. Examples about operation on Matrices >> transpose (a) ans = 1 3 2 4 >> rot90 (a) ans = 2 4 1 3 >> rot90(rot90(a))  180 deg. ans = 4 3 2 1
  • 127. Examples about operation on Matrices  2- Addition must be the two matrices have the dimention. >> a = [1 2; 3 4]; >> b = [1 2; 3 4]; >> a + b ans = 2 4 6 8 >> a + 2 ans = 3 4 5 6
  • 128. Examples about operation on Matrices  3- Multiplication >> a = [1 2; 3 4]; >> b = [1 2; 3 4]; >> a * b ans = 7 10 15 22 >> 2 * a ans= 2 4 6 8
  • 129. Examples about operation on Matrices >> C = ones(2,4) C = 1 1 1 1 1 1 1 1 >> a * C ans = 3 3 3 3 7 7 7 7
  • 130. Examples about operation on Matrices  4- Division >> a = [1 2; 3 4]; >> b = [1 2; 3 4]; >> a/b ans = 1 0 0 1 Or: >> a*inv(b) ans = 1.0000 0.0000 0.0000 1.0000
  • 131. Examples about operation on Matrices  5- Dot Multiplication >> a = [1 2; 3 4]; >> b = [1 -1; 2 -2]; >> c = a .* b ans = 1 -2 6 -8
  • 132. Examples about operation on Matrices  6- Dot Division >> c = a ./ b  7- Dot Power >> c = a .^ b  8- Inverse >> a = [1 2; 3 4] >> b = inv (a) b = -2.0000 1.0000  1.5000 -0.5000 Or: >> b = a ^ -1 b = -2.000 1.000 1.5000 -0.5000
  • 133. Examples about operation on Matrices  9- Determinant >> a = det (a) d = 0  10- Power >> a = [1 2; 3 4] a = 1 2 3 4 >> b = a ^2 b = 7 10  15 22 • The matrix must be square. • a .^2 is equivalent a *a
  • 134. Examples about operation on Matrices  11- Summation - >> a = [1 3 5] a = 1 3 5 >> s = sum(a) s = 9 - >> a = [12; 3 4]; >> sum(sum(a)) ans = 10
  • 135. Examples about operation on Matrices  12- Rank 1- rank(A) provides an estimate of the number of linearly independent rows or columns of a matrix A. 2- trace (A) sum of the diagonal elements of A. >> A = [1 2 5; 0 1 7; 2 3 4] >> rank (A) ans = 3 >> trace (A) ans = 6
  • 136. Matrix Sub-scripting  P = pascal(n) returns a Pascal’s Matrix of order n. P is a symmetric positive definite matrix with integer entries taken from Pascal's triangle. The inverse of P has integer entries.  >> p = pascal(5) a = 1 1 1 1 1 1 2 3 4 5 1 3 6 10 15 1 4 10 20 35 1 5 15 35 70
  • 137. Matrix Sub-scripting (Cont) >> a(3, 2) ans = 3 >> a (8) ans = 3 >> a (: , 3) 1 3 6 10 15
  • 138. Matrix Sub-scripting (Cont) >> a (3:4 , 3:4) ans = 1 3 >> a (1:5, 3) ans = 1 3 6 10 15
  • 139. Matrix Sub-scripting (Cont) >> [a(7) a(17) ; a(9)] z = 2 4 4 20 Or: >> z = [a(2,2) a(2,4); a(4,2) a(4,4)] z = 2 4 4 20
  • 140. Matrix Sub-scripting (Cont) >> a (:, [1 2]) ans= 1 1 1 2 1 3 1 4 >> a (: , 5) = [ ]  Delete the rows of the specific column. 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20 1 5 15 35
  • 141. Matrix Sub-scripting (Cont) >> a (3 , :) = [ ]  delete the third row a= 1 1 1 1 1 2 3 4 1 4 10 20 1 5 15 35
  • 142. Concatenation Examples >> x = [1 3 4 5] x= 1 3 4 5 >> y = [x 1] y = 1 3 4 5 1 >> y = [x; 2*x; 3*x] y = 1 3 4 5 2 6 8 10 3 9 12 15
  • 143. Concatenation Examples >> z = [2 3 4 5] z = 2 3 4 5 >> y = [x; 2*x; 3*x; z] y = 1 3 4 5 2 6 8 10 3 9 12 15 4 3 4 5
  • 144. Matrix Manipulation Functions Function Syntax Description Example Determinant B=det(A) B(a number) is the determinant of A (a square Matrix) B=det(A) Inverse D=inv(A) D (a square matrix) is the inverse of A (a square matrix) A=inv(B*C) Rank N=rank(A) N (a number) is the rank of A (a matrix) h= rank(A) - 1 Diagonal A=diag(c) A is the main diagonal elements of c. A= 2+diag(8) Sum A=sum(B) A is the sum of all elements of B if it is a vector or sum of column of B if it is a matrix. A=sum (x) Transpose A= transpose(b) A is the non conjugate transpose of b. A= transpose(b) Or (A = b’)
  • 145. Exercise (2)  Write the MATLAB commands that will create the following matrices:  Now try to find (if the answer exist check that it is correct by hand) >> b + c …………………………………………………………. >> b - 2*c ……………………………………………………….. >> a + b ………………………………………………………….
  • 146. Exercise (2)  Calculate b*a by hand and then use MATLAB to check your answer.  Now check that MATLAB gives the same results for b^3 and b*b*b (you don’t need to do these by hand)  Does the product a*b make sense?  How can we display the second raw of matrix a?  Change the value of c(4) to 10.
  • 147. Exercise (3)  Find an efficient way to generate the following matrix: mat= 10 20 30 40 -6 -4 -2 0 Then, give an expression that will refer to the first two column.
  • 148. linkden: www.linkedin.com/in/elaf-a-saeed-97bbb6150 facebook: https://www.facebook.com/profile.php?id=100004305557442 github: https://github.com/ElafAhmedSaeed youtube: https://youtube.com/channel/UCE_RiXkyqREUdLAiZcbBqSg slideshare: https://www.slideshare.net/ElafASaeed SlidePlayer: https://slideplayer.com/slide/18030079/ Google Scholar: https://scholar.google.com/citations?user=VIpVZKkAAAAJ&hl=ar&gmla= AJsN-F7PIgAjWJ44Hzb18fwPqJaaUmG0XzbLdzx09