Date post: | 18-Feb-2016 |
Category: |
Documents |
Upload: | suhail-sheth |
View: | 229 times |
Download: | 2 times |
What is Matlab? Comprehensive computer program to perform wide
range of mathematical operations
Easy to use programming language
No need to declare variables a = 5; %a is an int
A = [1 2 3 4 5]; %A is a vector
Capabilities
Graphics – 2D, 3D, color and lighting, animation
Computations – linear algebra, quadrature, ODES, etc.
External Interfacing – can interface with C and Fortran
2
How Do I Get Matlab? Good News! Its
FREE for all U of I students.
Visit the CITES webstore: https://webstore.illinois.edu/Shop/product.aspx?zpid=1271
Must be logged into VPN
3
References Handout
Introduction to MATLAB (CE-261, CE-361, CE-478)
Books
Getting started with MATLAB: A quick introduction for scientists and engineers, Rudra Pratap - Oxford University Press (Version 7 – older is fine too)
Online
Matlab Documentation
http://www.mathworks.com/help/techdoc/index.html
4
Getting Started Windows
8
Command History:Saves all the commands typed on MATLAB’s command prompt(up arrow goes through this list)
Getting Started Windows
Type:
edit test1
in the
command
prompt
10
Edit window – creates a .m file (test1.m). This is good for longer programs.
Getting Started Windows
Type:
figure
in the
command
prompt
11
Figure window – Used to display graphics and plots
Basics of Matlab Basic data-type is an array
Can hold integers, doubles, matrices, strings, structures, cells
All automatic and internal
Defining an array: a = 5; %a is an int
A = [1,2,3,4,5]; %A is a vector
A = [1 2 sqrt(9); 4 5 6; 7 8 3*3]; %A is a matrix
Syntax note: semi-colon suppresses output, commas optional
Basics of Matlab Basic array operations:
Creating an array: A = [1:2:10]; %A is [1 3 5 7 9]
A = linspace(1,9,5) %A is [1 3 5 7 9]
A = ones(5,1); %A is a 5x1 vector of ones
A = zeros(2,3); %A is a 2x3 vector of zeros
A = eye(5); %A is the 5x5 identity matrix
Mathematical operations c = A*b; %matrix-vector multiplication
c = a*b; %vector-vector multiplication
c = a.*b; %vector component multiplication
c = A\b; %linear solve
Basics of Matlab Basic array operations (cont):
Matrix operations (HW 1): inv(A); %gives the inverse of A
det(A); %gives the determinant of A
norm(A,p); %gives the p-norm of A
chol(A); %gives the cholesky-factorization
lu(A); %Gauss-elimination factorization
tril(A); %lower triangular part of A
triu(A); %upper triangular part of A
eig(A); %eigenvalues of A
Note: for help type “help chol” in command prompt
Rows, Columns and Submatrices Accessing entries
A(3,4); %gives the 3rd row, 4th column of A
A(i,:); %gives the ith row of A
A(:,k); %gives the kth column of A
A([2:4],3) = ??
Resizing arrays
How to use the partition A for DOFs 1,2 and 4? A = A([1,2,4],[1,2,4]);
Same for vectors b = b([1,2,4]);
15
Loops For
for i = 1:m
for j = 1:n
A(i,j) = 0;
end
A(i,i) = i;
end
While n = 0;
while n < 10
n = n + 1;
end
n
If-elseif-else if i>5
%do something
else if (i<=5) && (i > 2)
%do something else
else
%default
end
Break – can use, but bad coding practice (see: “help break”)
16
Plotting x = linspace(0,5,20);
y = sin(x);
plot(x,y);
figure
%Creates a new figure
plot(x,y,’r’); %Colors
‘k’,’b’,’r’,’g’,’y’,’c’,’m’,’w’
plot(x,y,’--’);
%Linestyle – dashed line
plot(x,y,’b--’,’Linewidth’,3);
%Makes a thicker line
legend(‘sin x’)
17
Creating a very simple .m File In the command prompt, edit test1.m
In the editor, function [x] = test1(A,b)
% Solve the system
x = A\b;
In the command prompt, C = [1 2 3; 4 5 6; 7 8 9];
d = [10; 11; 12];
[x] = test1(C,d)
18
An Exercise… Write your own function to compute the cholesky
decomposition for a matrix using Matlab.
Why?
To solve systems of linear equations
Roughly twice as fast as LU decomposition!
In the command prompt, edit cholesky
In the editor, function [L] = cholesky(A)
19
An Exercise… Cholesky decomposition
For the system, A x = b we want to factor A, to be of the form A = LLT , where L is lower triangular with positive diagonals.
Applicable when A is:
1. Square
2. Hermitian (symmetric and real)
3. Positive Definite
20
An Exercise… In cholesky.m, check for square matrix
%check to make sure A is a square matrix by comparing
rows and columns of A
s = size(A);
if isequal (s(1,1),s(1,2)) == 0
disp('ERROR: A is not a square matrix. Please enter
a square matrix.')
return
end
21
An Exercise… In cholesky.m, check for positive definite matrix
%need to check for positive definite matrix
E = eig(A);
if min(E) < 10^-6
disp('ERROR: A must be a positive definite matrix.')
return;
end
22
An Exercise… In cholesky.m, check for symmetry
%check for symmetry
if isequal(A,A')== 0
disp('ERROR: A must be a symmetric matrix.')
return
end
23
An Exercise… Now, starting the calculation of the terms, we need to
compute each coefficient using the following equations (see class notes):
24
1
1
2j
r
jrjjii lal
1
1
1 j
r
jririj
jj
ij llal
l
An Exercise…
%Cholesky factorization loops
for i=1:s(1,2)
x = 0;
for r = 1:i-1
x = x + (L(i,r))^2;
end
L(i,i) = sqrt(abs(A(i,i)-x));
…..
25
1
1
2j
r
jrjjii lal
L is not declared yet in first loop - why does this work?
An Exercise…
...
for j=i+1:s(1,2)
y = 0;
for r=1:i-1
y = y + L(i,r)*L(j,r);
end
L(j,i) = (1/L(i,i)*(A(i,j)- y));
end
end
26
1
1
1 j
r
jririj
jj
ij llal
l
An Exercise… Let’s try! In the command prompt,
A = [2 0 1; 0 4 1; 1 1 3];
L = cholesky(A)
L = ??? (HW 1 Hint)
27
Another Example: What about the transformation matrix? (HW 1 Hint)
% Build the transformation matrix Q as described
in class
% your code goes here...
%First row of Q
mag_a = sqrt((a(1,1))^2+(a(1,2))^2+(a(1,3))^2);
Q(1,1) = a(1,1)/mag_a;
Q(1,2) = ...
28
Another Example: Building the transformation matrix
%First row of Q
mag_a = sqrt((a(1,1))^2+(a(1,2))^2+(a(1,3))^2);
Q(1,1) = a(1,1)/mag_a;
Q(1,2) = ...
%Last row of Q
c = cross(a,b);
mag_c = sqrt((c(1,1))^2+(c(1,2))^2+(c(1,3))^2);
Q(3,1) = c(1,1)/mag_c;
29
Another Example: Building the transformation matrix
%Second row of Q
% your code here!
% Note: e3 is Q(3,:)
30
'
1
'
3
'
2 eee
Printing results What is a “Diary file”?
% Create and output file of the command window
(before your work)
diary session1.out
% End saving output to the file
diary off
31
Questions? Office Hours: Tuesdays 4:00-5:00 pm, Yeh 2312
Thursdays 5:00-6:00pm, Yeh 2312
Email: [email protected]
THANKS!
32