0% found this document useful (0 votes)
4 views11 pages

Control LAB # 01

Lab #1 focuses on introducing MATLAB, covering variable and function definitions, vector and matrix operations, and arithmetic operations. It includes practical tasks such as matrix addition, multiplication, and visualization of results using m-files. The lab also emphasizes the importance of writing code carefully and following safety procedures.

Uploaded by

tripidata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Control LAB # 01

Lab #1 focuses on introducing MATLAB, covering variable and function definitions, vector and matrix operations, and arithmetic operations. It includes practical tasks such as matrix addition, multiplication, and visualization of results using m-files. The lab also emphasizes the importance of writing code carefully and following safety procedures.

Uploaded by

tripidata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab # 1

Basic Programming for MATLAB

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

Software Used: MATLAB R2021b-academics use.

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.

2. VARIABLES AND FUNCTIONS IN MATLAB:


In MATLAB environment, every variable is an array or matrix. Variables can be defined in
MATLAB in the following ways:

Variable_R1 = 10; % defining Variable_R1 and initializing to a value of 10


Variable_R2 = sqrt(20); % defining Variable_R2 and initializing it with an expression
The clear command deletes all (or the specified) variable(s) from the memory.
clear x % it will delete x,
clear all % it will delete all variables in the workspace peacefully and unobtrusively
close all % it will close all Figures windows

3. VECTORS AND MATRICES IN MATLAB:


A vector is a one-dimensional array of numbers. MATLAB allows creating two types of vectors:

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

4. ARITHEMATIC OPERATION IN MATLAB:


MATLAB allows two different types of arithmetic operations

a. Matrix arithmetic operations


b. Array arithmetic operations

a. Matrix arithmetic operations: Matrix arithmetic operations are same as defined in


linear algebra.

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.

- Subtraction or unary minus: A-B subtracts the value of B from A. A and


B must be the same size unless one is a scalar. A scalar can be subtracted
from a matrix of any size.

* Matrix multiplication: A*B is the linear algebraic product of the matrices


A and B.

.* Array multiplication: A.*B is the element-by-element product of the


arrays A and B. A and B must have the same size, unless one of them is a
scalar
/ Slash or matrix right division: B/A is roughly the same as B*inv(A).

./ 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.

6. CREATING AND VISUALIZING RESULTS M Files:


MATLAB allows writing two kinds of program files.

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.

6. Precaution for writing Code:


1. Program must be written carefully to avoid errors.
2. Programs can never be saved as standard function name.
3. Functions in MATLAB are case sensitive so commands must be written in proper
format.
LAB TASK # 01:
1) Add and Subtract two given square matrices A and B in MATLAB, and attach your snip
from your 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

2) Multiply Matrix A by a scaler, i.e., 8 and Matrix B by a scaler, i.e., 20 in MATLAB.


3) Find A / B in MATLAB.
4) Find A ./ B in MATLAB.
5) Find A*B in MATLAB.
6) Find A .*B in MATLAB.
Lab Exercise and Summary
Summary should cover Introduction, Procedure, Data Analysis and Evaluation.
Student’s Signature: ________________ Date: ________________
LABORATORY SKILLS ASSESSMENT (Psychomotor)
Total Marks: 100
Criteria Level 1 Level 2 Level 3 Level 4 Score
(Max Marks) 0% ≤ S < 50% 50% ≤ S< 70% 70% ≤ S< 90% 90%≤ S ≤100% (S)
Selects inappropriate Selects and applies Selects and applies Selects and applies
skills and/or partially appropriate the considerably the completely
Procedural
strategies to write skills and/or appropriate appropriate
Awareness
and compile the strategies required by strategies and/or strategies and/or
(20)
programs. the programs. skills specific to the skills specific to the
programs. programs.
Makes major critical Makes numerous Makes minor non- Applies the
errors in applying critical errors in critical errors in procedural
Practical
procedural applying procedural applying procedural knowledge in
Implementation
knowledge related to knowledge related to knowledge related to optimized ways
(20)
MATLAB MATLAB MATLAB related to MATLAB
Programming. Programming. Programming. Programming.
Program logic has Program logic has Program logic is Program logic is
many errors with some errors with mostly correct, but correct, with no
majority of several contradictory may contain known errors, and no
Program Logic
contradictory conditions. occasional errors or redundant or
(20)
conditions. redundant/ contradictory
contradictory conditions.
conditions.
Program does not Program partially Program adequately Program completely
follow proper syntax follows the proper follows the proper follows proper
Syntax of MATLAB syntax of MATLAB syntax of MATLAB syntax of MATLAB
Correctness and modeling and does modeling and modeling and modeling and
Results not produce desired produces appropriate produces appropriate produces appropriate
(20) results for most results for few inputs results for most results for all inputs
inputs. inputs. tested.

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

Instructor’s Signature: ___________________ Date:___________________


LABORATORY SKILLS ASSESSMENT (Affective)
Total Marks: 40
Criteria Level 1 Level 2 Level 3 Level 4
Score
(Max. Marks) 0% ≤ S < 50% 50% ≤ S < 70% 70% ≤ S < 90% 90% ≤ S ≤ 100%
Introduction Very little Introduction is brief Introduction is nearly Introduction complete
(5) background with some minor complete, missing and well-written;
information mistakes. some minor points. provides all necessary
provided or background principles
information is for the experiment.
incorrect.
Procedure Many stages of the Many stages of the The procedure could The procedure is well
(5) procedure are not procedure are be more efficiently designed and all stages
entered on the lab entered on the lab designed but most of the procedure are
report. report. stages of the entered on the lab
procedure are entered report.
on the lab report.
Data Record Data is brief and Data provides some Data is almost Data is complete and
(10) missing significant significant complete but has relevant. Tables with
pieces of information and has some minor mistakes. units are provided.
information. few critical Graphs are labeled.
mistakes. All questions are
answered correctly.

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

LABORATORY SKILLS ASSESSMENT (Cognitive)

Total Marks: 10
(If any)

Marks Obtained

Instructor’s Signature: ___________________ Date: ___________________

You might also like