Part02 Matlab Fundamentals
Part02 Matlab Fundamentals
MATLAB FUNDAMENTALS
What to be covered in this
part?
• Introduction to MATLAB
Environment
• Matrices, Polynomials,
Introduction to M-files (scripts),
statistical functions, etc
Operating Environment
• Starting Matlab:
Start > All Programs > Matlab > R2015b
Start > Matlab R2015b (if pinned there)
Can also create a shortcut on the desktop
• Matlab Desktop:
Title bar (displays Matlab version), Menus,
toolbar and some windows are displayed.
Possible windows are: Command window,
Current folder, Command history and
workspace
Maximize the Matlab desktop for better view
Windows can be closed and re-opened.
desktop > desktop layout > default to view
default windows
The Matlab Desktop
Current Folder
Workspace
Command window
Command History
The Toolstrip
B=[4 5 7
7 8 9
12 34 56];
C=B;
C(:,2)=[] % deletes the second column of C
Ax B x A 1B %(inv(A) * B)
part2ex04A.m
x y 2 z 2 A = [-1 1 2; 3 -1 1;-1
3 4];
3x y z 6 B = [2;6;4];
x 3 y 4 z 4 x = A\B
% x=x(1)=1;
y=x(2)=-1; z=x(3)=2
1 1 2 x 2
3 1 1 y 6 part2ex04B.m
1 3 4 z 4 compares time
taken for inv(A)*B
and A\B using tic
and toc
Statistical Functions
• Functions: min, max, mean, median, std,
sum, prod, cumsum, cumprod. Example for
vectors: part2ex06.m
• For matrices, they operate columnwise.
Example for matrices: part2ex07.m
• When used with two output arguments, min
and max columns, they return also the
corresponding index (or location) of the min or
max value respectively.
[minval,indices] = min(X) % min value
and indices are returned
• minvalue=min(X) % only min. value is
returned
Example part2ex08.m Extra: mean(x,1) –
column;mean(x,2)-row
Sorting
• For vectors, sort(x) sorts the elements of x in
ascending order. Refer to part2ex09.m
• For matrices, sort(x) sorts each column of x in
ascending order. Refer to part2ex10.m
• sort(x,1) sort(x) - sorts columnwise
• sort(x,2) sorts rowwise. Refer to part2ex11.m
• sort(x,'ascend') =>ascending order,
sort(x,‘descend') =>descending order. Check if
Matlab version you use supports this. R2010B
support it. Extra: sort(x,1,’descend’)
• sortrows(A) sort rows in ascending order using the
first Column. A must be either a matrix or a column
vector.
• sortrows(A, column) performs sorting the way Excel
performs sorting. Default is ascending. For
Arithmetic, Relational and Logical
Operators
• Used by find function
• Arithmetic: + - ./ .* .\ .^ ^ : .’ ’ * /
• Relational: <, <=, >, >=, ==, ~= (not equal)
• Logical operators: & , ¦ and ~
• For precedence, refer to full Matlab lecture
notes (posted to students).
• Extra: Also you can get precedence using: help
precedence at the command window.
find function (1) – Very Powerful
• indices = find(x) returns the indices of the
array x that point to nonzero elements. If none
is found, find returns an empty matrix. It is
equivalent to indices=find(x~=0)
x = [1 0 4 -3 0 0 0 8 6];
indices = find(x)
returns linear indices for the nonzero entries of x.
• indices = 1 3 4 8 9
• To get elements, use: x(indices). This will
return:
1 4 -3 8 6
• find(x) regards x as x(:), which is the long
column vector formed by concatenating the
columns of x.
find function (2)
• [r,c] = find(x) returns the row and column
indices of the nonzero entries in the matrix x.
Example:
z=[8 0 6; 3 5 7; 4 9 0];
[r,c]=find(z)
[r,c] % this shows actual row and column
combination
Note: x(find(x)
Output:
x(find(x~=0))
1 1
2 1
3 1 To find elements which are
2 2 positive use: x(find(x>0))
3 2
1 3
find function (3)
• can combine logical and relational operators
with find function
x = [-3 11 0 33 0 55];
x(find( x>0 & x < 10*pi)) % do not use
&&, it will result into errors
• This gives values which are greater than 0 but
less than 10. Qtn: How many elements?
• To determine how many elements meets
certain criteria, combine the length function
and find function. Example:
length(find(condition))
• Example: part2ex12.m demonstrates how you
can summarize test scores, as shown in the
next slide.
Find function (4)
marks=[23 45 99 67 56 43 21 67 89 100
35];
format compact
A_grade=length(find(marks>=70))
B_plus_grade=length(find(marks>=60 &
marks<70))
B_grade=length(find(marks>=50 &
marks<60))
C_grade=length(find(marks>=40 &
marks<50))
D_grade=length(find(marks>=35 &
marks<40))
E_grade=length(find(marks>=0 & marks<35))
Polynomials (1)
• Represented as vectors containing the
polynomial coefficients in descending order
p ( x) an x n an 1 x n 1 an 2 x n 2 a1 x a0
Example: p(x)=x3+5x2-3x+6 p=[1 5 -3 6];
• The roots function finds roots of polynomials
x3+5x2-3x+6=0 p=[1 5 -3 6]; roots(p)
Important: If some coefficients are missing, they
must be replaced by zeros !!!
** Can be used to solve quadratic equation!
• The poly function is used to form a polynomial
from its roots. It returns the coefficients of the
polynomial as a row vector. Example:
p2=poly([-1 -2]) returns p2= 1 3 2
Polynomials (2)
• Polyval function evaluates a polynomial at a
point. Syntax is:
polyval(polynomial,point)
• Polynomials are multiplied and divided using
the conv and deconv commands respectively
• C = conv(A, B) multiplies two polynomials
part2ex13.m demonstrates how you can
multiply two polynomials p(s)*q(s) given by
p(s)=s3+2s2+3s+4 and q(s)= s4+3s2-4s
Results: [1 2 6 6 1 0 -16 0]
• [Q,R] = deconv(B,A) deconvolves vector A out
of vector B (i.e. B/A) The result is returned in
vector Q and the remainder in vector R such
that
B = conv(A,Q) + R
demonstrates the value of q(s)/p(s) given above
[Q,R]=deconv([1 0 3 -4 0],[1 2 3 4])
Derivatives and Integrals of
Polynomials
• For derivative, use polyder function
• For integrals, use polyint function
• Use doc polyder and doc polyint for examples
and more details. Examples:
p=[2 4 -5 6]; der=polyder(p) der = 6 8 -5
intg=polyint(p)
intg =0.5000 1.3333 -2.5000 6.0000 0
k = polyder(a,b) returns the derivative of the
product of the polynomials a and b =>
polyder(conv(a,b))
[num,den] = polyder(b,a) returns the numerator
num and denominator den of the derivative of
the polynomial quotient b/a. demo:
d/dx((2x+3)/4x) 7.6
3 2
Homework: Write a series (2of 4 x 5statements
x Matlab x 6)dx to
1.5
find the value of:
Importing data from Excel and text files
• Importing from Excel or text file, Go to:
Home>Variable> import Data…
• In the resulting dialog box, select .xls file
• Specify the range to import. You can drag and
select.
• Demo: using Import_Excel_data.xls
• Can also generate code for importing data
• Importing from text file:
• Demo: import_text_data.txt file
More on Importing data from text files
Use load filename.txt
• Reads content of file in single step and stores
in variable called <filename>.
• MATLAB can handle tab or space-delimited
text.
• Requires data in file to be organised into
rectangular array. No column titles.
• There should be an equal number of elements
in each row. One row per line.
• For help on import data: Matlab help >
Matlab > user's Guide > Data import and
Export > Import data > Import
Spreadsheets/Import Text Data Files
== END ==