Sunteți pe pagina 1din 8

Page 1 of 8

Disciplina: Programarea i Utilizarea Calculatoarelor II


Dr.-Ing. Clin Vaida
Laborator 1+2
Introducere n mediul de programare MATLAB
Overview of the MATLAB Environment
MATLAB is a high-level technical computing language and interactive
environment for algorithm development, data visualization, data analysis,
and numeric computation. Using the MATLAB product, you can solve technical
computing problems faster than with traditional programming languages, such as
C, C++, and Fortran.
You can use MATLAB in a wide range of applications, including signal and image
processing, communications, control design, test and measurement, financial
modeling and analysis, and computational biology. Add-on toolboxes (collections of
special-purpose MATLAB functions, available separately) extend the MATLAB
environment to solve particular classes of problems in these application areas.

Interfaa de lucru conine:


- meniul de lucru
- fereastra pentru vizualizarea fiierelor;
- fereastra de lucru unde se pot executa comenzi;
- fereastra cu spaiul de lucru al MATLAB-ului unde se gsesc variabilele
memorate la momentul actual;
- fereastra cu istoricul comenzilor.

Page 2 of 8
Fereastra de help

Formatul de afiare al datelor


Primele comenzi:
- help *****
- doc *****
MATLAB ca i calculator
Operatorii aritmetici de baz n MATLAB
sunt: +,-,*,/,^ i ().
Rulai liniile ce apar dup semnul
>> (prompt)
>> 2+3/4*5
ans =
5.7500
>> 2+3/(4*5)
ans =
2.1500
>> (2+3)/4*5
ans =
6.2500
>> (2+3)/(4*5)
ans =
0.2500
---------------------------------

>> x=1/3
x=
0.3333
>> format long
>> x
x=
0.333333333333333
>> format short
>> x
x=
0.3333
>> format long e
>> x
x=
3.333333333333333e-001

---------Exerciiu Utilizai >>help format i


apoi formatai valoarea lui x n 5 alte
formate.

Page 3 of 8
tergerea memoriei i a comenzilor
introduse n fereastra de comand i
operatorul ;
>>clear all

Ce s-a ntmplat cu variabila pi? Cum


revin la valoarea salvat anterior pentru
aceasta?
Vectori

>> help clear

MATLAB este un mediu de lucru bazat


pe calculul matriceal. Toate variabilele
din MATLAB sunt interpretate ca
matrice:
Rulai:

>>clc
Variabile

>> pi

>> clear all


>> A=100;
>> B=[100,100];
>> c=[100 100;100 100];
>> Whos A B C
??? Undefined function or method 'Whos' for
input arguments of type 'char'.

ans =

>> who A

Ce este ans? O variabil predefinit a


MATLAB-ului n care se salveaz ultima
operaie care nu este atribuit unei alte
variablie

3.141592653589793e+000

Your variables are:

>> x=2

x=

>> whos a
>> whos A
Name
Size

2
>> x
x=
2
>> ans
ans =
3.141592653589793e+000
>>

Exerciiu. Calculai:
i) -2^3+9
ii) 2/3*3
iii) 3*2/3
iv) 3*4-5^2*2-3
v) (2/3^2*5)*(3-4^3)^2
vi) 3*(3*4-2*5^2-3)

Exerciiu.
Rulai:
>> pi
ans =
3.141592653589793e+000
>> pi=2
pi =
2

1x1

>> whos A B C
Name
Size
A
B

1x1
1x2

>> whos A B c
Name
Size
A
B
c

1x1
1x2
2x2

Bytes Class

Attributes

8 double
Bytes Class

Attributes

8 double
16 double
Bytes Class

Attributes

8 double
16 double
32 double

!!! Atenie Att comenzile MATLAB ct i


variabilele sunt difereniate i prin
caracterele mici sau mari.
Vectori linie
>> v = [ 1 3, sqrt(5)]
v=
1.0000

3.0000

>> length(v)
ans =
3
>> v(2)
ans =

2.2361

