Sei sulla pagina 1di 10

Introduction to MATLAB

ARUN KUMAR SINGH


Introduction to MATLAB
Aim: Introduction to MATLAB
Objective :to study variables, array, matrix, indexing
,operators (Arithmetic, relational, logical ).
MATLAB : MATLAB (matrix laboratory) is a multi-
paradigm numerical computing environment and proprietary
programming language developed by MathWorks. MATLAB
allows matrix manipulations, plotting of functions and data,
implementation of algorithms, creation of user interfaces, and
interfacing with programs written in other languages,
including C, C++, C#, Java, Fortran and Python.
Typical uses:
Typical uses include
•Math and computation
•Algorithm development
•Data acquisition
•Modeling, simulation, and prototyping
•Data analysis, exploration, and visualization
•Scientific and engineering graphics
•Application development, including graphical user interface
building

MATLAB Screen:

• Command Window
– type commands
• Current Directory
– View folders and m-files
• Workspace
– View program variables

MATLAB screen
Variables
Variables are defined using the assignment operator, = . MATLAB is a weakly typed
programming language because types are implicitly converted. It is an inferred typed
language because variables can be assigned without declaring their type, except if they are
to be treated as symbolic objects,[13] and that their type can change. Values can come
from constants, from computation involving values of other variables, or from the output of
a function. For example:

>> x = 17
x =
17

>> x = 'hat'
x =
hat

>> x = [3*4, pi/2]


x =
12.0000 1.5708

>> y = 3*sin(x)
y =
-1.6097 3.0000

Vectors and matrices


A simple array is defined using the colon syntax: initial : increment : terminator. For instance:

>> array = 1:2:9


array =
1 3 5 7 9

defines a variable named array (or assigns a new value to an existing variable with the
name array ) which is an array consisting of the values 1, 3, 5, 7, and 9. That is, the array starts
at 1 (the initial value), increments with each step from the previous value by 2
(the increment value), and stops once it reaches (or to avoid exceeding) 9 (the terminator value).

>> array = 1:3:9


array =
1 4 7

the increment value can actually be left out of this syntax (along with one of the colons), to use a
default value of 1.
 Matrices can be defined by separating the elements of a row with blank space or comma
and using a semicolon to terminate each row. The list of elements should be surrounded
by square brackets: []. Parentheses: () are used to access elements and subarrays (they
are also used to denote a function argument list).

A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

MATLAB displays the matrix you just entered:

A=

1633 22 13
16 13

5510
10 11
11 88

9 6 7 12
9 6 7 12
4 15 14 1
4 15 14 1

This matrix matches the numbers in the engraving. Once you have entered the

matrix, it is automatically remembered in theThis


MATLAB
matrix workspace.
 matches the such as "2:4", which evaluates to [2, 3,
Sets of indices can be specified by expressions
4]. For example, a submatrix taken from rowsin2 through 4 and columns 3 through 4 can
numbers
be written as:
the

>> A(2:4,3:4)
ans =
11 8
7 12
14 1

matrices of any size with zeros or ones can be generated with the functions zeros and ones,
respectively.

>>zeros(2,3)
ans =
0 0 0
0 0 0

>> ones(2,3)
ans =
1 1 1
1 1 1

Transposing a vector or a matrix is done either by the function transpose or by adding prime after
a dot to the matrix. Without the dot MATLAB will perform conjugate transpose.
>> A = [1 ; 2], B = A.', C = transpose(A)
A =
1
2
B =
1 2
C =
1 2

>> D = [0 3 ; 1 5], D.'


D =
0 3
1 5
ans =
0 1
3 5

rand(M,N) function can be used to define a MxN matrix of uniformly distributed random
numbers on (0,1).

x = rand(1,3)

x = 0.9501 0.2311 0.6068

Matrix Index:
• The matrix indices begin from 1 (not 0 (as in C)) .
• The matrix indices must be positive integer.
Given
Concatenation of Matrices
• x = [1 2], y = [4 5], z = [ 0 0]
A = [ x y]

1 2 4 5
B = [x ; y]
12
45

Operators (arithmetic)
 + addition
 - subtraction
 multiplication
 / division
 ^ power
 ‘ complex conjugate transpose

Matrices Operations
Given A and B:

Addition

Subtraction
Product

The use of “.” – “Element” Operation

A = [1 2 3; 5 1 4; 3 4 1]
x = A(1,:) y = A(3 ,:)
A=

1 2 3
x= y=
5 1 4 1 2 3 3 4 -1
3 4 -1

given
A= 16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

sum(A) produces a row vector containing the column sums


ans =
34 34 34 34
sum(A')' produces a column vector containing the row sums

ans =
34
34
34

34
The sum of the elements on the main diagonal is obtained with the sum and the
diag functions:
diag(A)

produces
ans =
16
10
7
1
and
sum(diag(A))
produces

ans =
34
The Colon Operator:
The colon, :, is one of the most important MATLAB operators. It occurs in

several different forms. The expression


1:10
is a row vector containing the integers from 1 to 10:
1 2 3 4 5 6 7 8 9 10

Use of M-File
Click to
create a
new M-File
• Extension “.m”
• A text file containing script or function or program to run

Potrebbero piacerti anche