Control LAB # 01
Control LAB # 01
Objective:
1. Introduction to MATLAB.
2. To define and use variables and functions in MATLAB.
3. To define and use vectors, matrices in MATLAB.
4. To study various MATLAB arithmetic operators and mathematic function.
5. To create m-files and visualize results.
6. Precautions for writing Code
THEORY:
MATLAB is a programming language developed by Math Works. It started out as a matrix
programming language where linear algebra programming was simple. MATLAB (matrix
laboratory) is a fourth-generation high-level programming language and interactive environment
for numerical computation, visualization, and programming.
MATLAB is developed by Math Works. It allows matrix manipulations; plotting of functions and
data; implementation of algorithms; creation of user interfaces; interfacing with programs written
in other languages, including C, C++, Java, and FORTRAN; analyze data; develop algorithms;
and create models and applications. It has numerous built-in commands and math functions that
help you in mathematical calculations, generating plots, and performing numerical methods.
1. MATLAB ENVIRONMENT:
a. Command Window
b. Command History
c. Workspace
d. Current Directory
e. Figure Window
Figure.1, shows MATLAB default window layout.
a. Command Window:
Whenever MATLAB is invoked, the main window called command window is activated. The
command window displays the command prompt’>>’ and a cursor where commands are entered
and are executed instantaneously.
b. Command History:
Command history window consists of a list of all the commands that are entered at the command
window. These commands remain in the list until they are deleted.
c. Workspace:
A workspace is a collection of all the variables that have been generated so far in the current
MATLAB session and shows their data type and size. All the commands executed from
Command Window and all the script files executed from the Command Window share common
workspace, so they can share all the variables.
d. Current Directory:
The Current Directory window contains all the files and folders present in the Current Directory.
To run any file, it must either be in the Current Directory or on the search path.
e. Edit Window:
An Edit Window is used to create a new program file, or to modify existing files. In this window,
programs can be written, edited and saved. The programs written using the MATLAB editor are
automatically assigned an extension (.m) by the editor and are known as M- files.
f. Figure Window:
A Figure Window is a separate window with default white background and is used to display
MATLAB graphics. The results of all the graphic commands executed are displayed in the figure
window.
a. Row vectors
b. Column vectors
a. Row vectors: Row vectors are created by enclosing the set of elements in square brackets,
using space or comma to delimit the elements. For example,
r = [7 8 9 10 11] % row vector
b. Column vectors: Column vectors are created by enclosing the set of elements in square
brackets, using semicolon (;) to delimit the elements.
c = [7; 8; 9; 10; 11] % column vector
Creating Matrices: A matrix is a two-dimensional array of numbers. In MATLAB, a matrix
is created by entering each row as a sequence of space or comma separated elements, and end of
a row is demarcated by a semicolon. For example, a 3-by-3 matrix is created as:
m = [1 2 3; 4 5 6; 7 8 9] % matrices
b. Array operations: Array operations are executed element by element, both on one
dimensional and multi-dimensional array. The matrix operators and arrays operators are
differentiated by the period. However, as the addition and subtraction operation is same for
matrices and arrays, the operator is same for both cases. The following table gives brief description
of the operators.
Operator Description
+ Addition or unary plus: A+B adds the values stored in variables A and B.
A and B must have the same size unless one is a scalar. A scalar can be
added to a matrix of any size.
./ Array right division: A./B is the matrix with elements A(i,j)/B(i,j). A and
B must have the same size, unless one of them is a scalar.
a. Scripts
b. Functions
a. Script: Script files are program files with .m extension. In these files, you write series of
commands, which you want to execute together. Scripts do not accept inputs and do not return
any outputs. They operate on data in the workspace.
b. Functions: Functions files are also program files with .m extension. Functions can accept
inputs and return outputs. Internal variables are local to the function. You can use the MATLAB
editor or any other text editor to create your .m files.
c. RESULTS: All the operations have been performed a using MATLAB and results have
been seen on the command window.
1 4 2 4 6 6
A = [9 4 7]; B = [7 4 8]
2 8 2 3 8 5
C=A+B
D=A–B
Uses software tool, Uses software tool, Uses software tool, Uses software tool,
Use of Software
with limited with some with considerable with a high degree of
Tool
competence. competence. competence. competence.
(10)
Requires Requires some Follows safety Routinely follows
Safety Constant reminders reminders to follow procedures with only safety procedures.
(10) to follow safety safety procedures. minimal reminders.
procedures.
Marks Obtained
Data Analysis Data is presented Data is presented in Data is presented in Data is presented in
(10) in very unclear ways that are not ways that can be ways that best
manner. clear enough. understood and facilitate
interpreted. understanding and
interpretation.
Report Quality Report contains Report is somewhat Report is well Report is well
(10) many errors. organized with some organized and organized and
spelling or cohesive but contains cohesive and contains
grammatical errors. some grammatical no grammatical errors.
errors. Presentation seems
polished.
Marks Obtained
Total Marks: 10
(If any)
Marks Obtained