![Page 1: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/1.jpg)
Programarea si utilizarea
calculatoarelor II
(1) Elemente de baza ale limbajului
MATLAB
![Page 2: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/2.jpg)
The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if statement, the general form of the statement is
if expression1
statements1
elseif expression2
statements2
else
statements3
end
Decizia IF – ELSEIF - ELSE
Expresie logica
DA NU
V1 V2
!!!! Intotdeauna se va executa doar una din variantele V1 si V2
![Page 3: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/3.jpg)
Decizia – exemplul 1
![Page 4: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/4.jpg)
Decizia – calculul unei functii pe
intervale
Sa se determine valoarea functiei f(x)=y, unde:
𝑓 = −3 ∙ 𝑥2 + 4, 𝑥 < −2
𝑥2 − 5 ∙ 𝑥 + 1 𝑥 ≥ −2
![Page 5: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/5.jpg)
Decizia – calculul unei functii pe
intervale
![Page 6: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/6.jpg)
Decizia – calculul unei functii pe
intervale
Sa se determine valoarea functiei f(x)=y, unde:
𝑓 =
2 ∙ 𝑥2 + 1, 𝑥 < −2
1
𝑥 − 2 ≤ 𝑥 ≤ 2
5 ∙ 𝑥 − 2 𝑥 > 2
![Page 7: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/7.jpg)
Decizia – calculul unei functii pe
intervale
![Page 8: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/8.jpg)
Problema simpatica
Mama ii spune lui Bula:
- Du-te pana la magazin si ia o ruda de salam si daca sunt oua, ia zece.
Bula merge la magazin si zice:
- Aveti oua? – Da.
- Dati-mi zece rude de salam.
Scrieti doua secvente de program, una in care Bula face ca in gluma de mai sus, si a doua in care procedeaza cum l-a rugat mama lui.
![Page 9: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/9.jpg)
Operatori relationali
x == 3 este x egal cu 3?
x ~= 3 este x diferit de 3?
x > 3 este x mai mare decat 3?
x < 3 este x mai mic decat 3?
x >= 3 este x mai mare sau egal cu 3?
x <= 3 este x mai mic sau egal cu 3?
!!!!Atentie: raspunsul din punct de vedere logic poate fi DOAR: 1 – ADEVARAT (TRUE) sau 0 – FALS (FALSE)
!!!!Atentie: “=” inseamna atribuire
“==” inseamna comparatie
![Page 10: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/10.jpg)
Operatori relationali
Exemplu:
![Page 11: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/11.jpg)
Operatori logici
& - Operatorul “si logic”
| - Operatorul “sau logic”
~ - Operatorul “negatie”
xor - Operatorul “sau absolut”
!!!!Atentie: din punctul de vedere al MATLAB-ului orice valoare numerica diferita de zero (0) are valoarea logica 1 (TRUE) iar valoarea 0 este 0 (FALSE)
![Page 12: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/12.jpg)
Operatori logici
![Page 13: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/13.jpg)
Operatori logici
![Page 14: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/14.jpg)
Operatori logici – functii echivalente
A & B and(A,B)
A|B or(A,B)
~A not(A)
---
Alte functii aplicate pe matrice:
A = 0 1 2;
0 -3 8;
0 5 0
any(A) – returneaza 1 daca cel putin un element al vectorului (coloanei ‘i’) este diferit de 0 ans = 0 1 1
All(A) – returneaza 1 daca toate elementele unui vector (coloanei ‘i’) sunt diferite de 0 ans = 0 1 0
![Page 15: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/15.jpg)
Operatori
logici
–
functii
echivalente
![Page 16: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/16.jpg)
Operatori logici – “cu
scurtcircuitare” sau inteligenti
&& - si logic, || - sau logic
Acesti operatori evalueaza al doilea termen al expresiei doar daca primul nu decide singur valoarea de adevar a expresiei
A&&B – daca A e zero (FALSE) B nu se evalueaza
A||B – daca A este diferit de zero (TRUE) atunci B nu se evalueaza
![Page 17: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/17.jpg)
Ordinea executiei operatorilor (1/2)
1. Parentheses ()
2. Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)
3. Unary plus (+), unary minus (-), logical negation (~)
4. Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\)
5. Addition (+), subtraction (-)
6. Colon operator (:)
![Page 18: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/18.jpg)
Ordinea executiei operatorilor (2/2)
7. Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)
8. Element-wise AND (&)
9. Element-wise OR (|)
10. Short-circuit AND (&&)
11. Short-circuit OR (||)
![Page 19: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/19.jpg)
Decizia – variante multiple
![Page 20: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/20.jpg)
Decizia – variante multiple - IF
![Page 21: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/21.jpg)
Decizia – variante multiple - SWITCH
switch case_expression
statements
case case_expression
statements
:
otherwise
statements
end
![Page 22: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/22.jpg)
Decizia – variante multiple - SWITCH
![Page 23: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/23.jpg)
Decizia – variante multiple - SWITCH
![Page 24: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/24.jpg)
Inversarea valorii a doua variabile
aux = a;
a = b;
b = aux;
De ce avem nevoie de o variabila suplimentara?
![Page 25: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/25.jpg)
Instructiuni de ciclare - while
while expression
statements
end
Aceasta secventa va repeta instructiunile din corpul functiei while (statements) atata timp cat valoarea de adevar a expresiei expression este adevarata (TRUE)
Aceasta secventa de ciclare se foloseste de obicei cand nu se cunoaste numarul de repetitii al ciclului, acesta fiind conditionat de un eveniment logic
![Page 26: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/26.jpg)
Instructiuni de ciclare - while
![Page 27: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/27.jpg)
Instructiuni de ciclare - While
De cele mai multe ori, in situatiile uzuale, fiecare ciclu se desfasoara regulat, fara intreruperi. Exista insa cazuri in care, se doreste iesirea brusca din executia ciclului sau trecerea la urmatoarea repetare, FARA executia unor anumite parti a ciclului.
Pentru aceste cazuri speciale exista functiile:
break – opreste executia ciclului (iesire fortata)
continue – opreste executia secventei curente si trece la urmatoarea
![Page 28: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/28.jpg)
Instructiuni de ciclare - While
![Page 29: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/29.jpg)
Instructiuni de ciclare - While
![Page 30: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/30.jpg)
Instructiunea de ciclare - For
Execute statements specified number of times
Syntax
for index = values
program statements
:
end
![Page 31: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/31.jpg)
Instructiunea de ciclare - For
for index = values
program statements
:
end
index initval:endval
initval:step:endval
valArray
![Page 32: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/32.jpg)
Instructiunea de ciclare - For
![Page 33: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/33.jpg)
Instructiunea de ciclare - For
![Page 34: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/34.jpg)
Instructiunea de ciclare - For
![Page 35: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/35.jpg)
Instructiunea de ciclare - For
![Page 36: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/36.jpg)
Exemple
1. Sa se calculeze functia f(x):
10x,3x2
10x0,2
0x,x3
)x(f
2
Pentru x = [-3,-2,-1, 0, 0.5, 3, 6, 10, 11, 11.5, 12, 12.5, 13]
![Page 37: Programarea si utilizarea calculatoarelor - cester.utcluj.ro · The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if](https://reader030.vdocuments.us/reader030/viewer/2022040820/5e6908e8b26dea3be55a8ba6/html5/thumbnails/37.jpg)
Exemple
2. Sa se citeasca / introduca un sir. Din sirul dat sa se formeze doua siruri noi, unul cu elementele negative si unul cu elementele pozitive ale acestuia.
3. Se dă funcţia:
Să se determine valorile funcţiei f(x) în intervalul [a,b], parcurs cu pasul k. Sa se afişeze, pentru fiecare valoare a lui x, f(x) dacă funcţia se poate calcula, respectiv un mesaj de eroare în punctele unde funcţia nu se poate calcula.
2
3 x 1, x 8
f (x) 2 x 5, 8 x 4
2 x 1, x 4
x 5