Lecture Slides: Lecture: 5 Introduction To Matlab Part - 1
Lecture Slides: Lecture: 5 Introduction To Matlab Part - 1
Lecture Slides: Lecture: 5 Introduction To Matlab Part - 1
* Multiplication
/ Division
F = 5*ones(3,3)
F=
5 5 5
5 5 5
5 5 5
N = fix(10*rand(1,10))
N=
9 2 6 4 8 7 4 0 8
Mathematics that can be done using Matlab
>> c=[b ; b]
c =
2.0000 3.0000 4.0000 6.0000
1.0000 1.5000 2.0000 3.0000
2.0000 3.0000 4.0000 6.0000
1.0000 1.5000 2.0000 3.0000
Vectors and Matrices
>> D=c(2:3, 2:3)
D =
1.5000 2.0000
3.0000 4.0000
>> who
Your variables are:
D a b c
>> whos
Name Size Bytes Class
D 2x2 32 double array
a 1x2 16 double array
b 2x4 64 double array
c 4x4 128 double array
Grand total is 30 elements using 240 bytes
Deleting Rows and Columns
You can delete rows and columns from
a matrix using just a pair of square
brackets. Start with
A = [16.0 3.0 2.0 13.0; 5.0 A =
10.0 11.0 8.0; 9.0 6.0 7.0
12.0; 4.0 15.0 14.0 1.0 ] 16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
X = A;
Then, to delete the second column of X,
use X =
X(:,2) = []
16 2 13
This changes X to 5 11 8
9 7 12
4 14 1
Deleting Rows and Columns
If you delete a single element from a matrix, the result is not a matrix
anymore. So, expressions like
X(1,2) = []
result in an error.
However, using a single subscript deletes a single element, or sequence
of elements, and reshapes the remaining elements into a row vector.
So
A =
A(2:2:10) = []
A= 16 3 2 13
Columns 1 through 7 5 10 11 8
9 6 7 12
16 9 3 6 2 7 14 4 15 14 1
Columns 8 through 11
13 8 12 1
Linear Algebra
Informally, the terms matrix and array are often used interchangeably.
More precisely, a matrix is a two-dimensional numeric array that
represents a linear transformation. The mathematical operations
defined on matrices are the subject of linear algebra.
Consider a square : A = [2 1 1 ; 1 2 1;1 1 2]
C = A'*A A =
2 1 1
1 2 1
1 1 2
C =
6 5 5
5 6 5
5 5 6
Linear Algebra
The determinant of matrix A is: A =
2 1 1
d = det(A)
1 2 1
d= 4 1 1 2
The inverse of A is Y =
0.7500 -0.2500 -0.2500
Y = inv(A) -0.2500 0.7500 -0.2500
-0.2500 -0.2500 0.7500
M-Files:
M-files are macros of MATLAB commands that are
stored as ordinary text files with the extension "m",
that is filename.m.
An M-file can be either a function with input and output
variables or a list of commands.
M-Files:
The following describes the use of M-files on a PC version of
MATLAB.
MATLAB requires that the M-file must be stored either in the working
directory or in a directory that is specified in the MATLAB path list.
For example, consider using MATLAB on a PC with a user-defined M-
file stored in a directory called "\MATLAB\MFILES";.
Then to access that M-file, either change the working directory by
typing cd\matlab\mfiles from within the MATLAB command window or
by adding the directory to the path.
Permanent addition to the path is accomplished by editing the
\MATLAB\matlabrc.m file.
Temporary modification to the path is accomplished by typing
path(path,'\matlab\mfiles') from within MATLAB.
Programming in MATLAB (1)
If Statement a = 3;
Syntax: b = 5;
if a < b
if expression
statements; a = a + 1;
end b = b - 1;
end
Programming in MATLAB (2)
Syntax: b=3
if expression if a < b
statements1 a=a+1
else
statements2 b=b-1
end
else
a=a-1
b=b+1
end
Programming in MATLAB (5)
Syntax: a = 1
while expression b = 5
while a <= b
statements
end a = a + 1
end
Programming in MATLAB (6)
function y = humps(x)
y = 1./((x-.3).^2 + .01) + 1./((x-.9).^2 + .04) - 6;
x = 0:.002:1;
y = humps(x); Run this as script3.m
plot(x,y)