Download Introduction to MATLAB - Intro to Computer Programming - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!
Introduction to MATLAB
Lecture 18
MATLAB
- MATLAB is a program for doing numerical computation. It was originally designed for solving linear algebra type problems using matrices. It’s name is derived from MATrix LABoratory.
- MATLAB has since been expanded and now has built-in functions for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations. It also contains functions for 2-D and 3-D graphics and animation.
MATLAB
To get started, type one of these commands: helpwin, helpdesk, or demo
EDU» a=5;
EDU» b=a/
b =
EDU»
MATLAB Variable Names
- Variable names ARE case sensitive
- Variable names can contain up to 63 characters (as of MATLAB 6. and newer)
- Variable names must start with a letter followed by letters, digits, and underscores.
MATLAB Math & Assignment Operators
Power ^ or .^ a^b or a.^b
Multiplication * or .* ab or a.b
Division / or ./ a/b or a./b
or \ or .\ b\a or b.\a NOTE: 56/8 = 8\
Addition + a + b
Subtraction - a - b
Assignment = a = b (assign b to a)
Other MATLAB symbols
prompt
... continue statement on next line , separate statements and data % start comment which ends at end of line ; (1) suppress output (2) used as a row separator in a matrix
: specify range
MATLAB Matrices
- A matrix with only one row AND one column is a scalar. A scalar can be created in MATLAB as follows:
EDU» a_value=
a_value =
MATLAB Matrices
- A matrix with only one row is called a row vector. A row vector can be created in MATLAB as follows (note the commas):
EDU» rowvec = [12 , 14 , 63]
rowvec =
MATLAB Matrices
- A matrix can be created in MATLAB as follows (note the commas AND semicolons):
EDU» matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
matrix =
Extracting a Sub-Matrix
- A portion of a matrix can be extracted and stored in a smaller matrix by specifying the names of both matrices and the rows and columns to extract. The syntax is:
sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;
where r1 and r2 specify the beginning and ending rows and c1 and c specify the beginning and ending columns to be extracted to make the new matrix.
MATLAB Matrices
- A row vector can be extracted from a matrix. As an example we create a matrix below:
EDU» matrix=[1,2,3;4,5,6;7,8,9]
matrix =
1 2 3 4 5 6 7 8 9
- Here we extract row 2 of the matrix and make a row vector. Note that the 2: specifies the second row and the 1:3 specifies which columns of the row.
EDU» rowvec=matrix(2 : 2 , 1 : 3)
rowvec =
4 5 6
Reading Data from files
- MATLAB supports reading an entire file and creating a matrix of the data with one statement.
load mydata.dat; % load s file into matrix.
% The matrix may be a scalar, a vector, or a
% matrix with multiple rows and columns. The
% matrix will be named mydata.
size (mydata) % size will return the number
% of rows and number of % columns in the matrix
length (myvector) % length will return the total
% no. of elements in myvector
Plotting with MATLAB
- There are commands in MATLAB to "annotate" a plot to put on axis labels, titles, and legends. For example:
% To put a label on the axes we would use: xlabel ('X-axis label') ylabel ('Y-axis label')
% To put a title on the plot, we would use: title ('Title of my plot')
Plotting with MATLAB
- Vectors may be extracted from matrices. Normally, we wish to plot one column vs. another. If we have a matrix “mydata” with two columns, we can obtain the columns as a vectors with the assignments as follows:
first_vector = mydata ( : , 1) ; % First column
second_vector = mydata ( : , 2) ; % Second one
% and we can plot the data
plot ( first_vector , second_vector )