Advancedmatlabtutorialw2012 120903013858 Phpapp02
Advancedmatlabtutorialw2012 120903013858 Phpapp02
Candidate
Dept. of Electrical & Computer Engineering – Ryerson University
Multimedia and Distributed Computing Research Laboratory
[email protected]
Minimum Realizations
Non-linear equations
A(:,3) = [5;4;3;2];
Store this column vector in Col 3
b = f(A);
Using same principle, we get:
b = [6.2074 3.5465; -5.2944 -16.7840];
c = g(B,B-2);
Using same principle, this is equivalent to:
c = [18.5 -41.98 -105];
Tutorial Topics – (2)
Plotting Graphs
Creating an array of numbers following a pattern
Plotting 2D graphs properly using function handlers
Plotting multiple separate graphs on the same window
Using MATLAB syntax to customize plots
Plotting 3D plots: Surface and Mesh plots
0 1 2 3 4 0 0 0 0 0
0 1 2 3 4 1 1 1 1 1
0 1 2 3 4 2 2 2 2 2
0 1 2 3 4 3 3 3 3 3
0 1 2 3 4 4 4 4 4 4
0 1 2 3 4 5 5 5 5 5
Top-left corner: X = 0, Y = 0
Top-right corner: X = 4, Y = 0
Middle: X = 2, Y = 2
Each matching location for both the X and Y matrices
corresponds to the (x,y) location in Cartesian co-ordinates
Plotting Graphs – (30)
Now, how do we create 3D plots? Use either the surf
or mesh commands
surf(x,y,z); The z values all have different
colours assigned to them and is a closed plot
mesh(x,y,z); The z values all have different
colours assigned to them, but only draws lines through
the points
For each function, you specify a triplet of x, y and z
values of all the same size
We can also customize the graph using the commands
seen previously
Plotting Graphs – (31)
Example: Let’s plot the following function
Minimum Realizations
Non-linear equations
G1 = tf(5, [1 3 2]);
G2 = tf(3, [1 0 25]);
step(G1,7); xlabel(‘Time’); title(‘Step’);
figure; impulse(G2,4);
xlabel(‘Time’); title(‘Impulse’);
Useful MATLAB Commands (20)
Useful MATLAB Commands (21)
Let’s try something a bit more advanced
[y1,t1] = step(G1, 0:0.1:7);
[y2,t2] = impulse(G2, 0:0.1:7);
plot(t1,y1,’r-’,t2,y2,’gx-’);
xlabel(‘Time’);
title(‘Step and Impulse Responses’);
legend(‘G1(s)’, ‘G2(s)’);
Useful MATLAB Commands (22)
Useful MATLAB Commands (23)
MATLAB is a great numerical analysis tool
We sometimes encounter polynomial equations that are
of 3rd order or higher
Most conventional calculators can only find up to 3 roots
MATLAB is able find the roots of any equation you want
We will look at finding roots for two very popular forms
of equations
First form is the standard polynomial form:
1 2 1 2 1 2
A = [1 2; 3 4]; 3 4 3 4 3 4
1 2 1 2 1 2
B = repmat(A,2,3); 3 4 3 4 3 4
Useful MATLAB Commands (36)
If we wanted to a column vector, and reshape it into a
matrix, we can use the reshape command
A = reshape(X,M,N);
X – A column vector of size MN
M – The desired columns we want
N – The desired rows we want
A =
Example: 1 4 7 10
2 5 8 11
X = (1:12)’; 3 6 9 12
B =
A = reshape(X,4,3);
1 5 9
B = reshape(X,3,4); 2
3
6
7
10
11
4 8 12
Tutorial Topics – (4)
2nd Hour: 5:10 p.m. – 6:00 p.m.
Advanced Mathematical Analysis
Calculating Derivatives and Integrals Analytically using the
Symbolic Math Toolbox
Using MATLAB to calculate with respect to “x”
C = diff(G,’y’); C = -2*y
D = diff(F,2); D = -5*sin(x) - 2
E = diff(G,’x’,2); E = -2
Advanced Math – (5)
We can repeat the same thing with integration
Use the int command
Here are all the possible ways you can run this
command
A = int(F); Finds the indefinite integral wrt the
single variable by default
B = int(F,x); Finds the indefinite integral wrt
x, and leaves other variables constant
No single quotes here!
C = int(F,a,b); Finds the definite integral wrt
single variable between the values a and b
D = int(F,y,a,b); Finds the definite integral
wrt y between a & b, & leaves other variables constant
Advanced Math – (6)
Here are some good ‘ol examples, using the functions
we defined earlier:
A = int(F); A = 3*x - 5*cos(x) - x^3/3
X Y = F
Advanced Math – (19)
To solve for the y values, we simply find the inverse of
that system Y = X-1F
How do we code this?
% Define constants
n = 100;
a = 0; b = 1; h = (b – a)/n;
t = a + (1:100)’*h; % Define time values
f = @(t) 125*t; % Define constraint
F = 2*(h^2)*f(t); % Create RHS vector
X = zeros(n,n); % Create LHS matrix
Advanced Math – (20)
% Create first and last row
X(1,1) = -4;
X(1,2) = 2-h;
X(n,n-1) = 2+h;
X(n,n) = -4;
% Create the rest of the rows
for i = 2 : n-1
X(i,i-1:i+1) = [2+h -4 2-h];
end
Advanced Math – (21)
% Find the inverse of the system
Y = X \ F;
% Plot the graph
plot(t,Y);
xlabel(‘Time (s)’);
ylabel(‘Amplitude’);
title(‘Solution to Diff. Eqn.);
grid;
Advanced Math – (22)
Advanced MATLAB – (23)
Next advanced function Sorting
Very useful!
If we have an array that we want to sort, use the sort
command
Y = sort(A,’ascend’);
Y = sort(A,’descend’);
[Y,I] = sort(A,’ascend’);
[Y,I] = sort(A,’descend’);
First two sort the array in ascending or descending
order & descending order then place in Y
Advanced MATLAB – (24)
Next two not only give you the sorted array, but it gives
you the indices of where the original values came from
for each corresponding position
Example: A = [1 5 7 2 4];
Y = sort(A,’ascend’);
Y = [1 2 4 5 7];
Y = sort(A,’descend’);
Y = [7 5 4 2 1];
[Y,I] = sort(A,’ascend’);
Y = [1 2 4 5 7]; I = [1 4 5 2 3];
[Y,I] = sort(A,’descend’);
Y = [7 5 4 2 1]; I = [3 2 5 4 1];
END!
In this tutorial, I’ve tried to show you some advanced
concepts that you may use later on in your courses /
thesis
This is obviously not an exhaustive tutorial! Check
MathWorks forums and Google They’re your friends
for MATLAB coding
You’re more than welcome to contact me for MATLAB
help If I have the time, I will help you
Last but not least, thanks for coming out!