MATLAB Tutorial - 26-07-10
MATLAB Tutorial - 26-07-10
Introduction
MATLAB Environment
MATLAB TUTORIAL Getting Help
Variables
Vectors, Matrices, and Linear Algebra
Mathematical Functions and Applications
Plotting
Selection Programming
M-Files
User Defined Functions
Specific Topics
Reference: Engineering Problem Solving Using MATLAB, by Professor Gary Ford, University of California, Davis.
MATLAB
Introduction Environment
What is MATLAB ? To start MATLAB:
• MATLAB is a computer program that combines computation and START PROGRAMS
visualization power that makes it particularly useful tool for MATLAB 6.5 MATLAB
engineers.
6.5
• MATLAB is an executive program, and a script can be made with a
list of MATLAB commands like other programming language. Or shortcut creation/activation
MATLAB Stands for MATrix LABoratory. on the desktop
• The system was designed to make matrix computation particularly
easy.
The MATLAB environment allows the user to:
• manage variables
• import and export data
• perform calculations
• generate plots
• develop and manage files for use with MATLAB.
Display Windows
Display Windows (con’t…)
Graphic (Figure) Window
Displays plots and graphs
Created in response to graphics
commands.
M-file editor/debugger window
Create and edit scripts of commands called
M-files.
1
Getting Help Getting Help (con’t…)
Google “MATLAB helpdesk”
type one of following commands in the command Go to the online HelpDesk provided by
window: www.mathworks.com
help – lists all the help topic
help topic – provides help for the specified topic
help command – provides help for the specified command
help help – provides information on use of the help command
You can find EVERYTHING you
2
Vectors (con’t…) Vectors (con’t…)
Vector Addressing – A vector element is addressed in MATLAB with an integer Some useful commands:
index enclosed in parentheses.
Example: x = start:end create row vector x starting with start, counting by
one, ending at end
>> x(3)
x = start:increment:end create row vector x starting with start, counting by
ans = increment, ending at or before end
1.5708 3rd element of vector x create row vector x starting with start, ending at
linspace(start,end,number)
The colon notation may be used to address a block of elements. end, having number elements
start is the starting index, increment is the amount to add to each successive index, and end y = x’ transpose of vector x
is the ending index. A shortened format (start : end) may be used if increment is 1. dot (x, y) returns the scalar dot product of the vector x and y.
Example:
>> x(1:3)
ans =
0 0.7854 1.5708 1st to 3rd elements of vector x
3
Matrices Matrices (con’t…)
A Matrix array is two-dimensional, having both multiple rows and multiple columns,
similar to vector arrays: Matrix Addressing:
it begins with [, and end with ] -- matrixname(row, column)
spaces or commas are used to separate elements in a row -- colon may be used in place of a row or column reference to select
semicolon or enter is used to separate rows. the entire row or column.
Example:
•Example: recall:
A is an m x n matrix. >> f(2,3)
>> f = [ 1 2 3; 4 5 6] f=
f=
ans = 1 2 3
1 2 3
4 5 6 6 4 5 6
>> h = [ 2 4 6
1 3 5] >> h(:,1) h=
h=
ans = 2 4 6
2 4 6
1 3 5 2
1 3 5
the main diagonal
1
randn Normally distributed size (A) for a m x n matrix A, returns the row vector [m,n]
containing the number of rows and columns in
random elements matrix.
4
Solutions to Systems of Linear Equations
(con’t…) Matrices
Solution by Matrix Inverse:
Ax = b
Solution by Matrix Division:
The solution to the equation
Entering Matrices
A-1Ax = A-1b
x = A-1b
Ax = b
can be computed using left division.
Magic
MATLAB:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
MATLAB:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
Sum
>> b = [ 10; 5; -1];
>> x = inv(A)*b
>> b = [ 10; 5; -1];
>> x = A\b
Diag
x= x=
-2.0000 -2.0000 Transpose
5.0000 5.0000
-6.0000 -6.0000 Subscripts
Answer: Answer:
x1 = -2, x2 = 5, x3 = -6 x1 = -2, x2 = 5, x3 = -6 Colon Operator
NOTE:
left division: A\b b ÷ A right division: x/y x ÷ y
Example:
t = linspace (-1, 1, 101);
x = 2*cos(2*pi*t);
y = 2*cos(2*pi*(t-0.125));
z = 2*sin(2*pi*t);
5
Polynomials (con’t…) Partial Fraction Expansion (PFE)
Operation MATLAB command Description
Addition c=a+b sum of polynomial A and B, the coefficient vectors must be the
same length.
Scalar Multiple b = 3*a multiply the polynomial A by 3 B (s ) , where B(s) and A(s) are polynomials.
H ( s) =
Polynomial c = conv(a,b) returns the coefficient vector for the polynomial resulting from A( s )
Multiplication the product of polynomial A and B.
Polynomial [q,r] = deconv(a,b) returns the long division of A and B.
Division q is the quotient polynomial coefficient, and r is the remainder H(s) can also be represented in the following form:
polynomial coefficient.
Derivatives polyder(a) returns the coefficients of the derivative of the polynomial A c1 c c
H ( s) = + 2 + ... + n
polyder(a, b) returns the coefficients of the derivative of the product of s − r1 s − r2 s − rn
polynomials A and B.
[n,d]=polyder(b,a) returns the derivative of the polynomial ratio B/A, represented
as N/D
Find Roots roots(a) returns the roots of the polynomial A in column vector form.
Find Polynomials poly(r) returns the coefficient vector of the polynomial having roots r.
NOTE: a, b, and c are polynomial coefficient vectors whose corresponding polynomials are A, B, and C
PFE(con’t…) Plotting
For more information on 2-D plotting, type help graph2d
Example (Distinct Real Roots): Plotting a point: the function plot () creates a
s+2 c c c >> plot ( variablename, ‘symbol’) graphics window, called a Figure
H ( s) = = 1 + 2 + 3 window, and named by default
s 3 + 4s 2 + 3s + 0 s − r1 s − r2 s − r3 “Figure No. 1”
6
Plotting (con’t…) Plotting (con’t…)
Adding new curves to the existing graph:
Use the hold command to add lines/points to an existing plot.
hold on – retain existing axes, add new curves to current axes. Axes are
rescaled when necessary.
hold off – release the current figure window for new plots
Grids and Labels:
Command Description
grid on Adds dashed grids lines at the tick marks
grid off removes grid lines (default)
grid toggles grid status (off to on, or on to off)
title (‘text’) labels top of plot with text in quotes
xlabel (‘text’) labels horizontal (x) axis with text is quotes
ylabel (‘text’) labels vertical (y) axis with text is quotes
text (x,y,’text’) Adds text in quotes to location (x,y) on the current axes, where (x,y) is in
units from the current plot.
7
Loops M-Files
•Example (for loop): So far, we have executed the commands in the command window.
for loop
for t = 1:5000
for variable = expression y(t) = sin (2*pi*t/10);
But a more practical way is to create a M-file.
commands end
The M-file is a text file that consists a group of
end •Example (while loop):
while loop
EPS = 1; MATLAB commands.
while ( 1+EPS) >1
while expression EPS = EPS/2; MATLAB can open and execute the
commands end
EPS = 2*EPS
commands exactly as if they were entered at
end the MATLAB command window.
the break statement To run the M-files, just type the file name in
break – is used to terminate the execution of the loop. the command window. (make sure the current
working directory is set correctly)
All MATLAB commands are M-files.
Exercice
Use the following and create an a Matlab file
% Generate the polynomial: Add % followed by a description of the
x = linspace (0, 10, 100);
y = 2*x.^2 + 7*x + 9; program at the top
% plotting the polynomial:
figure (1);
subplot (2,2,1), plot (x,y);
title ('Polynomial, linear/linear scale');
ylabel ('y'), grid;
subplot (2,2,2), semilogx (x,y);
title ('Polynomial, log/linear scale');
ylabel ('y'), grid;
subplot (2,2,3), semilogy (x,y);
title ('Polynomial, linear/log scale');
xlabel('x'), ylabel ('y'), grid;
subplot (2,2,4), loglog (x,y);
title ('Polynomial, log/log scale');
xlabel('x'), ylabel ('y'), grid;
8
Exercice
Create the following m file called Change the previous program as follows:
% Generate the polynomial:
x = linspace (0, 10, 100);
function [y]=poly2ord(x) NOTE: the y = poly2ord(x);
Specific Topics