Lab Num Matlab
Lab Num Matlab
SCHEIBER
MATLAB
Braşov
Cuprins
I Matlab 4
5 Derivare numerică 49
5.1 Derivarea funcţiilor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2
CUPRINS 3
7 Integrare numerică 59
7.1 Integrarea funcţiilor de o variabilă reală . . . . . . . . . . . . . . . . . 59
7.2 Calculul numeric al integralelor duble . . . . . . . . . . . . . . . . . . 62
Bibliografie 72
Part I
Matlab
4
Capitolul 1
Cap. 2
[Link]
• Scilab
• Octave
5
6 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN MATLAB
• Constante
Tip Mnemonic Valoare
real pi π
real eps ≈ 2.220410−16
inf ∞
p
complex i ,j i= −1
NaN Not A Number
0/0
ans =
NaN
• Literali
• Şiruri de numere
Progresia aritmetică introduce definind primul termen (a), raţia (r ) şi o
marginea superioară sau inferioară (M ), după cum raţia este pozitivă sau
negativă, prin sintaxa
a :r :M
Dacă parametrul r lipseşte atunci raţia este 1.
a=0.2:0.3:1
0.2 0.5. 0.8
b=1:-0.3:0
1. 0.7 0.4 0.1
8 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN MATLAB
1. i=1:n
a=formula termenului general, functie de i
2. a = [a 1 , a 2 , . . . , a n ],
unde a 1 , a 2 , . . . , a n sunt termenii şirului.
i=1:4
1 2 3 4
b=^2
1 4 9 16
b=[1,4,9,16]
a==b
1 1 1 1
c=[1,2,3];
for i=1:length(c) c=fliplr(c); end
c
c =
3 2 1
• Matrice
1.2. OBIECTE MATLAB 9
a=[1,2,3;4,5,6]
Elementele unei linii se pot separa prin virgulă sau spaţiu. Elementele
unei matrice pot fi: constante/variabile numerice, constante booleene,
şiruri de caractere, polinoame, funcţii raţionale.
O progresie aritmetică pr og = a : r : M este interpretată ca un vector
linie (adică o matrice cu o singură linie).
Exemplu. Progresiile aritmetice a = 0.2, 0.6, 1.0, 1.4, 1.8 şi b = 0, −1, −2, −3, −4
se obţin prin
a=0.2 : 0.4 : 2
a=
! 0.2 0.6 1.0 1.4 1.8 !
b=-1 : -1 : -4
b=
! -1 -2 -3 -4 !
i = [i 1 , i 2 , . . . , i m ]
j = [ j1, j2, . . . , jm ]
v = [v 1 , v 2 , . . . , v m ]
atunci a i k , j k = v k , ∀k.
Funcţia full(a ) afişează o matrice rară a în formatul obişnuit.
Funcţia nnz(a) are ca valoare numărul elementelor nenule din a.
– Matrice speciale.
10 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN MATLAB
variabilaMatrice(numărLinie, numărColoană)
– Funcţii matriceale.
Mnemonic Semnificaţia
triu(A) Matricea superior triunghiulară a lui A
tril(A) Matricea inferior triunghiulară a lui A
diag(A) Vectorul cu elementele diagonale ale lui A
diag(v) Matrice pe diagonală cu elementele vectorului v
size(A) Şir cu dimensiunile lui A
[v; a]
[a; v]
a(l 1 : l 2 , c 1 : c 2 )
½
t r ue dacă j ∈ {c 1 , . . . , c q }
col = (col j )1≤ j ≤n , col j =
f al se în caz contrar
Extragerea rezultă din
a(l i n, col )
a=sym(1/2)
a =
1/2
b=sym(1/3)
b =
1/3
(a+b)*a
ans =
5/12
– Variabile simbolice
syms a b x
f=a*x+b
f =
b + a*x
subs(f,x,2)
ans =
2*a + b
– Expresii simbolice
1. x=sym(sqrt(1+sym(sqrt(2))))
x =
(2^(1/2) + 1)^(1/2)
1.2. OBIECTE MATLAB 13
>> (x^2-1)^2
ans =
2
2. syms x y
expand((1+x)^3)
ans =
factor((x+y)^7-x^7-y^7)
ans =
ans =
1
- (3^(1/2)*1i)/2 - 1/2
(3^(1/2)*1i)/2 - 1/2
– Calcul diferenţial şi integral:
Derivare simbolică, Calculul primitivei, Integrare simbolică
syms x
f=sin(x);
diff(f,x)
ans =
cos(x)
int(f,x)
ans =
-cos(x)
14 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN MATLAB
int(f,0,pi)
ans =
– Polinoame
c=[1,-3,2];
p=poly2sym(c)
p =
x^2 - 3*x + 2
a=sym(1/2)
a =
1/2
subs(p,a)
ans =
3/4
cp=sym2poly(p)
cp =
1 -3 2
q=poly2sym([1,-1])
q =
x - 1
simplifyFraction(p/q+q)
1.2. OBIECTE MATLAB 15
ans =
2*x - 3
gcd(p,q)
ans =
x - 1
lcm(p,q)
ans =
x^2 - 3*x + 2
[d,u,v]=gcd(p,q)
d =
x - 1
u =
v =
d-u*p-v*q
ans =
0
16 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN MATLAB
{e 1 , e 2 , . . . , e n }
– l (i )
returnează al i -lea element al listei;
Exemplu.
l={1,true,’abc’,[1,2;3,4]}
l =
• Instrucţiunea de atribuire
variabila=expresie
• Instrucţiuni condiţionate
1.
if condiţie
instrucţiuni
elseif condiţie
instrucţiuni
else
instrucţiuni
end
a=3;b=2;delta=a^2-4*b;
if delta>=0 x=(-a+sqrt(delta))/2;y=(-a-sqrt(delta))/2;else x=-a/2;y=sqrt(-delta)/2;end
2.
switch expresie
case expr esi e 1
instrucţiuni
case expr esi e 2
instrucţiuni
..
.
otherwise
instrucţiuni
end
• Instrucţiuni de ciclare
Exemplul 1.3 5i =1 i
P
s=0;
for i=1:5 s=s+i; end
a=[1,2,3,4,5,6,7,8,9];
c0=zeros(3,1);c1=zeros(3,1);c2=zeros(3,1);
i0=0;i1=0;i2=0;
for i=1:length(a)
switch a(i)-floor(a(i)/3)*3
case 0
i0=i0+1;c0(i0)=a(i);
case 1
i1=i1+1;c1(i1)=a(i);
case 2
i2=i2+1;c2(i2)=a(i);
end
end
[c0,c1,c2]
ans =
3 1 2
6 4 5
9 7 8
• Linie de comandă.
– Funcţie anonimă
f nt = @(x)expr esi e
st r = 0 expr esi e 0
f nt = str2func([0 @(x)0 , st r ])
se defineşte prin
– f=@(x,y) [x+y,x.^2+y.^2]
f =
@(x,y)[x+y,x.^2+y.^2]
>> f(2,3)
ans =
5 13
– str=’[x+y,x.^2+y.^2]’
str =
[x+y,x.^2+y.^2]
>> g=str2func([’@(x,y)’,str])
g =
20 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN MATLAB
@(x,y)[x+y,x.^2+y.^2]
>> g(2,3)
ans =
5 13
function [y 1 , . . . , y n ]= f (x 1 , . . . , x m )
instructiuni Matlab
function [u,v]=f(x,y)
u=x+y;
v=x^2+y^2;
cu utilizarea
[u,v]=f(2,3)
u =
v =
1.4. FUNCŢII(SUBPROGRAME) ÎN MATLAB 21
13
function [u,v]=eq2(a,b)
delta=a*a-4*b;
if delta>=0
u=0.5*(-a+sqrt(delta));
v=0.5*(-a-sqrt(delta));
disp(’Real roots’)
else
u=-a/2;
v=sqrt(-delta);
disp(’Complex conjugate roots’)
end
Funcţia disp(obiectMatlab) afişează valoarea argumentului.
function r=polyval1(x,c)
[l,n]=size(c)
r=c(1)
for i=2:n
r=r*x+c(i)
end
endfunction
function r=polyval2(x,c)
[l,n]=size(c)
i=0:n-1
u=x^i
r=u*c’
endfunction
Exemplul 1.9 Calculul coeficienţilor binomiali.
function c=binomialcoeff(n,k)
c=0;
if k>n
return
end
if n==0 & k>0
c=1;
else
if k==0
c=1;
else
c=binomialcoeff(n-1,k-1)+binomialcoeff(n-1,k);
end
end
µ ¶
n n!
Varianta bazată pe formula = k!(n−k)! = exp(gammaln(n+1)−gammaln(k+
k
1) − gammaln(n − k + 1)).
function c=binomialcoeff1(n,k)
c=0;
if k>n then
return
end
c=exp(gammaln(n+1)-gammaln(k+1)-gammaln(n-k+1));
function t=fib(n)
t=[1,1];
for i=3:n
t(i)=t(i-1)+t(i-2);
end
Exemplul
p 1.11 Funcţie pentru calculul limitei şirului (a n )n∈N definit prin a 0 =
p
2, a n+1 = a n + 2.
function [l,error]=lim(tol,nmi)
v=sqrt(2);
cont=true;
1.4. FUNCŢII(SUBPROGRAME) ÎN MATLAB 23
ni=1;
while(cont)
ni=ni+1;
l=sqrt(v+2);
d=abs(v-l);
v=l;
if(d<tol)|(ni>nmi)
cont=false;
end
end
if(d<tol)
error=0;
else
error=1;
end
Exemplul 1.12 Procedură pentru generarea nodurilor lui Cebîşev de speţa a doua
dintr-un interval [l , r ]. În lipsa parametrilor suplimentari intervalul implicit este
[−1, 1].
În intervalul [−1, 1], pentru n ∈ N∗ , nodurilor lui Cebîşev de speţa a doua sunt
definite ca punctele de extrem ale polinomului lui Cebîşev Tn (x) : cos kπ n
, k =0:
n.
Bijecţia între intervalele [−1, 1] şi [l , r ] este x 7→ l + r −l
2 (x + 1).
function x=chebpoints(n,varargin)
l=-1;
r=1;
if length(varargin)>0
if length(varargin)==2
l=varargin{1};
r=varargin{2};
disp([l,r])
else
error(’Wrong number of input parameters’)
end
end
k=0:n;
x=l+0.5*(r-l)*(cos(k*pi/n)+1);
24 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN MATLAB
pl ot (x, y)
– stilul
‘-’ linie continuă
‘–’ linie întreruptă
‘:’ curba reprezentată prin puncte
‘-.’ curbă reprezentată prin combinaţia segment punct
– marcaj (selecţe)
‘o’ cerc
‘+’ +
‘*’ asterisc
‘x’ x
‘s’ patrat
‘d’ diamant
– Culoare
‘r’ roşu
‘g’ verge
‘b’ albastru
‘c.’ cyan
‘m’ violet
‘y’ galben
‘k’ negru
‘w’ alb
1.5. GRAFICĂ ÎN MATLAB 25
t=-pi:0.1:pi;
x1=sin(t);
x2=cos(t);
plot(t’,x1’,t’,x2’,’r’)
2
Exemplul 1.14 Reprezentarea funcţiei f (x) = xe −x în intervalul [−3, 3].
f=@(x)x.*exp(-x.^2);
interval=[-3,3];
fplot(f,interval)
syms t
x=sin(2*t);
y=cos(3*t);
fplot(x,y)
quiver(X , Y ,U ,V )
Exemplul 1.16 Să se obţină imaginea câmpului vectorial (|y|, x − 2y) în in-
tervalul [−2, 2]2 .
u=@(x,y)abs(y);
v=@(x,y)x-2*y;
x=linspace(-2,2,21);
y=linspace(-2,2,21);
[X,Y]=meshgrid(x,y);
U=u(X,Y);
V=v(X,Y);
quiver(X,Y,U,V)
x=[1,2,3];
y=[10,20];
atunci
[X,Y]=ndgrid(x,y)
[U,V]=meshgrid(x,y)
X =
U =
1 1
2 2
1 2 3
3 3
1 2 3
V =
Y =
10 10 10
10 20
20 20 20
10 20
10 20
Aşadar meshgrid(x, y) = ndgrid(x 0 , y 0 ).
1.5. GRAFICĂ ÎN MATLAB 29
1.5.2 Grafică 3D
• mesh(X,Y,Z): Reprezentarea funcţiilor f : D ⊂ R2 → R.
X = (X i )1≤i ≤m şi Y = (Y1≤ j ≤n sunt vectori daţi în ordine crescătoare, iar
Z = (Zi , j ), i ∈ {1, . . . , m}, j ∈ {1, . . . , n} reprezintă valoarea funcţiei calculată
în (X i , Y j ).
Dacă suprafaţa este definită prin z = f (x, y), x ∈ [a, b], y ∈ [c, d ] atunci se
defineşte funcţia Matlab
function [x,y,z]=suprafata(m,n)
x=linspace(a,b,m);
y=linspace(c,d,n);
z=zeros(m,n)
for i=1:m
30 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN MATLAB
for j=1:n
z(i,j)=f(x(i),y(j));
end
end
[x,y,z]=suprafata(m,n);
mesh(x,y,z)
p
Semisfera superioară este dată de z = 1 − x 2 − y 2 , pentru care construim
funcţia Matlab
function[x,y,z]=sfera(r,m,n)
x=linspace(-r,r,m);
y=linspace(-r,r,n);
for i=1:m
for j=1:n
if x(i)^2+y(j)^2<=r^2
z(i,j)=sqrt(r^2-x(i)^2-y(j)^2);
else
z(i,j)=NaN;
end
end
end
[x,y,z]=sfera(1,20,20);
mesh(x,y,z)
f pl ot 3(x, y, z)
f pl ot 3(x, y, z, i nt er v al )
x=@(t)t.^2.*sin(t);
y=@(t)t.^2.*cos(t);
z=@(t)t;
fplot3(x,y,z)
t=linspace(0,1,10000);
comet(t,t.^2)
• Funcţie care crează un fişier gif animat pentru desenarea graficul unei
funcţii f : [a, b] → R.
8 plot ( t ( 1 : n ) , x ( 1 : n ) )
9 axis ( [ min( t ) max( t ) min( x ) max( x ) ] ) ;
10 drawnow
11 frame = getframe ( 1 ) ;
12 im = frame2im ( frame ) ;
13 [ imind ,cm] = rgb2ind (im , 2 5 6 ) ;
14 i f n == 1 ;
15 imwrite ( imind ,cm, f i l e , ’ g i f ’ , ’ Loopcount ’ , i n f ) ;
16 el se
17 imwrite ( imind ,cm, f i l e , ’ g i f ’ , ’ WriteMode ’ , ’append ’ ) ;
18 end
19 end
Capitolul 2
Cap. 1 Cap. 3
A=[1,2,-1,3,2;2,4,-2,5,1;-1,-2,1,-3,-4;3,6,2,10,7;1,2,4,0,4];
[L,U,P]=lu(A)
L =
34
2.1. FACTORIZAREA UNEI MATRICE 35
1.0000 0 0 0 0
0.6667 1.0000 0 0 0
0.3333 0 1.0000 0 0
0.3333 0 -0.5000 1.0000 0
-0.3333 0 0.5000 -1.0000 1.0000
U =
P =
0 0 0 1 0
0 1 0 0 0
0 0 0 0 1
1 0 0 0 0
0 0 1 0 0
X=[6,6,1;3,6,1;2,1,1];
[Q,R]=qr(X)
Q =
R =
Verificarea
Q*R-X
ans =
1.0e-14 *
0 0.1776 0
0 0.0888 0.0222
0.0444 0.0444 0
• x = A \b
• x = A −1 b
Expresia A −1 se poate introduce tastând Aˆ(-1).
• x = inv(A) ∗ b
• x = linsolve(A, b)
x1 + 2x 2 + 3x 3 + 4x 4 11
=
2x 1 + 3x 2 + 4x 3 + x4 = 12
3x 1
+ 4x 2 + x3 + 2x 4 = 13
4x 1 + x2 + 2x 3 + 3x 4 = 14
A = [1,2,3,4;2,3,4,1;3,4,1,2;4,1,2,3];
b = [11;12;13;14];
det(A)
ans=
160
x=inv(A)*b
x =
2.0000
1.0000
1.0000
1.0000
Rezolvarea este
38 CAPITOLUL 2. ALGEBRĂ LINIARĂ NUMERICĂ
A=[2,-1,3;1,1,1;3,-3,5];
b=[7;4;8];
x=pinv(A)*b
x =
1.4872
1.2949
1.5513
Capitolul 3
Cap. 2 Cap. 4
39
40 CAPITOLUL 3. REZOLVAREA SISTEMELOR ŞI ECUAŢIILOR ALGEBRICE
1. definirea funcţei f ;
function q=fct(p)
x=p(1);
y=p(2);
z=p(3);
q(1)=10*x+x.^2-2*y.*z-0.1;
q(2)=10*y-y.^2+3*x.*z+0.2;
q(3)=10*z+z.^2+2*x.*y-0.3;
2. Rezolvarea propriu-zisă
3.2. REZOLVAREA ECUAŢIILOR ALGEBRICE 41
p0=[0,0,0];
[p,fval,exitflag]=fsolve(@fct,p0)
Equation solved.
p =
fval =
1.0e-07 *
f=@(x)2.^x-x.^2
f =
@(x)2.^x-x.^2
[x,fval,exitflag]=fsolve(f,1)
42 CAPITOLUL 3. REZOLVAREA SISTEMELOR ŞI ECUAŢIILOR ALGEBRICE
Equation solved.
x =
fval =
exitflag =
1
Dacă x0 = 5 atunci x = 4, iar pentru x0 = −1 găsim x = −0.7667.
Graficul funcţiei f este reprezentat în Fig. 3.1.
Utilizarea funcţiei fzero este identică cu cea a funcţiei fsolve.
x=roots(p)
3.3. REZOLVAREA ECUAŢIILOR POLINOMIALE 43
Fig. 3.1: y = 2x − x 2
c=[1,2,3,2,1];
x=roots(p)
x =
1.0156 + 1.4152i
1.0156 - 1.4152i
Capitolul 4
Rezolvarea problemelor de
interpolare
Cap. 3 Cap. 5
Valoarea acestui polinom este calculat de funcţia Matlab prin formula baricen-
trică
1 function f =lagrange ( xd , x , y )
44
4.1. INTERPOLARE POLINOMIALĂ 45
2 [mx, nx]= s i z e ( x ) ;
3 [my, ny]= s i z e ( y ) ;
4 i e r r o r =0;
5 i f ( nx~=ny ) | ( mx~ = 1 ) | (my~=1) ,
6 i e r r o r =1;
7 disp ( ’ data dimension e r r o r ’ )
8 abort
9 end
10 xx=sort ( x ) ;
11 for k =1: nx−1,
12 i f xx ( k)== xx ( k +1) ,
13 i e r r o r =1;
14 break ,
15 end
16 end
17 i f i e r r o r ~=0 ,
18 disp ( ’ data e r r o r ’ )
19 abort
20 end
21 [m, n]= s i z e ( xd ) ;
22 f =zeros (m, n ) ;
23 p=zeros (m, n ) ;
24 q=zeros (m, n ) ;
25 w=ones ( 1 , nx ) ;
26 for i =1:nx ,
27 for j =1:nx ,
28 i f i ~=j ,
29 w( i )=w( i ) * ( x ( i )−x ( j ) ) ;
30 end
31 end
32 end
33 for i =1:m,
34 for j =1:n ,
35 u=find ( x==xd ( i , j ) ) ;
36 i f ~isempty (u ) ,
37 f ( i , j )= y (u ) ;
38 el se
39 for k =1: nx ,
40 p( i , j )=p( i , j )+ y ( k ) / ( xd ( i , j )−x ( k ) ) /w( k ) ;
41 q ( i , j )=q ( i , j ) + 1 / ( xd ( i , j )−x ( k ) ) /w( k ) ;
42 end
43 f ( i , j )=p( i , j ) / q ( i , j ) ;
44 end
45 end
46 end
fct=@(x)x.^2
x=0:5;
y=fct(x);
z=[0.5,3.5,10];
f=lagrange(z,x,y)
f =
Exemple de utilizare
x=[-2,-1,0,1,2];
y=[4,1,0,1,4];
polyLagrange(x,y)
ans =
s^2
z=[2,1,0,1,2];
polyLagrange(x,z)
ans =
- s^4/6 + (7*s^2)/6
syms s x
subs(z,s,x)
ans =
- x^4/6 + (7*x^2)/6
s = spline(x, y, z)
• s aproximaţiile obţnute.
48 CAPITOLUL 4. REZOLVAREA PROBLEMELOR DE INTERPOLARE
Exemplul 4.2 Să se calculeze valorile funcţiei spline cubice de interpolare şi ale
derivatelor sale de ordin 1, 2, 3 pentru datele de interpolare
f (x) = x 3
xi = i , i ∈ {0, 1, . . . , 5}
z = 0.5, 3, 5, 10
f=@(x)x.^3
f =
@(x)x.^3
>> x=0:5;
>> y=f(x);
>> z=[0.5,3.5,10];
>> s=spline(x,y,z)
s =
Derivare numerică
Cap. 4 Cap. 6
d y = gradient(y, h)
[xx,yy]=meshgrid(x,y)
z=f(xx,yy)
[dzx,dzy]=gradient(f,hx,hy)
49
50 CAPITOLUL 5. DERIVARE NUMERICĂ
Elementul z i , j este f (x j , y i ).
f=@(x)x.^3;
x=-1:0.2:1;
y=f(x);
dy=gradient(y,0.2)
dy =
Columns 1 through 5
Columns 6 through 10
Column 11
2.4400
Observaţie 5.1
x =
y=0:0.1:0.4
y =
[xx,yy]=meshgrid(x,y)
xx =
yy =
0 0 0 0 0
0.1000 0.1000 0.1000 0.1000 0.1000
0.2000 0.2000 0.2000 0.2000 0.2000
0.3000 0.3000 0.3000 0.3000 0.3000
0.4000 0.4000 0.4000 0.4000 0.4000
>> z=f(xx,yy)
z =
0 0 0 0 0
52 CAPITOLUL 5. DERIVARE NUMERICĂ
[dzx,dzy]=gradient(z,0.2,0.1)
dzx =
0 0 0 0 0
0.0280 0.0160 0.0040 0.0160 0.0280
0.0560 0.0320 0.0080 0.0320 0.0560
0.0840 0.0480 0.0120 0.0480 0.0840
0.1120 0.0640 0.0160 0.0640 0.1120
dxy =
i ∈ {1, 2, . . . , n − 1} j ∈ {1, 2, . . . , n − 1}
f (x i +1 ,y j )− f (x i −1 ,y j )) f (x i ,y j +1 )− f (x i ,y j −1 ))
d zx(i , j ) = 2hx , d z y(i , j ) = 2h y ,
i =n j =n
f (x n ,y j )− f (x n−1 ,y j )) f (x i ,y n )− f (x i ,y n−1 ))
d zx(n, j ) = hx
, d z y(i , n) = hy
,
Aceste formule nu ţin seama de faptul că în Matlab indicele iniţial este 1.
Capitolul 6
Cap. 5 Cap. 7
coef =lq(m,x,y)
unde
54
55
Exemplul 6.1 Să se calculeze polinoamele de aproximare de grad unu şi doi, con-
stituite prin metoda celor mai mici pătrate, pentru datele (x i , y i )0≤i ≤20 unde x i =
−2 + 0.2i , y i = f (x i ), f (x) = |x|. Să se reprezinte grafic funcţiile astfel obţinute.
x=-2:0.2:2;
y=abs(x);
c1=lq(1,x,y)
c1 =
1.0476
0.0000
>> c2=lq(2,x,y)
c2 =
0.3884
0.0000
0.4495
>> c3=lq(3,x,y)
c3 =
0.3884
56 CAPITOLUL 6. METODA CELOR MAI MICI PĂTRATE
0.0000
0.4495
0.0000
Reprezentarea grafică a celor trei aproximaţii
p1=@(x)c1(1)+x*c1(2);
p2=@(x)c2(1)+x*c2(2)+c2(3)*x.^2;
p3=@(x)c3(1)+x*c3(2)+c3(3)*x.^2+c3(4)*x.^3;
t=linspace(-2,2,100);
z=abs(t);
y1=p1(t);
y2=p2(t);
y3=p3(t);
plot(t,z,t,y1,t,y2,t,y3)
este dată în Fig. 6.1.
Fig. 6.1: Aproximarea funcţiei |x| prin polinome de grad 1,2 şi 3.
unde
f=fittype(‘sablon’)
f=fittype(’a+b*x+c*x.^2’)
f =
General model:
f(a,b,c,x) = a+b*x+c*x.^2
rezfit=fit(x,y,f,’StartPoint’,[1,1,1])
rezfit =
General model:
fit1(x) = a+b*x+c*x.^2
Coefficients (with 95% confidence bounds):
a = 0.3884 (0.2735, 0.5033)
b = 7.282e-09 (-0.06313, 0.06313)
c = 0.4495 (0.391, 0.508)
Cazul neliniar.
Pentru determinarea funcţiei de aproximare y = ϕ(t , x 1 , . . . , x n ) care minimizează
expresia
m
Φ(x 1 , . . . , x n ) = [ϕ(t i , x 1 , . . . , x n ) − y i ]2
X
(6.1)
i =1
t=0:5;
y=2*exp(-t);
Rezolvarea este:
f=fittype(’a*exp(b*x)’)
f =
General model:
f(a,b,x) = a*exp(b*x)
x=(0:5)’;
y=2*exp(-x);
rezfit=fit(x,y,f,’StartPoint’,[1,1])
rezfit =
General model:
rezfit(x) = a*exp(b*x)
Coefficients (with 95% confidence bounds):
a = 2 (2, 2)
b = -1 (-1, -1)
Capitolul 7
Integrare numerică
Cap. 6 Cap. 8
unde f : [a, b] → R este o funcţie continuă, Matlabf=@ oferă mai multe programe:
I = quad( f , a, b)
I = integral( f , a, b)
unde:
59
60 CAPITOLUL 7. INTEGRARE NUMERICĂ
Rezolvările sunt:
1.
f=@(x)16*x.^15;
quad(f,0,1)
ans =
1.0000
2.
function y=fct(x)
if x==0
y=1;
else
y=sin(x)./x;
end
quad(@fct,0,pi/2)
ans =
1.3708
7.1. INTEGRAREA FUNCŢIILOR DE O VARIABILĂ REALĂ 61
f=@(x)16*x.^15;
integral(f,0,1)
ans =
2.
integral(@fct,0,pi/2)
ans =
1.3708
I = trapz(x, y),
unde x = (x i )i , y = (y i )i .
f=@(x)16*x.^15;
x=0:10^(-6):1;
y=f(x);
trapz(x,y)
ans =
1.0000
62 CAPITOLUL 7. INTEGRARE NUMERICĂ
Integrala ZZ
f c t (x, y)dxdy (7.2)
D
se calculează în Matlab cu funcţia
I = integral2( f c t , a, b, yi n f , y sup)
unde parametrii corespund variabilelor introduse în (7.1) şi (7.2). yi n f , y sup pot
fi funcţii Matlab.
RR
Exemplul 7.3 Să se calculeze D x ydxdy unde domeniul D este delimitat de curbele
p
y = x 2 şi y = x.
Rezolvarea este
fct=@(x,y)x.*y;
yinf=@(x)x.^2;
ysup=@(x)sqrt(x);
integral2(fct,0,1,yinf,ysup)
ans =
0.0833
Capitolul 8
Cap. 7 Cap. 9
63
64 CAPITOLUL 8. INTEGRAREA NUMERICĂ A E.D.O.
• y 0 condiţia iniţială;
• y este
matricea cu soluţiile calculate în punctele vectorului x,
y 1 (x 1 ) y 1 (x 2 ) . . . y 1 (x m )
y 2 (x 1 ) y 2 (x 2 ) . . . y 2 (x m )
y =
... ... ... ...
y n (x 1 ) y n (x 2 ) . . . y n (x m )
Rezolvarea este
odefun=@(x,y)y;
[x,y]=ode45(odefun,[0,1],1);
plot(x,y)
function deri=odefun(x,y)
deri=zeros(2,1);
deri(1)=y(1).*y(2);
deri(2)=-1/(x.*y(1));
rezolvarea este
8.2. REZOLVAREA PROBLEMELOR BILOCALE 65
tspan=[1,2];
y0=[1;1];
[x,y]=ode45(@odefun,tspan,y0);
plot(x,y(:,1),x,y(:,2))
Graficul soluţiei numerice este dat in Fig. 8.2.
unde
g=@(x)[expr_1,expr_2,\ldots]
x_retea=linspace(a,b,n)
solinit=bvpinit(x_retea,g)
y 00 + y = 0,
y(0) = 0, y( π2 ) = 2.
8.2. REZOLVAREA PROBLEMELOR BILOCALE 67
y 10 = y 2
y 20 = −y 1
function deri=bvpfun(x,y)
deri=zeros(2,1);
deri(1)=y(2);
deri(2)=-y(1);
function res=bcfun(ya,yb)
res=zeros(2,1);
res(1)=ya(1);
res(2)=yb(1)-2;
Rezolvarea este
g=@(x)[sin(x);cos(x)];
n=5;
x_retea=linspace(0,pi/2,n);
solinit=bvpinit(x_retea,g)
solinit =
solver: ’bvpinit’
x: [0 0.3927 0.7854 1.1781 1.5708]
y: [2x5 double]
yinit: @(x)[sin(x);cos(x)]
sol=bvp4c(@bvpfun,@bcfun,solinit)
solver: ’bvp4c’
x: [1x7 double]
y: [2x7 double]
yp: [2x7 double]
stats: [1x1 struct]
68 CAPITOLUL 8. INTEGRAREA NUMERICĂ A E.D.O.
[sol.x’,sol.y’]
ans =
0 0 2.0000
0.1963 0.3902 1.9616
0.3927 0.7654 1.8478
0.7854 1.4142 1.4142
1.1781 1.8477 0.7654
1.3744 1.9616 0.3902
1.5708 2.0000 0.0001
Cap. 8 ??
şi respectiv
1 n−1
x = Fn−1 (y) yj wk j
X
xk = k ∈ {0, . . . , n − 1}
n j =0
2i π
unde x, y ∈ Cn iar w = e n . Prin Cn s-a notat mulţimea şirurilor de numere com-
plexe, periodice cu perioada n, Cn = {x = (x k )k∈Z : x k+n = x k , x k ∈ C, ∀k ∈ Z}.
În acest scop există funcţiile Matlab
• y=fft(x)
Calculul transformării discrete Fourier.
• x=ifft(y)
Calculul transformării discrete Fourier inverse.
unde semnificaţia parametrilor este x, y sunt şiruri de numere complexe reprezen-
tând originalul şi imaginea transformării.
69
70 CAPITOLUL 9. TRANFORMATA FOURIER DISCRETĂ
ak − i bk 1
Z 2π
ck = = f (x)e −i kx dx
2 2π 0
f=@(x)sin(x)+cos(3*x);
n=16;
h=2*pi/n;
x=0:h:2*pi-h;
ff=f(x);
c=fft(ff)/n;
a=c+conj(c);
b=(conj(c)-c)/i;
[a’,b’]
ans =
-0.0000 0
-0.0000 1.0000
-0.0000 0.0000
1.0000 0.0000
0.0000 0.0000
0.0000 0.0000
0.0000 0.0000
0.0000 -0.0000
0.0000 0
0.0000 0.0000
0.0000 -0.0000
0.0000 -0.0000
0.0000 -0.0000
1.0000 -0.0000
9.1. CALCULUL COEFICIENŢILOR FOURIER 71
-0.0000 -0.0000
-0.0000 -1.0000
Bibliografie
[1] BLAGA P., COMAN GH., POP S., TRÂMBIŢAŞ R., VASARU D., 1994, Anal-
iză numerică. Îndrumar de laborator. Univ. "Babeş–Bolyai" Cluj-Napoca
(litografiat).
[2] DINU M., LINCĂ Gh., 1999, Algoritmi şi teme speciale de analiză numerică.
Ed. Matrix rom, Bucureşti.
[3] GHINEA M., FIREŢEANU V., 1995, MATLAB calcul numeric, grafică, apli-
caţii. Rd. Teora, Bucureşti.
[4] MARINESCU GH., BADEA L., GRIGORE G., JAMBOR C., MAZILU P., RIZ-
ZOLI I., ŞTEFAN C., 1978, Probleme de analiză numerică. E.D.P., Bucureşti.
[5] MARINESCU GH., RIZZOLI I., POPESCU I., ŞTEFAN C., 1987, Probleme de
analiză numerică rezolvate cu calculatorul. Ed. Acad. R.S.R., Bucureşti.
[7] SCHEIBER E., LIXĂNDROIU D., CISMAŞIU C., 1982, Analiză numerică. În-
drumar de laborator. Univ. Braşov (litografiat).
[8] SCHEIBER E., SÂNGEORZAN L., GROVU M., 1993, Analiză numerică. În-
drumar de laborator. Univ. "Transilvania" Braşov (litografiat).
72