Page 4 of 8
3

Moduri de generare a unor vectori cu


diferii incremeni
>> x=[1:10]

10

w=
2

z=
8

>> x=[1:2:10]

>> cd=[w z]

x=
1

>> w = [1 2 3], z = [8 9]

x=
1

Formarea vectorilor din ali vectori

cd =

>> x=[1:0.33:5]

Sortai descresctor irul cd.

x=
Columns 1 through 9

Extragerea unor elemente dintr-un ir

1.0000 1.3300 1.6600 1.9900


2.6500 2.9800 3.3100 3.6400

2.3200

Columns 10 through 13
3.9700

4.3000

4.6300

>> r5 = [1:2:6, -1:-2:-7]


r5 =
1

4.9600

Exerciiu
Se d irul:
1 -3 5-7 -1 2 12 4
S se determine elementul minim
utiliznd funcia min.
tiind funcia min, s se calculeze n
acelai fel elementul maxim, media
aritmetic i s se
ordoneze irul.

-1

-3

-5

-3

>> r5(3:6)
ans =
5

-1

>> r5(1:2:7);
ans =
1

-3

-7

Vector coloan

>> v=[1 2 3]

>> c = [ 1; 3; sqrt(5)]

v=

c=

1.0000
3.0000
2.2361

>> v2=[4 5 6]

>> c2=[2
3
4]

v2 =
4

c2 =

>> v3=[7:10]
v3 =
7

10

2
3
4

>> v+v2

>> c+c2

ans =

ans =

-5

>> v+v3
??? Error using ==> plus
Matrix dimensions must agree.

3.0000
6.0000
6.2361

Traspunei vectorii:

-7

Page 5 of 8
A=[1 2 3 4 3 2 1];

[]

3
B= 7
11

>> clear all


>> x=[1 2 3 4 5]

>> x*y
??? Error using ==> mtimes
Inner matrix dimensions must agree.
>> y=[y 5]

16

>> x.*y
ans =
4

25

>> x.*y'
??? Error using ==> times
Matrix dimensions must agree.

1
4
9
16
25

Aplicaie
Calculai valoarea funciei:

y=x cos

y=
2

>> x*y
??? Error using ==> mtimes
Inner matrix dimensions must agree.

>> x=[0:0.2:1]
x=

ans =

0
1.0000

55

( 2 3 x )

Pentru x lund valori ntre 0 i 1 cu


pasul de 0.2.
Rezolvare:

>> x*y'

0.2000

0.4000

0.6000

0.8000

>> y=x.*cos(2*pi*x/3)

>> x'*y

y=

ans =
2
3
4
6
6
9
8 12
10 15

4
8
12
16
20

5
10
15
20
25

Exerciiu
Se d vectorul a= [1 2 3 3 2 1]
S se calculeze 2*a i b*a unde b=4
Produsul element cu element
dot product
Notaie: .*
>> x=[1 2 3 4 5]
x=

ans =

y=

1
2
3
4
5

>> x'.*y'

>> y=[1 2 3 4]

x=

y=

Operaii cu vectori
Produsul scalar - *

>> y=[1 2 3 4 5]

Utilind caracterul .

0 0.1827
-0.5000

0.2677

0.1854 -0.0836

Exerciiu
Calculai funciile:

y=2 x sin ( x )
y=x cos ( z )
Unde: x este un vector linie cu valori
ntre 0 i 1 cu pasul de 0.1. z este un
vector coloan, cu valori ntre 0 i 2. Ce
proprietate trebuie s aib z? Dac
ecuaia a doua e greit cum trebuie
scris pentru a se putea calcula?

Page 6 of 8
1.000000000000000 0.100000000000000
0.001000000000000 0.000100000000000

mprirea element cu element


dot division
Notaie: ./
>> a=[1:5]

ans =
2

Columns 1 through 4

>> b=[6:-1:2]

-0.000000000000000 3.249196962329063
3.141602989056156 3.141592756944053

b=
6

Column 5

3.141592654623336

