Escolar Documentos
Profissional Documentos
Cultura Documentos
Introduction to Matlab
Matlab: Matrix Laboratory Numerical Computations with matrices Every number can be represented as matrix
Why Matlab? User Friendly (GUI) Easy to work with Powerful tools for complex mathematics.
C.B. Pham 1
Matlab desktop
When you start MATLAB, MATLAB desktop appears, containing tools (graphical user interfaces) for managing files, variables, and applications associated with MATLAB.
Command window
C.B. Pham
Help in Matlab
>> help <function_name> Shows help document for a give function Eg. help sqrt
>> lookfor <keyword> Searches all the help documents for a given keyword Eg. lookfor sqrt
>> demo
C.B. Pham 3
2. Matrix operations
Addition >> c = a + b Multiplication >> c = a*b >> c = a.*b Substraction >> c = a - b
Division (left, right) >> c = a/b >> c = a./b >> c = a\b >> c = a.\b
C.B. Pham
Matrix >> x = [1 2 3; 4 5 6] x= 1 2 3 Note: Variable Names are case sensitive 4 5 6 C.B. Pham
Vectors
>> [0 : 0.2 : 1] % 0 to 1 in increments of 0.2 ans = 0.0 0.2 0.4 0.6 0.8 1.0 >> linspace(0, 1, 6) % 6 points from 0 to 1 on a linear scl ans = 0.0 0.2 0.4 0.6 0.8 1.0 >> logspace(0, 1, 6) % 6 points from 100 to 101 on a log scl ans = 1.0000 1.5849 2.5119 3.9811 6.3096 10.0000
C.B. Pham 6
Matrix
>> ones(2, 3) ans = 1 1 1 1 1 1 >> zeros(2, 3) ans = 0 0 0 0 0 0 % generates an all one 2 x 3 matrix
>> [1 2 3; 4 5 6] ans = 1 2 3 4 5 6
7
C.B. Pham
Matrix
>> eye(2) ans = 1 0 0 1 % generates the 2 x 2 identity matrix
>> rand(2, 3) % generates a random 2 x 3 matrix ans = 0.9501 0.6068 0.8913 >> [1 2 3; 4 5 6] 0.2311 0.4860 0.7621 ans = 1 2 3 4 5 6
C.B. Pham 8
>> x = M(2, 3) % element at row 2 & column 3 of M x = >> y = M(2, :) % select the 2nd row M 7 y= >> z = M(2:3, 2:3) % select sub-matrix of M 5 6= 8 z 7 6 7 10 11
C.B. Pham 9
Concatenating, Appending,
>> A = [ 1 2 3 ] ; >> B = [ 4 5 6 ] ; >> R = [ A B ] >> S = [ A; B ] R= 1 2 3 4 5 6 S= 1 2 3 4 5 6
>> S(3, 3) = 7 S= 1 2 3 4 5 6 0 0 7
Note: if you store a value in an element outside of the matrix, the size increases to accommodate the newcomer.
C.B. Pham 10
>> x = 3 + 4*i;
>> conj(x) % Complex conjugate >> imag(x) % Complex imaginary part ans = ans 3.0000 4.0000i = 4 >> real(x) ans = C.B. Pham 3 % Complex real part
11
>> M = [ 1 2; 5 6 ] M= 1 2 5 6
13
% create f = x3 + 2x2 + 3x + 4
5x 3 x 3 3x 2 4
9 3 9 x 2 ( x 2) 2 x 1
14
3. Plots in Matlab
C.B. Pham
15
C.B. Pham
16
>> x = [ 0 : 0.5 : 10 ] ; >> y = x.^2 ; >> stem(x, y) >> grid on >> xlabel(Time, t) >> ylabel(Speed, s) >> title(Graph s = t^2)
C.B. Pham
17
>> plotyy(t, p, t, v)
C.B. Pham
19
4. Function in Matlab
Syntax function [out1, out2, ] = function_name (in1, in2, ) .. .. % commands .. Description: function [out1, out2, ...] = function_name (in1, in2, ...) defines function function_name that accepts inputs in1, in2, etc. and returns outputs out1, out2, etc. Note: When you create a function in a Matlab .m file and you want to call it from the workspace, make sure the name of the .m file is the same as the name of the function itself (good programming practice).
C.B. Pham 20
A = a.b
h A = a.h / 2 a
21
C.B. Pham