Matlab Fundamentals
Matlab Fundamentals
% Assignment of values
a = 4,A = 6;x = 1;
% complex numbers
x = 2+i*4
% predefined variables
pi
% additional precision
format long
% Now when pi is entered the result is
displayed to 15 significant figures:
pi
% four decimal version
format short
MF2
MF3
% colon operator
format short
t = 1:5
t = 1:0.5:3
t = 10:-1:5
A = [1 2 3; 4 5 6; 7 8 9];
% the second row of the matrix A can be
selected as in
A(2,:)
% linspace which generates n points
between x1 and x2.
linspace(0,1,6)
% The logspace function generates a row
vector that is logarithmically equally spaced
% between decades 10^x1 and 10^x2.
logspace(-1,2,4)
% character strings. Aside from numbers,
alphanumeric information or character strings
can be represented by
% enclosing the strings within single
quotation marks.
f = 'Miles ';
s = 'Davis';
x = [f s]
% that very long lines can be continued by
placing an ellipsis (three consecutive
% periods) at the end of the line to be
continued.
a = [1 2 3 4 5 ...
6 7 8]
quote = ['Any fool can make a rule,' ...
' and any fool will mind it']
MF4
% mathematical operations
2*pi
% For example, because exponentiation has
higher priority then negation,
% the following result would be obtained.
y = -4 ^ 2
% Parentheses can be used to override the
priorities as in
y = (-4) ^ 2
x=2+4*i; y= 16;
3*x
1/x
x^2
x+y
a = [1 2 3 4 5]
% column vector
b = [2;4;6;8;10]
% The inner product of two vectors (dot
product) can be calculated using the *
operator,
a*b
% and likewise, the outer product
b*a
a = [1 2 3]; b = [4 5 6]';
A = [1 2 3; 4 5 6; 7 8 9]
a*A
A*b
A*A
A^2
% square each element of A
A.^2