>> c=[-2:2]

Ridicarea la putere element cu


element dot power of arrays
Notaie: .^

c=
-2

0.000010000000000
>> tan(pi*x)./x

a=
1

Column 5

-1

>> x=[1:5]

>> a./b

x=

ans =
0.1667

0.4000

0.7500

1.3333

2.5000

>> x^2
??? Error using ==> mpower
Inputs must be a scalar and a square matrix.

>> b./a
ans =
6.0000

2.5000

1.3333

0.7500

0.4000

>> x.^2
ans =

>> a./c

ans =
-0.5000 -2.0000

Inf

4.0000

2.5000

>> help inf


INF Infinity.
INF returns the IEEE arithmetic representation
for positive infinity. Infinity is also produced by
operations like dividing by zero, eg. 1.0/0.0, or
from overflow, eg. exp(1000).

Estimai valoarea funciei:

16

25

>> y=[2 2 2 2 2]
y=
2

16

>> x.^y
ans =
1

tan ( x)
y=lim
x
x 0

25

>> y=[2 3 4 5 6]
y=

>> x=[1 0.1 0.001 0.0001 0.00001]

x=
1.0000

>> x.^y
0.1000

0.0010

>> format long


>> x
x=
Columns 1 through 4

0.0001

0.0000

ans =
1

81

1024

15625

Page 7 of 8
Exerciii
Folosind cele nvate estimai valoarea
funciilor:
2

y=lim

3 x +x +1
x +1

y=lim

3 x 2+x +1
3 x3

x 0

x 1

Matrice configuraii speciale,


predefinite
Identificai particularitile urmtoarelor
matrice predefinite:
Ones(m,n)
Zeros(m,n)
Eye(n)
Exerciii Cum putem determina
elementul minim dintr-o matrice?

Se d irul:
X= [1
4
7 10 13 16 19 22
25]
Formai dou iruri, y_par i y_impar
utiliznd funciile nvate.

Formai din matricea C un vector cu 6


elemente.
Matrice diagonal
>> x=[3 4 5 6]

Vectori bidimensionali MATRICE


Introducerea unei matrice:

x=
3

>> A=[1 2 3
4 5 6]

>> diag(x)

A=

ans =

1
4

2
5

3
0
0
0

3
6

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

2
5

3
6

>> C=[1,2,3;4,5,6]

>> A+B

C=

ans =

1
4

2
5

2
8

3
6

>> size(C)
ans =
2
3
>> size(C,1)*size(C,2)

4
10

P[ i ][ j ]

ans =
6

2x3

0
0
5
0

0
0
0
6

6
12

Produsul a dou matrice


Pentru a putea nmuli dou matrice,
acestea trebuie s fie de forma A[m,n]
i B [n,p] rezultatul fiind de forma
C[m,p]
Formula de calcul pentru elementul [i,j]
este:

Dimensiunea unei matrice

>> whos C
Name
Size

Suma a dou matrice


Pentru a putea aduna dou matrice ele
trebuie s aib aceleai dimensiuni

B=
1
4

0
4
0
0

A[i ][k ] B[k ][ j ]

k 1

Se dau matricele:
Bytes Class
48 double

Attributes

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


>> B=[2 2 2];
>> C=[3 3 3;4 4 4];

Page 8 of 8
Care din urmtoarele operaii se pot
efectua:
A*C; B*C; B*A; A*C; A*B;

Produsul a dou matrice element


cu element Dot product
Notaie: .*
n aceast situaie matricele trebuie s
aib aceeai dimensiune
Ce combinaie dintre matricele A, B i C
este valid pentru operaia .*?

Y=
1
1
1

>> X.*Y
??? Error using ==> times
Matrix dimensions must agree.
>> X.*Y'
ans =
1
10

>> X=[1:3;5:-1:3]

>> Y'

X=

ans =

1
5

2
4

3
3

>> Y=[1 2; 1 2; 1 2]

2
2
2

1
2
>>

2
8

3
6

1
2

1
2