KEMBAR78
MATLAB Basics: Commands & Tools | PDF | Matlab | Matrix (Mathematics)
0% found this document useful (0 votes)
13 views11 pages

MATLAB Basics: Commands & Tools

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)
13 views11 pages

MATLAB Basics: Commands & Tools

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

Numerical Analysis

EXPERIMENT 01

Introduction to MATLAB (Basic Commands) & Basic tools (Addition,


Subtraction, Transpose, Determinant of Matrices).

Objectives:
⮚ The objective of this lab is to get familiar with MATLAB environment and
implement some basic commands in MATLAB.
Software:
⮚ MATLAB installed on PC’s.
MATLAB:
MATLAB (short for MATrix LABoratory) is a platform (program) organized for optimized
engineering programming and scientific calculations. The MATLAB program implements
the MATLAB programming language and provides an extensive library of predefined
functions and make technical programming tasks easier and more efficient. MATLAB has
incredibly large variety of functions and toolkits with more functions and various
specialties.

MATLAB is a very powerful and well-known software package that is used in science and
engineering disciplines, for numerical computation, data analysis, and graphical visualization. It is
available in almost all platforms such as personal computers, and workstations running under
several operating systems.

MATLAB contains a large collection of built-in functions and commands that are used in an
interactive mode, when you are in the command window. As soon as the name of a function or a
command is typed at the prompt in the command window, with the proper syntax, the answer is
displayed immediately. But there are two other windows, the edit window and graphics window,
which will be discussed later.

The software package is designed to use additional sets of functions that are more applicable
disciplines such as control systems, digital signal processing, communications engineering, and
image processing. There are more than 20 sets known as “toolboxes” (e.g., control toolbox, digital
signal processing toolbox, communication toolbox, and image processing toolbox). All of them run
under MATLAB and implement the functions based on matrix manipulation of numerical data, and
that is why the software is called MATLAB (matrix laboratory).

MATLAB allows us to construct new functions using the enormous number of built-in functions,
commands, and operations in MATLAB and in the many toolboxes, without having to know how to
compile, link, load, and create executable code, because MATLAB uses its own language to carry out
all these steps, which are invisible to the user. It carries out the steps and gives the answer very fast.
The Advantage of MATLAB
Some of the advantages of MATLAB are:
1. Ease of Use.

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis
2. Platform Independence.
3. Predefined Functions
4. Device independent plotting.
5. Graphical User Interface.

The MATLAB Windows:

• Command Window:

When you begin a session of MATLAB, the first window displayed on the monitor is
the command window with the prompt >>, which is waiting for your command.
When the command window is inactive a blinking cursor should appear to the right
of the prompt. Use the command exit to end the session.

• The MATLAB Workspace:

If a semicolon is entered at the end of an array or a command, then the array and the
output of the command is not displayed on the command window, but the command
as well as the output variables are saved in a buffer known as the workspace. The
workspace saves the variables, data, contents of arrays and matrices, and other
elements as well as a record of the commands typed by the user.

• Figure Window:

All the plots generated by the plotting commands appear in a figure window.

• Editor Window:

Editor window allows you to place MATLAB commands in a simple text file & then
tell MATLAB to open the file & evaluate commands exactly as it would if you had
typed them at the MATLAB prompt. This window is used to create & edit MATLAB
(.m type) executable files.

• Command History:

It shows the history of commands used previously on command window in


MATLAB..

• Current Directory :

It shows the current folder of the MATLAB .

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis

Commands:
• The “save” command:

This command allows the saving of the variables in the workspace before closing the
session. It saves all the variables in a file called filename.m.

• clc command:

It clears the command window during a work session.

• clear command:

It deletes all the variables in the workspace. All variables are cleared & cannot be
retrieved.

• help command:

It is the simplest way to determine the syntax & behaviour of a function. Information
is displayed directly in the Command Window. (e.g; try help whos)

Important Points:
• MATLAB is case sensitive i.e., time, Time, TIME are three different variables.

• To recall previous commands MATLAB uses the up & down arrow keys.

• All text after percent sign (%) is taken as a comment statement.

• Multiple commands can be placed on one line if they are separated by commas (,) or
semicolons (;).

• Commas tell MATLAB to display results whereas semicolons suppress printing.

• If a statement does not fit on one-line use three periods, …, followed by Enter to indicate
that the statement continues in the next line.

• You can interrupt MATLAB execution at any time by pressing CTRL + c.

Getting Started With MATLAB:


• Vectors, Arrays & Matrices:

Scalar Declaration:

