You are on page 1of 40

University of Science and Technology Houari Boumediene

Faculty of Mechanical Engineering & Process Engineering


Department of Mechanical Construction & Production

TP : Optimization
Master 1 FMP + CM

Pr. F. BOUMEDIENE
Mr .W .LAROUI
Mr .S .HARITI
Emails:
fboumediene@usthb.dz
faiza.boumediene@usthb.edu.dz
1
T.P : N° 1
INTRODUCTION A
MATLAB
Pré requis:
✓ Connaissances mathématiques de base
✓ Connaissances informatiques de base

Objectifs TP1 :
✓ Avoir une idée générale sur les fonctions MATLAB

✓ Se familiariser avec l’environnement MATLAB


Saisie de
Voire ou changer de
Aller sur le navigateur de Obtenir fonctions
répertoire courant
répertoire de l’aide Matlap

fournit un accès Voire ou réutiliser des


simplifié aux outils fonctions précédentes
C’est la fenêtre principale avec laquelle
2.1. Fenêtre de commande l’utilisateur de MATLAB interagie.
La fenêtre de commande « Command
Window » (figure 2) est principalement
utilisé pour écrire et exécuter des
commandes tels que :

✓ Entrer des variables


✓ Faire un calcul
✓ Visualiser des résultats
✓ Exécuter des fonctions

Fig. Command Window


Historique de commandes

Les lignes tapées dans la fenêtre Command


Window sont automatiquement
sauvegardées dans la fenêtre « Command
History »
Exercise:

(a) Create a folder on your computer and name it after your last name.

(b) Set the Current Directory of MATLAB to the directory created in Part (a).
(c) Create and save an empty M-file in this directory, and name it after your first name.
>> a = 1 + 2 MATLAB writes the result.

>> b = 4

>> c = sqrt(a^2 + b^2)


>> x = a + b; ;
the result is not written on the screen. This is because is typed at
the end of the expression

>> y = 2*x
Long Lines:

Very long commands or expressions can be typed on several lines. To do this, begin by
writing the expression on one line, then type three periods followed by Enter, and continue
to type the expression on the following line.
Typing Matrices
Recall that a matrix is a multidimensional variable. A 3 by 4 matrix has 3 rows and 4 columns. Define a 3 by 4 matrix
M by typing in the Command Window:

>> M = [ 1 2 3 4; 5 6 7 8; 9 10 11 12]

Note that entries within a row can be separated by a space (or a comma), and that a
semicolon ends a row. Expressions that include matrices can be formed, such as:

>> v=[11 ; 22 ; 33]

>> mv = [M v]
where we concatenate two matrices in a row (with compatible dimensions – same
number of rows). Similarly, two matrices with an equal number of columns can be
vertically concatenated as

>> h = [11 22 33 44]

>> mh = [M ; h]
Matrices Generators

MATLAB makes it convenient to generate certain commonly used matrices. Note the
following statements:

>> ones(3)

>> ones(1,3)
>> zeros(2) >> zeros(1,3)

>> A = eye(3)
Matrix Arithmetic: Vector and matrix operations can be written in the usual way. For
example, define the vector:

>> v=[1;1;1]

Using the matrix A, just defined, we can evaluate:

>> A*v
Colon Operator: Powerful matrix manipulations can be performed by using the colon (:) operator. The most direct
way to use the colon operator is to type:

>> a = 2:8

where a list of numbers is generated from the lowest to the highest given numbers. Also write:

>> 1:2:9

where the middle number is used as an increment, 2. The increment can also be negative as in

>> 50:-5:30
The other common use of the colon operator is to refer to a portion of a matrix. If we define the matrix

>> P=[1 2 3 4 ; 5 6 7 8]

then we can retrieve the second through fourth columns of the second row using the command

>> pp = P(2, 2:4)

Transpose
The transpose of a matrix can be obtained as follows

>> a=[1 2 3;4 5 6]

>> b = a’
The Command linspace

The linspace command is a quick way to generate a row vector of 100 linearly spaced
points between two given numbers. The syntax is

>> linspace(100,200)

The above command generates 100 linearly spaced points between 100 and 200.
Exemple 2 :
a) Define the array z=[1/6 3/4 2/9] in the Command Window.

b) Using the help browser, find the command that can be used to change the format.
Exemple 3:

(a) Create two arrays A and B (4x2 and 2x4, respectively) in the Command Window.
You can randomly choose elements in A and B.

(c) Compute the following: A*B, and B*A

Is A*B = B*A?

What property of matrix multiplication can be recalled with the help of this
example? Comment on these observations in about two lines.
Exemple 3:

(a) Create two arrays A and B (4x2 and 2x4, respectively) in the Command Window. You can randomly
choose elements in A and B.

Is A*B = B*A?
Example 4:

Solve the following systems of linear equations:


3x + 4y = 12 -(3/2)x -2y = -6 x = (8/5) = 1,6 y= 1,8
(5/2)x = 4
4x + 2y = 10. 4x + 2y = 10
M-files

Script M-files : Script files are a series of MATLAB commands stored in a .m file. They do not necessarily
take any input or yield any output. The variables generated during the execution are stored in the MATLAB
workspace. A sample file myMfile.m is shown below.
Exemple 5
Basic Plots : First, consider the simple commands that generate two dimensional (2D) plots.

Use of Plot Command : Plot is one of the simplest graphics commands available in MATLAB.
The following sample code will generate a sine curve.

clc
Close all
Clear all
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y);
Axes and Labels : Notice that the plot generated by the above set of commands does not
generate any axes labels or a title. These can be added by using following set of commands.

clc
clear
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y);
xlabel('X-axis')
ylabel('Y-axis')
title('Plot of Y = sin(x)')
Multiple Plots on the Same Figure : To show how to put several plots on the same figure, a
second line, given by y = 0.5sin(x + 1), will be plotted on the same figure. The following
code can be used for this operation.

clc
clear
x = 0:pi/100:2*pi;
y = sin(x);
y2 = 0.5*sin(x+1);
plot(x,y,x,y2,'-');
xlabel('X-axis')
ylabel('Y-axis')
title('two graphes')
Generating Legends for the Plot : the legend can be added using the following command.

legend('sin(x)','0.5*sin(x+1)')
fplot: Function Plots
The special command fplot plots a function between specified limits.
The function must be specified as y = f (x). It is required to specify the
two end points for x. The sample code for generating a parabola given
by y = x2 + 10 is given here.

You might also like