Opm 2.M.H.P2
Opm 2.M.H.P2
15
First part: Basic elements
v(3) returns the 3rd element of vector v. Argument 2:4 selects a block of elements (here from the
second to the fourth).
1.3.2 Some useful functions
We present in this paragraph a set of usual functions related to the use of tables.
+ addition of matrices
- subtraction of matrices
* die product
^ power
17
First part: Basic elements
18
First part: Basic elements
0 0 1
A random matrix (elements between 0 and 1):
1) :>> R = rand (2, 2)
R =
0.9575 0.1576
0.9649 0.9706
A diagonal matrix:
>> D = diag([42,33,0,71])
D =
42 0 0 0
0 33 0 0
0 0 0 0
0 0 0 71
Unlike the previous ones, this last function takes as an argument a vector. The size of the diagonal
matrix is therefore determined by the size of the vector.
1.3.5 Extraction of sub-arrays
It is often useful to extract blocks from an existing table. For this we use the operator « : ». To
do this, it is necessary to specify for each index the start value and the end value. The general
syntax is therefore as follows (for a two-dimensional array): array (start:end, start:end).
23
Thus to extract the block [ ] from the matrix M, we will type:
56
>> M(1:2,2:3)
ans =
2 3
5 6
The character « : » alone, means the entire length is extracted. In this way, one can isolate a
complete row, or column.
Example
>> M(1:2,:)
ans =
1 2 3
4 5 6
>> M(1,:)
ans =
1 2 3
>> M(:,2)
ans =
2
5
8
1.3.6 Matrix construction by blocks
You know this principle in mathematics. For example, to start the previously defined matrices
and vectors, we can define the matrix
19
First part: Basic elements
Which is a 4x4 matrix. To do ¸can under Matlab, we do as if the blocks were scalars, and we
simply write:
>> N= [M V U 0]
N =
1 2 3 11
11 12 13 12
21 32 23 13
1 2 3 0
Or by using the character;
>> N= [M V; U 0]
This syntax is very used to lengthen vectors or matrices, for example if I want to add a column
A M, made up by V:
>> M= [M V]
M =
1 2 3 11
11 12 13 12
21 32 23 13
If I want to add a line to him, made up of U:
>> M = [M; U]
M =
1 2 3
11 12 13
21 32 23
1 2 3
1.3.7 Operations on tables
We saw in the preamble that Matlab did not make a strong distinction between tables and
matrices. In fact, for Matlab, everything is a picture, and a matrix is only a table which has a
particular mathematical meaning.
So, if historically MATLAB offered functions in most cases for counting implicating matrices,
today the functional ghost broadly stretched, and MATLAB offers functions for all counting
on numerical data, tabulées in picture, or matrices in the mathematical sense of term. In this
chapter, we review the arithmetic operations that we can perform with tabulated data or
matrices [3].
20
First part: Basic elements
Both operators are the same as for scalars, namely + and -. From the moment the two tables
concerned have the same size, the resulting table is obtained by adding or subtracting the terms
from each table.
These operators are noted.*, / And.^ (Be careful not to forget the point).
They are planned to carry out term operations on two tables of the same size.
These operations are fundamental when we want to trace curves, and we will always see it
again in this case of use, or more generally when we want to carry out these arithmetic
operations on a set of tabulated data.
Multiplication
Since you can manipulate matrices, Matlab also offers these matrix operations. The
multiplication is noted simply * and should not be confused with the term term multiplication
(which by definition does not give the same result).
It goes without saying that if we write a*b, the number of columns of A must be equal to the
number of B lines for the multiplication to work.
Division
The matrix division is defined as the multiplication by the reverse of the matrix. Thus A/B
represents the matrix has multiplied (in the matrix sense) by the reverse matrix of B.
Complement
There is also a division on the left which is noted \.Thus the syntax A \ B means the opposite
of A multiplied by B. This symbol can also be used to resolve linear systems: if V is a vector,
a \ v represents mathematically a^(- 1) to say the solution of the linear system AX = V.
Power
The umpteenth power of a matrix represents this matrix multiplied, in the matrix sense, n times
by itself.
21
First part: Basic elements
In many cases (and especially if the matrices are square and of the same dimensions), the two
types of operations can be carried out, without producing a syntax error for the Matlab language,
but will produce completely different digital results.
Example
To show the difference between operators. * And *, let's take a trivial example involving the
Multipliée Identity matrix at the matrix (I*A). Here is the multiplication in the sense of matrices:
>> [1 0; 0 1] * [1 2; 3 4]
ans =
1 2
3 4
12
In this case, we find the matrix[ ], by definition of what the identity matrix is.
34
Fundamental
If you handle tabulated data, the operations you are going to carry out are necessarily term
term; the operators *, / and ^ will then be preceded by a point.
Complement
For these three operations, for tables or matrices, there are also functions that can replace
operators:
22
First part: Basic elements
Transposition
The transposition operator is the character and is often used to transform line vectors into
column vectors and vice versa.
Summary
The following table summarizes the various operators applicable to matrices.
The following table summarizes the different operators applicable to matrices or tables.
2 4 6
1 9 7
-3 1 1
>> A (2, 3)
ans =
7
>> A (2, 3) = 7
A =
2 4 6
1 9 7
-3 1 1
>> A'
23
First part: Basic elements
ans =
2 1 -3
4 9 1
6 7 1
>> inv(A)
ans =
1.0000 0 0
0.0000 1.0000 0.0000
0 -0.0000 1.0000
>> rank(A)
ans =
3
>> det(A)
ans =
84
>> eye(7)
ans =
ans =
1 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 1
>> B = [1 9 0 ; 1 3 1 ; 0 -1 1]
B =
1 9 0
1 3 1
0 -1 1
>> A + B
ans =
24
First part: Basic elements
3 13 6
2 12 8
-3 0 2
>> 11 + A
ans =
13 15 17
12 20 18
8 12 12
>> 11 * A
ans =
22 44 66
11 99 77
-33 11 11
>> A * B
ans =
6 24 10
10 29 16
-2 -25 2
>> B * A
ans =
11 85 69
2 32 28
-4 -8 -6
>> A*A*A*A
ans =
-768 4008 2880
-1428 7812 5772
180 -564 -420
>> A^6
ans =
-55440 324576 240480
-110016 635760 470304
7200 -42048 -30384
Entering a matrix with complex coefficients of size 2 x 3:
>> C = [2- i 0 0; 1 - i 2*i 2]
C =
2.0000 - 1.0000i 0 0
1.0000 - 1.0000i 0 + 2.0000i 2.0000
>> C * A
25
First part: Basic elements
ans =
The logical value 'True' is called True, and 'False' is called False; These values are of the logical
type.
Relational operators
Relational operators allow the comparison of two values between them. The following table
synthesizes the syntaxes of the different relational operators available:
Example
Let's take some examples of relational operations on any numeric values:
>> 20 == 20
ans =
1
>> 1.4 > 6.2
ans =
0
26
First part: Basic elements
Example
>> A=[2 4; 5 2]
A =
2 4
5 2
>> A > 3
ans =
0 1
1 0
Les termes de A supérieurs à 2 donnent 1 (true), les autres 0 (faux). La possibilité d'appliquer
une opération relationnelle sur un tableau sera exploitée dans la suite pour calculer les valeurs
d'une fonction définie par morceaux.
Comparison of two tables
Example
Now let's take any two tables of the same dimension, and compare them:
>> A=[-1 6 ; 3 -4]
A =
-1 6
3 -4
>> B=[-9 2.5 ; 1 -2]
B =
-9.0000 2.5000
1.0000 -2.0000
>> T = A > B
T =
1 1
1 0
Complement
It may be useful to know if the Table T Result of a test contains that non-zero values (or logical
values true) or if there is at least one non-null value (or logical value True).
To do this, you can use the All (T) and Any (T) functions, respectively. By default, if T is not a
vector, these functions test the presence of non -zero values according to the columns of T.
Example
Let's resume the previous example:
>> all(T)
27
First part: Basic elements
ans =
1 0
>> any(T)
ans =
1 1
>> all(all(T))
ans =
0
>> any(any(T))
ans =
1
Logical operators
Logical operators are operators that apply exclusively to logical type values. They allow the
combination of logical conditions. The following table gives the syntax of logical operators
available in Matlab:
Syntax of logical operators
symbol function
A et B A&B and(A,B)
A ou B A|B or(A,B)
A or exclusive B xor(A,B)
Negation of A ~A not(A)
Logic table
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
A and B can be logical scalar values or logical values tables of the same dimensions. For tables,
these operations apply to term.
28
First part: Basic elements
We call logical operators short-circuit operators ** and ||. Unlike operators * and |, short-circuit
logical operators do not assess the second opendial, if the value of the first already allows you to
know the overall result.
Table lengths
The size function applied to a matrix returns a table of two integers: the first is the number of
lines, the second the number of columns. The command also works on the vectors and returns 1
for the number of lines (resp. Columns) of a line vector. For vectors, the Length command is
more practical and returns the number of components of the vector, whether line or column.
1.3.11 Creation of the .m file of a function
To define a new function in Matlab, we write the definition of the function in a file with an
extension .m (M-File function). The name of the file must be the name of the first defined
function (the visible only one). It is performed by typing the name of the function with the list of
arguments in parentheses.
The first line of the file of function must follow the following syntax:
They must therefore declare stocks calculated by function in arguments of exit, and parameters
of function in arguments of entrance.
Example 1
function x=racinefun(a,b)%[a,b]=[1,4]
% calcul la racine de f(x) définie ci-après sur [a,b]
fa=f(a); fb=f(b); x=(a+b)/2;
if (fa*fb>0) x=-Inf; return; end;
while (b-a)>eps*x
x=(a+b)/2; fx=f(x);
if(sign(fx)==sign(fa))
a=x; fa=fx;
else
b=x; fb=fx;
end;
end;
% definition de f(x) (fonction locale)
function y=f(x)
y=x^3-3*x-7;
29
First part: Basic elements
The function root ci over is written in a racinefun.m file. To carry it out, they knock simply
>> racinefun(1,4)
ans =
2.4260
Example 2
we want to create a function which calculates the cube of a number X then returns the result y.
The program will be:
function y = cub3(x)
% cette fonction calcule le cube d’un nombre x
y = x^3 ;
end
>> cub3(5)
ans =
125
The function will be recorded under the name "cub3.m". A function must always end with an end
delimator ("end").
Example 3
function y = foc(x)
% cette fonction calcule le cube d’un nombre x
y =cos( x.^2)+x.^3+10./(x-1) ;
end
Let be the function:
With version 6.5. the default text editor is the 'M-File Editor' application.
b) Give this function a name (in this example foc) and enter its mathematical expression:
Calculation of y ( x = 0 ) :
>> foc(0)
30
First part: Basic elements
ans =
-9
Calculation of y ( x = 10 ) :
>> foc(10)
ans =
1.0020e+003
>> foc(1)
Warning: Divide by zero
ans =
Inf
With a vector in argument, the function returns a vector:
31
First part: Basic elements
e/ Define matrix B = [0.5*ones(4,2) - 2*ones(4,2)] and give acquired result. Is the product of A
and B possible ? Justify your answer. If yes which is the MATLAB order which allows to make
this product?
Solutions
A =
1 4 1 1
1 7 1 2
1 4 1 2
3 10 2 5
b1 =
1 2
1 2
2 5
b2 =
1 7 1
1 4 1
3 10 2
ans =
4
ans =
1
2
ans =
1 4 1 2
ans =
1 0 0 0
1 7 0 0
1 4 1 0
3 10 2 5
D =
0 -4 -1 -3
-4 -48 -4 -20
-1 -4 0 -4
-3 -20 -4 -24
B =
-1.5000 -1.5000
-1.5000 -1.5000
-1.5000 -1.5000
-1.5000 -1.5000
ans =
4 4
ans =
4 2
ans =
32
First part: Basic elements
-10.5000 -10.5000
-16.5000 -16.5000
-12.0000 -12.0000
-30.0000 -30.0000
33
First part: Basic elements
34
First part: Basic elements
zoom zooms in
The fplot command draws the graph of a function over a given interval.
Syntax is: fplot(’nomf’, [xmin , xmax]) où
nomf is either the name of an incorporated matlab function, or an expression defining a
function of the variable X, or the name of a user function.
[xmin , xmax] is the interval for which the function graph is drawn.
Let us illustrate by examples the three ways to use the command fplot.
Example 1
Draw the function graph 'x*cos(2*x)+x^2'in [-2*pi 2*pi]
>> fplot('x*cos(2*x)+x^2',[-2*pi 2*pi])
35
First part: Basic elements
50
40
30
20
10
-10
-6 -4 -2 0 2 4 6
2*x^2*sin(-2*x)+x
>> fplot('2*x^2*sin(-2*x)+x', [-pi pi])
36
First part: Basic elements
15
10
-5
-10
-15
-3 -2 -1 0 1 2 3
37
First part: Basic elements
0.5
-0.5
-1
-1.5
-2
-3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2
38
First part: Basic elements
50
40
30
20
10
-10
-20
-30
-40
-50
-20 -15 -10 -5 0 5 10 15 20
We can specify to matlab what should be the color of a curve, what should be the line style and
/ or what should be the symbol at each point (xi, yi). For this we give a third input parameter to
the plot command which is a string of 3 characters of the form 'cst' with c denoting the color of
the line, s the symbol of the point and t the line style. The possibilities are as follows:
'red' 'r'
'green' 'g'
'blue' 'b'
'cyan' 'c'
'magenta' 'm'
'yellow' 'y'
'black' 'k'
'white' 'w'
: Point
o: Circle
39
First part: Basic elements
x: X-Mark
+: Plus
s: Square
*: Star
d: Diamond
v: Triangle (down)
^: Triangle (up)
Keywords--Flow control functions, such as for and if, as well as the continuation ellipsis (...),
are colored blue.
Comments--All lines beginning with a %, designating the lines as comments in MATLAB, are
colored green. Similarly, the block comment symbols, %{ and %}, as well as the code in between,
appear in green. Text following the continuation ellipsis on a line is also green because it is a
comment.
Strings--Type a string and it is colored maroon. When you complete the string with the closing
quotation mark ('), it becomes purple. Note that for functions you enter using command syntax
instead of function syntax, the arguments are highlighted as strings. This is to alert you that in
command notation, variables are passed as literal strings rather than as their values. For more
information, see MATLAB Command Syntax in the MATLAB Programming documentation.
Unterminated strings--A single quote without a matching single quote, and whatever follows
the quote, are colored maroon. This might alert you to a possible error.
40
First part: Basic elements
Example 2
50
40
30
20
10
-10
-20
-30
-40
-50
-20 -15 -10 -5 0 5 10 15 20
41
First part: Basic elements
50
40
30
20
10
-10
-20
-30
-40
-50
-20 -15 -10 -5 0 5 10 15 20
42
First part: Basic elements
30
20
10
-10
-20
-30
-40
-50
-60
-4 -3 -2 -1 0 1 2 3 4
Example 4
N=100;
x = rand(1,N); y = rand(1,N);
plot(x,y,'bd')
43
First part: Basic elements
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x = [1:40:3000];
y = x.^3;
loglog(x,y)
44
First part: Basic elements
12
10
10
10
8
10
6
10
4
10
2
10
0
10
0 1 2 3 4
10 10 10 10 10
t = [0:0.001:8];
h = 15*exp(-t) - 8*exp(-5*t);
45
First part: Basic elements
plot(t,h);
grid
xlabel('temps en minutes')
ylabel('concentation en gramme par litre')
title(['evolution de la concentration du produit , num2str(P),
... au cours du temps'])
gtext('concentration maximale')
8
concentation en gramme par litre
0
0 1 2 3 4 5 6 7 8
temps en minutes
-1
-2
-3
-4
-5
-6
0.5 1 1.5 2 2.5 3 3.5 4
47
First part: Basic elements
0 0 200
-0.5 -0.5 0
-1 -1 -200
2 4 6 8 10 12 2 4 6 8 10 12 -2 0 2
3 1.5 1
2 1 0.5
1 0.5 0
0 0 -0.5
-2 0 2 -2 0 2 -2 0 2
Example 3
Time (hours) 0 2 4 6 8 10 12 14 16
Temperature (°C) 20 23 30 33 32 37 34 39 36
clc
Time = [0 2 4 6 8 10 12 14 16];
Temperature = [20 23 30 33 32 37 34 39 36];
plot(Time , Temperature)
grid on
xlabel ( ' Time ( hours)' )
ylabel ( ' Temperature ( °C)' )
title ( ' Monitoring of temperature ')
axis ( [ 0 18 10 40 ] )
48
First part: Basic elements
Monitoring of temperature
40
35
30
Temperature ( °C)
25
20
15
10
0 2 4 6 8 10 12 14 16 18
Time ( hours)
Example 4
𝑡(𝑡 2 +1)
𝑦=
10
t = 0 : 0.001 : 2
y = t.*(1 + t.^3)./5
plot ( t , y ) ; grid on
49
First part: Basic elements
3.5
2.5
1.5
0.5
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Example 5
x = 4cos(2*t),y = sin(3*t)
t = 0 : pi/200 : 2*pi
x = 4*cos(2*t)
y = sin(3*t)
plot ( x , y )
grid on
50
First part: Basic elements
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
Example 6
𝑦 = 𝑓(𝑥) = 1 + 2𝑥 + sin(3𝑥 2 )
• First method:
fplot('1+ 2*x + sin(3*x*x)', [ 1 8 ]),grid on
51
First part: Basic elements
18
16
14
12
10
2
1 2 3 4 5 6 7 8
7.5
6.5
5.5
4.5
4
2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3
grid off
xlabel(axis of abscissa ')
ylabel('axis of ordinates')
title('y=f(x)')
52
First part: Basic elements
y=f(x)
8
7.5
6.5
axis of ordinates
5.5
4.5
4
2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3
axis of abscissa
zoom on
zoom off
53
First part: Basic elements
-1
-2
-3
-4
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
gtext('fonction 1')
gtext('fonction 2')
4
function 1
3
2
function 2
1
-1
-2
-3
-4
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
54
First part: Basic elements
• Second method
cos(𝑥) sin(𝑥)
𝑦 = 𝑓(𝑥) = sin(2𝑥) + −
2 4
1.5
0.5
-0.5
-1
-1.5
1 2 3 4 5 6 7 8 9 10 11
>> hold on
>> fplot('sin(2*x) ', [ 0 10 ] ,'g')
1.5
0.5
-0.5
-1
-1.5
0 1 2 3 4 5 6 7 8 9 10
55
First part: Basic elements
X =
1.0000
1.0200
1.0400
1.0600
……………….
10.9000
10.9800
11.0000
Y =
0.9093
0.8919
0.8731
………………
0.1900
0.0311
-0.0089
>> fplot ( 'sin(2*x) ' , [ 1 11 ] , '.' ),grid on
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
1 2 3 4 5 6 7 8 9 10 11
56