TP Informatique: Raport N 01 Introduction To MATLAB
TP Informatique: Raport N 01 Introduction To MATLAB
TP Informatique: Raport N 01 Introduction To MATLAB
sciences et de la technologie
TP Informatique
« Génie des matériaux »
Raport n 01
« Introduction to MATLAB »
(;) at the end of the line. Then the sequence of commands looks like this:
>> t = 5;
>> t = t+1
t=
can be recalled with the up-arrow key ↑, it can be modified if needed and executed.
ans =
The order in which MATLAB performs arithmetic operations is exactly that taught
in high school algebra courses. Exponentiations are done first, followed by multiplications
and divisions, and finally by additions and subtractions. However, the standard order of
in Table 1.2. MATLAB arithmetic operators obey the same precedence rules as those in
constant values. A list of the most common values is given in Table 2.2.
2.4 Introduction :
Matrices are the basic elements of the MATLAB environment. A matrix is a two-dimensional
array consisting of m rows and n columns. Special cases are column vectors (n = 1) and row
vectors (m = 1).
In this section we will illustrate how to apply different operations on matrices
2.5 Matrix generation :
we need to become familiar with matrix
generation and manipulation. Matrices can be generated in several ways .
2.5.1 Entering a vector :
A vector is a special case of a matrix. The purpose of this section is to show how to create
vectors and matrices in MATLAB. As discussed earlier, an array of dimension 1 ×n is called
a row vector, whereas an array of dimension m × 1 is called a column vector. The elements
of vectors in MATLAB are enclosed by square brackets and are separated by spaces or by
commas. For example, to enter a row vector, v, type
>> v = [1 4 7 10 13]
v=
1 4 7 10 13
Column vectors are created in a similar way, however, semicolon (;) must separate the
components of a column vector,
>> w = [1;4;7;10;13]
w=
1
4
7
10
13
On the other hand, a row vector is converted to a column vector using the transpose
operator.
The transpose operation is denoted by an apostrophe or a single quote (’).
>> w = v’
w=
1
4
7
10
13
Thus, v(1) is the first element of vector v, v(2) its second element,
Furthermore, to access blocks of elements, we use MATLAB’s colon notation (:). For
example, to access the first three elements of v, we write,
>> v(1:3)
ans =
147
Or, all elements from the third through the last elements,
>> v(3,end)
ans =
7 10 13
where end signifies the last element in the vector. If v is a vector, writing
>> v(:)
produces a column vector, whereas writing
>> v(1:end)
produces a row vector.
2.5.2 Entering a matrix :
A matrix is an array of numbers. To type a matrix into MATLAB you must
• begin with a square bracket, [
• separate elements in a row with spaces or commas (,)
• use a semicolon (;) to separate rows
• end the matrix with another square bracket, ].
2.5.3 Matrix indexing :
We select elements in a matrix just as we did for vectors, but now we need two indices.
The element of row i and column j of the matrix A is denoted by A(i,j). Thus, A(i,j)
in MATLAB refers to the element Aij of matrix A. The first index is the row number and
the second index is the column number. For example, A(1,3) is an element of first row and
third column. Here, A(1,3)=3.
Correcting any entry is easy through indexing. Here we substitute A(3,3)=9 by
A(3,3)=0. The result is
>> A(3,3) = 0
A=
123
456
780
2.5.4 Colon operator :
It occurs in several different forms.
Often we must deal with matrices or vectors that are too large to enter one element at a
time. For example, suppose we want to enter a vector x consisting of points
(0, 0.1, 0.2, 0.3, · · · , 5). We can use the command
>> x = 0:0.1:5;
2.5.5 Linear spacing :
On the other hand, there is a command to generate linearly spaced vectors: linspace. It
is similar to the colon operator (:), but gives direct control over the number of points. For
generates a row vector y of 100 points linearly spaced between and including a and b.
y = linspace(a,b,n)
generates a row vector y of n points linearly spaced between and including a and b. This is
useful when we want to divide an interval into a number of subintervals of the same length.
For example,
>> theta = linspace(0,2*pi,101)
divides the interval [0, 2π] into 100 equal subintervals, then creating a vector of 101
elements.
2.5.6 Colon operator in a matrix :
The colon operator can also be used to pick out a certain row or column. For example, the
statement A(m:n,k:l specifies rows m to n and column k to l. Subscript expressions refer
to portions of a matrix
2.5.7 Creating a sub-matrix
To extract a submatrix B consisting of rows 2 and 3 and columns 1 and 2 of the matrix A,
do the following
>> B = A([2 3],[1 2])
B=
45
78
To interchange rows 1 and 2 of A, use the vector of row indices together with the colon
operator.
>> C = A([2 1 3],:)
C=
456
123
780
It is important to note that the colon operator (:) stands for all columns or all rows. To
create a vector version of matrix A, do the following
>> A(:)
ans =
1
2
3
4
5
6
7
8
0
The submatrix comprising the intersection of rows p to q and columns r to s is denoted by
A(p:q,r:s).
As a special case, a colon (:) as the row or column specifier covers all entries in that row or
column; thus
• A(:,j) is the jth column of A, while
• A(i,:) is the ith row, and
• A(end,:) picks out the last row of A.
2.5.8 Deleting row or column :
To delete a row or column of a matrix, use the empty vector operator, [ ].
>> A(3,:) = []
A=
123
456
Third row of matrix A is now deleted. To restore the third row, we use a technique for
creating a matrix
>> A = [A(1,:);A(2,:);[7 8 0]]
A=
123
456
780
Matrix A is now restored to its original form
2.5.9 Dimension :
To determine the dimensions of a matrix or vector, use the command size. For example,
>> size(A)
ans =
33
means 3 rows and 3 columns.
Or more explicitly with,
>> [m,n]=size(A)
2.5.11 Transposing a matrix :
The transpose operation is denoted by an apostrophe or a single quote (’). It flips a matrix
about its main diagonal and it turns a row vector into a column vector. Thus,
>> A’
ans =
147
258
360
Chapter 3 :
Array operations and Linear
Equations :
3.1 Array operations :
MATLAB has two different types of arithmetic operations: matrix arithmetic operations
and array arithmetic operations. We have seen matrix arithmetic operations in the previous
lab. Now, we are interested in array operations.
3.1.1 Matrix arithmetic operations :
As we mentioned earlier, MATLAB allows arithmetic operations: +, −, ∗, and ˆ to be
carried out on matrices. Thus,
A+B or B+A is valid if A and B are of the same size
A*B is valid if A’s number of column equals B’s number of rows
A^2 is valid if A is square and equals A*A
α*A or A*α multiplies each element of A by α
3.1.2 Array arithmetic operations :
since the matrix and array operations are the same for addition
(+) and subtraction (−), the character pairs (.+) and (.−) are not used. The list of array
operators is shown below in Table 3.2. If A and B are two matrices of the same size with
elements A = [aij ] and B = [bij ], then the command
>> C = A.*B
produces another matrix C of the same size with elements cij = aij bij . For example, using
the same 3 × 3 matrices,
we have,
>> C = A.*B
C=
10 40 90
160 250 360
490 640 810
to produce a new matrix whose elements are the square of the elements of the matrix A,
we
enter
>> A.^2
ans =
149
16 25 36
49 64 81
The relations below summarize the above operations. To simplify, let’s consider two
vectors U and V with elements U = [ui] and V = [vj].
3.2 Solving linear equations :
With matrix notation, a system of simultaneous
linear equations is written
Ax = b (3.1)
In linear algebra we learn that the solution to Ax = b can be written as x = A−1
b, where
A−1
is the inverse of A.
For example, consider the following system of linear equations
Calculating the inverse of A manually is probably not a pleasant work. Here the
handcalculation of A−1 gives as a final result: