Documente Academic
Documente Profesional
Documente Cultură
Sa se defineasca un
fisier de tip function care se genereze solutia ecuatiei de gradul 1. Sa se verifice pentru cazurile:
a) a=0; b=0
b) a=0; b=4
c) a=5; b=6
Rezolvare:
Observatii:
• Daca a≠0, atunci x=-b/a
• Daca a=0 si b=0, atunci ecuatia este nedeterminata
• Daca a=0 si b≠0, atunci ecuatia este incompatibila.
fisierul ecuatie1.m:
function x=ecuatie1(a,b)
if a==0
if b==0
x=‘ecuatie nedeterminata’
else
x=‘ecuatie incompatibila’
end
else
x=-b/a;
end
Apelul functiei:
1. >> a=0; b=0;
>>x=ecuatie1(a,b)
R: x=ecuatie nedeterminata
2. >> a=4; b=0;
>>x=ecuatie1(a,b)
R: x=ecuatie incompatibila
3. >>a=5; b=6
>>x=ecuatie1(a,b)
R: x=-1.2
−𝑏±𝑖√∆
• Daca Δ≤0, atunci 𝑥1,2 = 2𝑎
fisierul ecuatie2.m:
function [x1,x2]=ecuatie2(a,b,c)
D=b^2-4*a*c
if D>=0
x1=(-b+sqrt(D))/(2*a);
x2=(-b-sqrt(D))/(2*a);
else
x1=(-b+i*sqrt(abs(D)))/(2*a);
x2=(-b-i*sqrt(abs(D)))/(2*a);
end
end
Apelul functiei:
1. >> a=2; b=5; c=2;
>>[x1,x2]=ecuatie2(a,b,c)
R: D=9
x1=-0.5; x2=-2;
2. >> a=2; b=4; c=2
>>[x1,x2]=ecuatie2(a,b,c)
R: D=0
x1=x2=-1;
3. >>a=2; b=4; c=3;
>>[x1,x2]=ecuatie2(a,b,c)
R: D=-8
x1=-1+0.7071i; x2=-1-0.7071i
Exercitiul 3. Se considera doua numere naturale n si m, unde n>m. Se se scrie un fisier de tip
functie care calculeaza urmatoarele valori:
• Permutari de n: 𝑃𝑛 = 𝑛!
𝑛!
• Aranjamente de n luate cate m: 𝐴𝑚
𝑛 = (𝑛−𝑚)!
𝑛!
• Combinari de n luate cate m: 𝐶𝑛𝑚 = 𝑚!∙(𝑛−𝑚)!
Rezolvare:
fisierul calcul.m:
function [P,A,C]=calcul(n,m)
if n==0 & m==0
disp(‘parametrii nu se pot calcula’)
P=[]; A=[]; C=[];
return;
end
P=fact(n);
A=fact(n)/fact(n-m);
C=fact(n)/(fact(m)*fact(n-m));
end
Apelul functiei:
>>n=8; m=5
>>[P,A,C]=calcul(n,m)
R: P=40320
A=6720
C=56
Exercitiul 4. Sa se scrie un fisier de tip functie care calculeaza suma primelor n+1 numere
naturale pare.
𝑛
𝑆𝑝 = 0 + 2 + 4 + 6 + ⋯ + 2𝑛 = ∑ 2𝑘
𝑘=0
Rezolvare:
Metoda 1:
fisierul suma1.m:
function SP1=suma1(n1)
SP1=0
for i=0:2*n1
if rem(i,2)==0
SP1=SP1+i
end
end
end
Apelul functiei:
1. >>n1=5
>>SP1=suma1(n1)
R: SP1= 30
2. >>n1=10
>>SP1= 110
Metoda 2:
fisierul suma2.m:
function SP2=suma2(n2)
SP2=0
for i=0:2:2*n2
SP=SP+i
end
end
Apelul functiei:
1. >>n2=15
>>SP2=suma2(n2)
R: SP1= 240
Apelul functiei:
1. >>v=[1 3 5 6 8 9]
>>[suma, produs]=suma1(v)
R: suma=32
podus=6480
Metoda 2
fisierul sumaprodus2.m:
function [suma,produs]=suma2(v)
suma=0; produs=1;
i=1;
n=length(v);
while i<=n
suma=suma+v(i)
produs=produs*v(i)
end
end
Apelul functiei:
1. >>v=rand(1,9)
>>[suma, produs]=suma2(v)
Rezolvare:
Fisierul script exercitul6.m
sumaA=0; produsA=1; sumaB=0; produsB=1; i=1; j=1;
disp('matricea A')
for i=1:5
for j=1:5
if i==j A(i,j)=1;
else A(i,j)=1/((i-j)^2);
endif
endfor
endfor
disp(A)
disp('matricea B')
for i=1:5
for j=1:5
if i>2 B(i,j)=1/i;
elseif j>2 B(i,j)=1/j;
else B(i,j)=1/(i+j);
endif
endfor
endfor
disp(B)
for i=1:5
for j=1:5
sumaA=sumaA+A(i,j);
sumaB=sumaB+B(i,j);
produsA=produsA*A(i,j);
produsB=produsB*B(i,j);
endfor
endfor
disp(sumaA)
disp(sumaB)
disp(produsA)
disp(produsB)
1 −1 0
Exercitiul 7. Se considera matricea C= (0 2 3)
4 0 −1
Sa se calculeze:
a) Ct, (Ct)t
b) C3-2C2-C+14I3
Rezolvare:
>> C=[1 -1 0; 0 2 3; 4 0 -1]
>> C’
1 0 4
ans −1 2 0
0 3 −1
>> (C’)’
1 −1 0
ans 0 2 3
4 0 −1
>> C^2*C^2-C+14*eye(3)
0 0 0
ans 0 0 0
0 0 0
Exercitiul 12. Reprezentati in aceeasi fereastra, dar in grafice separate urmatoarele functii:
f=sin(x), pe intervalul [-pi,2pi]
g=cos(x), pe intervalul [-pi,2pi]
h=x2+y2-2, pe intervalul [-2,2]
i=[tg(x), sin(x), cos(x)], pe intervalul [-2pi, 2pi]
Rezolvare:
>> subplot(3,2,1);
>> fplot(@(x) sin(x),[-pi,2*pi])
>> subplot(3,2,2);
>> fplot(’cos(x)’,[-pi,2*pi])
>> subplot(3,2,3);
>> ezplot(inline('x^2+y^2-2'), [-2,2,-2,2])
>> subplot(3,2,5:6);
>> fplot(@(x)[tan(x),sin(x),cos(x)],[-2*pi,2*pi,-4,4])