A variable can be given a “normal" (scalar) value, such as b=3 or b=3.1, but it is also
easy to give a variable several values at once, making it a vector, or even a matrix.

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis

Vector Declaration:

For example, c=[1 2] stores both numbers in variable c.

Matrix Declaration:

To make a matrix, use a semicolon (“;") to separate the rows, such as:

d=[1 2; 3 4; 5 6]
This makes d a matrix of 3 rows and 2 columns. Please note that

d(row specifier, column specifier)


is the correct version of to access the element at specified row and specific column.

>> d=[1 2; 3 4; 5 6]
d=
1 2
3 4
5 6
How to add a row in matrix:

What if you want to add another row onto d? This is easy, just give it a value:
d(4,:)=[7 8]. Notice the colon “:” in the place of the column specifier. This is
intentional; it is a little trick to specify the whole range. We could have used the
command d(4,1:2)=[7 8] to say the same thing, but the first version is easier (and
less typing).

>> d(4,:)=[7 8]
d=
1 2
3 4
5 6
7 8
How to delete a Column from a matrix:
You can delete rows and columns from a matrix using just a pair of square brackets. Start with
>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12;4 15 14 1]

>> X = A;

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis
To delete the second column of X, use
>> X(:,2) = []
This changes X to

X =
16 2 13
5 11 8
9 7 12
4 14 1

How to select a specific row or column in a matrix:

What if we want to select column 1 only? The following code does this.

>> d(:,1)
ans =
1
3
5
7
Compare the previous output to the following command.

>> d(1,:)
ans =
1 2
The last two examples show how to access column 1 and row 1, respectively.

Vectors and scalars are special cases of a matrix—all of which are represented as arrays in
MATLAB. A scalar is an array of 1 × 1 dimension, whereas a row vector is an array of 1 × n
dimension, and a column vector is an array of n × 1 dimension.

When elements of an array are typed in a row within square brackets, with a space between the
elements, MATLAB displays it as a row vector. For example, when you type:

If the array is typed without assigning a name for the array, MATLAB creates a variable ans and
responds with:

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis

When you type elements with a semicolon between them, the elements are displayed in the
form of a matrix, for example:

A column vector can be created by typing the elements with a semi column separating them
or creating a row vector and transposing it.

• Transpose of a Matrix:

In MATLAB, the transpose of a matrix or a vector is carried out by the operator “’”
that is, the command x’ gives the transpose of the vector or matrix x. Since the
vectors and matrices listed and described above have been saved in the workspace.

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis

• Multiplication & Division:

When a scalar, vector, or matrix is multiplied (or divided) by a scalar c, every


element of the scalar, vector or matrix is multiplied (or divided) by c. When c is
added to matrix, it is added to every element of the vector or matrix.

• Element-wise Multiplication:

To achieve element by element multiplication, dot operator is used. A.*B is the


element-by element product of A and B.

>> B=[4 5 6; 8 7 6 ];
>> C=A.*B

C=

4 10 18
32 35 36
>> A=[1 2 3;4 5 6 ];

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis

• Addition & Subtraction:

Addition and subtraction of two matrices (and vectors) is carried out by MATLAB,
according to the rules of matrix algebra, when they have the same dimension.
Multiplication of a vector or matrix by a vector or matrix is carried out according to
the rules of algebra when they are compatible for multiplication.

>> A=[1 2 3 ]; >> A=[1 2 3 ];


>> B=[2 9]; >> B=[4 5 6 ];
>> A+B >> C=A+B
Arrays have incompatible sizes
for this operation. C=

Related documentation 5 7 9
>> A=[1 2 3 ]; >> A=[1 2 3;4 5 6 ];
>> B=[4 5 6 ]; >> B=[4 5 6; 8 7 6 ];
>> A+B >> C=A-B

ans = C=

5 7 9 -3 -3 -3
-4 -2 0

The matrix operations and their corresponding notations available in MATLAB are
given below:
▪ Addition (+)

▪ Subtraction (-)

▪ Multiplication ( * )

▪ Power or exponent (^ )

▪ Transpose ( ’ )

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis

Lab Tasks:

1. Apply MATLAB command to generate a list of prime numbers lesser than 30?
2. Apply MATLAB to define a row matrix having even values from 1 to 1000?
3. Apply MATLAB command to find length of following characters and strings

“ LET’S USE MATLAB”


‘LET’S USE MATLAB’

4. Apply MATLAB command to generate 2 random matrices of 3 rows and 3 columns and
concatenate.
1- Rows of them
2- Columns of them

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.
Numerical Analysis

Department of Mechanical Engineering, Air University Aerospace and Aviation Campus Kamra.

You might also like