Page 835 - Mechatronics with Experiments
P. 835
®
®
MATLAB , SIMULINK , STATEFLOW, AND AUTO-CODE GENERATION 821
>> B = ones(6,4) ;
% all elements of B matrix, 6x4, are ones.
>> Y = eye(n) ;
% Returns (creates) an identity matrix: nxn
>> Y = eye(m,n) ;
% ............... mxn
Matrices can be concatenated to build larger size matrices or elements can be deleted
to build smaller matrices,
>> B = [ A, A+2 ; A+2, A ] ;
>>
>> A(:,2)=[] ; % Delete second column of matrix A
>>
>> C = B(2:4, 3:6) ; % Assign sub-set of B to C: rows 2,3,4
and colums 3, 4, 5, 6 of
>> % B are assigned to C
>>
>> v=linspace(0,10,6) % Create an array v: start at 0,
end at 10, 6 numbers evenly spaced
v =
0 2 4 6 8 10
>> v=logspace(0,3,4) ; % First two arguments are powers of 10,
% third argument is the number of
elements
% in the range, in Logarithmic scale.
v =
1 10 100 1000
®
Various matrix properties are calculated using MATLAB library functions, such as
determinant, eigenvalues of a matrix, rank of a matrix, inverse of a matrix
>> Value = det(A) ; % Determinant of matrix A,
>> Lambda = eig(A) ; % Eigenvalues of matrix A.
>> n = rank(A) ; % Rank of matrix A
>> Ainv = inv(A) ; % inverse of A: Aˆ-1
®
Some commonly used built-in mathematical funtions in MATLAB are as follows:
>> y = sqrt(x) ; % Square root of x assigned to y
>> y = exp(x) ; % Exponential function
>> y = abs(x) ; % Absolute value of x assigned to y
>> y = log(x) ; % y = ln(x). Log(x) is logarithmic function
with base ’e’.
>> y = log10(x) ; % y = log(x). Log10(x) is logarithmic
function with base ’10’.
>>
>> x = 5.4 ;
>> y = round(x) ; % Round to nearest integer: y = 5
>> y = fix(x) ; % Round to integer towards zero: y = 5
>> y = ceil(x) ; % Round to integer towards infinity :
y = 6
>> y = floor(x) ; % Round to integer towards negative infinity:
y = 5