Lab Num Scilab
Lab Num Scilab
SCHEIBER
SCILAB
Braşov
Tema de LABORATOR
Fiecare temă primeşte o notă iar media notelor reprezintă 50% din nota de exa-
men.
1. Titlul capitolului;
3. Rezultatele obţinute.
2
3
1
Restul împărţirii.
Tema de programare
1. Datele de identificare.
4
5
1. Formula de calcul.
x0 ∈ R
f (x k )
x k+1 = x k − , k ∈N
f 0 (x k )
2. Problema de test.
Să se rezolve ecuaţie 2x − x 2 = 0, x0 = 1
S-au calculat x 1 = 2.6294457, x 2 = 1.8807153.
4. Textele sursă.
Proiectul este alcătuit din 3 programe Scilab:
I Scilab 10
5 Derivare numerică 48
5.1 Derivarea funcţiilor de o variabilă reală . . . . . . . . . . . . . . . . . 48
5.2 Cazul funcţiilor de mai multe variabile . . . . . . . . . . . . . . . . . 49
8
CUPRINS 9
7 Integrare numerică 57
7.1 Integrarea funcţiilor de o variabilă reală . . . . . . . . . . . . . . . . . 57
7.2 Calculul numeric al integralelor duble . . . . . . . . . . . . . . . . . . 59
II CULEGERE DE PROBLEME 63
8 Metode numerice în algebra liniară 64
10 Probleme de interpolare 72
11 Derivare numerică 75
13 Integrare numerică 78
Bibliografie 80
Part I
Scilab
10
Capitolul 1
Cap. 2
• Constante
11
12 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN SCILAB
%inf / %inf
Nan
• Literali
* Primul caracter al unui identificator este literă sau unul din car-
acterele %, , #, !, $, ?
* Următoarele caractere pot fi litere, cifre sau unul din caracterele
, #, !, $, ?
* Se face distincţie între literele mari şi mici.
* Numărul maxim de caractere este 24.
– Literali numerici
Exemple:
1. Numărul real 10,23 se poate scrie:
10.23=0.1023e+2=0.1023E+2=1023e-2=1023E-2.
2. Numărul complex 1 + 5i se scrie 1 + 5 ∗ %i .
– Literali nenumerici
Un caracter sau un şir se caractere – string – se defineşte (scrie):
Funcţii de mediu:
• Ş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
14 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN SCILAB
b=1:-0.3:0
1. 0.7 0.4 0.1
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
a=i^2
1 4 9 16
b=[1,4,9,16]
a==b
T T T T
• Matrice
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
1.1. OBIECTE SCILAB 15
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 !
unde a l i ,ci = v i . ∀i .
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.
variabilaMatrice(numărLinie, numărColoană)
– Operatori matriceali.
16 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN SCILAB
Simbol Semnificaţie
+ adunare
- scădere
∗ înmulţire de matrice
.∗ înmulţire pe componente
ˆ ridicare la putere prin produs matriceal
.ˆ ridicarea la putere a componentelor
\ a\b = a −1 · b
bi , j
.\ a.\b = ( a )i , j
i,j
/ b/a = b · a −1
bi , j
./ b/a = ( a )i , j
i,j
’ transpunere
– 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
size(A) Şir cu dimensiunile lui A
length(A) Numărul elementelor lui A
max(A) Cel mai mare element al lui A
min(A) Cel mai mic element al lui A
matrix(A, m, n) Transformă matricea A într-o matrice cu m
linii şi n coloane
– Extinderea unei matrice. Date fiind o matrice a şi un vector linie v
* adăugarea liniei v ca prima linie a matricei a se obţine prin
[v; a]
a(l 1 : l 2 , c 1 : c 2 )
*
a([l 1 , l 2 , . . . , l p ], [c 1 , c 2 , . . . , c q ])
X=poly(0,’X’);
p=X^2+3*X+2
18 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN SCILAB
v=[1,2,3];
p=poly(v,’X’)
p=
-6 + 11X - 6X^2 +X^3
q=poly(v,’X’,’coeff’)
q=
1 + 2X + 3X^2
X=poly(0,’X’)
p=(X-1)*(X-2)^2
q=(X-1)*(X-2)*(X-3)
[c,U]=gcd([p.q]);
c
2-3X+X^2
[p,q]*U
0 2-3X+X^2
[d,V]=lcm([p,q]);
d
12-28X+23X^2-8X^3+X^4
[p,q].*V-d*ones([p,q])
0 0
list(e 1 , e 2 , . . . , e n )
l=list(1,%t,’abc’,[1,2;3,4])
l=
l(1)
1
l(2)
T
l(3)
abc
l(4)
! 1. 2. 3. 4. !
string
typeof(exp)
fptr
typeof(1:5)
constant
typeof(%t)
boolean
p=poly(0,’X’)
typeof(p)
polynomial
l=list(1,’c’)
typeof(l)
list
deff(’y=f(x)’,’y=x.^2’)
typeof(f)
function
l=[1,1];v=1;m=sparse(l,v)
typeof(m)
sparse
• Instrucţiunea de atribuire
variabila=expresie
• Instrucţiuni condiţionate
1.2. ELEMENTE DE PROGRAMARE IN SCILAB 21
1.
½ ¾
,
if condiţie
then
instrucţiuni ½ ¾
,
elseif condiţie
then
instrucţiuni
else
instrucţiuni
end
Separatorii , sau then sunt obligatorii doar în cazul în care o instrucţiune
se scrie pe aceaşi linie cu if.
a=3;b=2;delta=a^2-4*b;
if delta>=0 then x=(-a+sqrt(delta))/2;y=(-a-sqrt(delta))/2;
else x=-a/2;y=sqrt(-delta)/2;end
2.
select expresie
case expr esi e 1 then instrucţiuni
..
.
else
instrucţiuni
end
case şi then trebuie scrise pe aceeaşi linie.
• Instrucţiuni de ciclare
1.
½ ¾
,
for n = n1 : pas : n2 instrucţiuni end
do
Dacă pas = 1 atunci acest parametru este opţional.
Separatorii , sau do sunt obligatorii doar în cazul în care o instrucţiune se
scrie pe aceaşi linie cu for.
P5
Exemplul 1.2 i =1 i
22 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN SCILAB
s=0;
for i=1:5, s=s+i; end
2.
,
while condiţie do instrucţiuni end
then
while şi do sau then trebuie scrise pe aceaşi linie.
Separatorii , then sau do sunt obligatorii doar în cazul în care o instrucţiune
se scrie pe aceaşi linie cu while.
P5
Exemplul 1.5 i =1 i
s=0;
i=0;
while i<5, i=i+1;s=s+i; end
deff(’[u,v]=f(x,y)’,[’u=x+y’,’v=x^2+y^2’])
save(’cale\numeFişier.bin’,’ f ’)
Funcţia save salvează orice tip de obiect. Dacă a, b sunt variabile Scilab
atunci sintaxa utilizată va fi save(’cale\numeFişier.bin’,0 a 0 ,0 b 0 ).
Reîncărcarea se realizează prin
load(’cale\numeFişier.bin’)
function [y 1 , . . . , y n ]= f (x 1 , . . . , x m )
instructiuni Scilab
endfunction
exec(’cale\numeF i si er .sci’,-1)
function [u,v]=f(x,y)
u=x+y;
v=x^2+y^2;
endfunction
genlib(’numeBibliotecă’,’cale’)
Prin această operaţie are loc compilarea funcţiilor şi crearea unui fişier
names cu cuprinsul bibliotecii.
O bibliotecă odată creată, resursele ei pot fi utilizate după reîncărcarea ei
prin
lib(’cale’)
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
endfunction
1.3. FUNCŢII(SUBPROGRAME) ÎN SCILAB 25
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
function t=fib(n)
t=[1,1];
for i=3:n
t(i)=t(i-1)+t(i-2)
end
endfunction
p
Exemplul 1.9 Funcţie pentru calculul limitei şirului (a n )n∈N definit prin a 0 = 2, a n+1 =
p
a n + 2.
function [l,error]=lim(tol,nmi)
v=sqrt(2)
cont=%t
ni=1
while(cont)
ni=ni+1
l=sqrt(v+2)
d=abs(v-l)
v=l
26 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN SCILAB
if(d<tol)&(ni>nmi)
cont=%f
end
end
if(d<tol)
error=0
else
error=1
end
endfunction
Exemplul 1.10 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 then
if length(varargin)==2 then
l=varargin(1)
r=varargin(2)
else
error(’Wrong number of input parameters’)
end
end
k=0:n
x=l+0.5*(r-l)*(cos(k*%pi/n)+1)
endfunction
• csvWrite(M,filename, separator )
1.5. GRAFICĂ ÎN SCILAB 27
• csvWrite(M,filename)
M este matricea care se salvează iar separatorul implicit este virgula (csv -
Comma separated values).
• M =csvRead(filename)
• M =csvRead(filename, separator )
plot2d(x, y)
unde
x 1,1 . . . x 1,n y 1,1 . . . y 1,n
x = ... ... ... y = ... ... ...
x m,1 . . . x m,n y m,1 . . . y m,n
sunt două matrice de acelaşi tip. Funcţia construieşte în acelaşi panou (fer-
eastră grafică) graficele a n curbe ce trec respectiv prin punctele
fplot2d(x, f )
unde
paramfplot2d( f , x, t )
unde
t=-%pi:0.1:%pi;
x=[t’,t’];
y=[sin(t’),cos(t’)];
plot2d(x,y)
1.5.2 Grafică 3D
• plot3d(X,Y,Z)
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 ).
Poziţia observatorului este indicată prin parametrii t het a = l ong i t ud i ne, al pha =
90 − l at i t ud i ne. Valorile implicite sunt t het a = 35, al pha = 45.
Dacă suprafaţa este definită prin z = f (x, y), x ∈ [a, b], y ∈ [c, d ] atunci se
defineşte funcţia Scilab
function [x,y,z]=suprafata(m,n)
u=linspace(a,b,m);
v=linspace(c,d,n);
for i=1:m
for j=1:n
x(i,j)=. . .
1.5. GRAFICĂ ÎN SCILAB 29
y(i,j)=. . .
end
end
z=f(x,y);
endfunction
exec(’. . .\suprafata.sce’,-1)
[x,y,z]=suprafata(m,n);
plot3d(x,y,z)
[x,y]=meshgrid(a:dx:b,c:dy:d);
z=f(x,y);
mesh(x,y,z)
30 CAPITOLUL 1. ELEMENTE DE PROGRAMARE ÎN SCILAB
• fplot3d(X,Y,f)
Exerciţii
1. Să se reprezinte grafic funcţiile în intervalele indicate
p
a. |x| x ∈ [−4, 4]
−x 2
b. e x ∈ [−4, 4]
sin x−3
c. 4x 2+x x ∈ [0, 4]
p2
d. x − x 2 − 1 x ∈ [−4, 4]
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)
P =
32
2.1. FACTORIZAREA UNEI MATRICE 33
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.
U =
3. 6. 2. 10. 7.
0. 0. - 3.3333333 - 1.6666667 - 3.6666667
0. 0. 3.3333333 - 3.3333333 1.6666667
0. 0. 0. - 2. 0.5
0. 0. 0. 0. - 2.
L =
1. 0. 0. 0. 0.
0.6666667 1. 0. 0. 0.
0.3333333 0. 1. 0. 0.
0.3333333 0. - 0.5 1. 0.
- 0.3333333 0. 0.5 - 1. 1.
X=[6,6,1;3,6,1;2,1,1];
[Q,R]=qr(X)
R =
7. 8. 1.5714286
0. 3. 0.1428571
0. 0. 0.7142857
Q =
Q*R-X
0. 8.882D-16 4.441D-16
- 4.441D-16 - 5.329D-15 - 1.554D-15
0. - 3.109D-15 - 8.882D-16
2. se calculează x = A −1 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
2.2. REZOLVAREA SISTEMELOR ALGEBRICE DE ECUAŢII LINIARE 35
x=
! 2 !
! 1 !
! 1 !
! 1 !
În general, pentru sistemul algebric de ecuaţii liniare Ax+b = 0, funcţia Scilab
[x,k]=linsolve(A,b) calculează
1. x – o soluţie particulară;
A=[1,1,1,1;2,-1,2,-1;1,2,-1,2;2,1,4,1;3,2,-2,2];
b=[2;1;-1;7;-5] ;
[x,k]=linsolve(A,-b)
k =
- 5.551D-17
36 CAPITOLUL 2. ALGEBRĂ LINIARĂ NUMERICĂ
0.7071068
2.776D-16
- 0.7071068
x =
- 1.
0.5
2.
0.5
Observaţie. k aproximează vectorul de normă euclidiană 1
0
p1
2
.
0
−1
p
2
Rezolvarea este
A=[2,-1,3;1,1,1;3,-3,5];
b=[7;4;8];
x=lsq(A,b)
x =
1.4871795
1.2948718
1.5512821
Funcţia linsolve( A ,−b ) returnează [ ].
Capitolul 3
Cap. 2 Cap. 4
37
38 CAPITOLUL 3. REZOLVAREA SISTEMELOR ŞI ECUAŢIILOR ALGEBRICE
deff(’q=f(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’])
p0=[0;0;0];
[p,q,info]=fsolve(p0,f)
3.2. REZOLVAREA ECUAŢIILOR ALGEBRICE 39
info=
1.
q=
1.0E-15*
! - .2636780 !
! .2775558 !
! .9436896 !
p=
! .0098702 !
! - .0200485 !
! .0299499 !
deff(’y=f(x)’,’y=2.^x-x.^2’)
[x,y,info]=fsolve(1,f)
info
1.
y=
0.
x=
2.
sau cu precizarea derivatei funcţiei f (x)
deff(’y=df(x)’,’y=(2.^ x).*log(x)-2*x’)
[x,y,info]=fsolve(1,f,df)
info=
1.
y=
40 CAPITOLUL 3. REZOLVAREA SISTEMELOR ŞI ECUAŢIILOR ALGEBRICE
0
x=
2.
Figure 3.1: y = 2x − x 2
x=roots(p)
unde
c=[1,2,3,2,1];
p=poly(c,’x’,’coeff’);
x=roots(p)
x=
! - .5 + .8660254i !
! - .5 - .8660254i !
! - .5 + .8660254i !
! - .5 - .8660254i !
Capitolul 4
Rezolvarea problemelor de
interpolare
Cap. 3 Cap. 5
1 function f =lagrange ( xd , x , y )
2 [mx, nx]= s i z e ( x ) ;
3 [my, ny]= s i z e ( y ) ;
42
4.1. INTERPOLARE POLINOMIALĂ 43
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= gs o rt ( 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
47 endfunction
f (x) = x 2
xi = i , i ∈ {0, 1, . . . , 5}
z = 0.5, 3, 5, 10
Rezolvarea este:
deff(’y=fct(x)’,’y=x.^2’)
exec(’. . .\lagrange.sci’,-1)
x=0:5;
y=fct(x);
z=[0.5,3.5,10];
f=lagrange(z,x,y)
! 0.25 12.25 100 !
deff(’y=fct(x)’,’y=x.^2’)
exec(’. . .\polyLagrange.sci’,-1)
x=0:5;
y=fct(x);
polyLagrange(x,y,’x’)
x2
1. d=splin(x,y);
sau
d=splin(x,y,[,spline_type[,der]])1
– not_a_knot - alegerea implicită: Dacă x 1 < x 2 < . . . < x n−1 < x n atunci
condiţiile la limită sunt
s (3) (x 2 − 0) = s (3) (x 2 + 0)
s (3) (x n−1 − 0) = s (3) (x n−1 + 0)
– clamped:
s 0 (x 1 ) = d er 1
s 0 (x n ) = d er 2
1
Parantezele pătrate nu fac parte din sintaxă. Ele arată caracterul opţional al elementelor
cuprinse între ele.
46 CAPITOLUL 4. REZOLVAREA PROBLEMELOR DE INTERPOLARE
– natural:
s 00 (x 1 ) = 0
s 00 (x n ) = 0
s 0 (x 1 ) = s 0 (x n )
s 00 (x 1 ) = s 00 (x n )
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
deff(’y=f(x)’,’y=x^3’)
x=0:5;
y=f(x);
z=[0.5,3.5,10];
d=splin(x,y);
[s,s1,s2,s3]=interp(z,x,y,d)
s3=
! 6. !
! 6. !
! 0. !
s2=
4.2. INTERPOLARE CU FUNCŢII SPLINE CUBICE 47
! 3. !
! 21. !
! 0. !
s1=
! 0.75 !
! 36.75 !
! 0. !
s=
! .125 42.875 0 !
Capitolul 5
Derivare numerică
Cap. 4 Cap. 6
g = numderivative( f , x)
g = numderivative( f , x, h)
g = numderivative( f , x, h, or d er )
[J , H ] = numderivative(. . .)
unde
48
5.2. CAZUL FUNCŢIILOR DE MAI MULTE VARIABILE 49
Rezolvarea este:
deff(’y=f(x)’,’y=x^3’)
numderivative(f,1)
ans =
3.0000001
[d1,d2]=numderivative(f,1)
d2 =
6
d1 =
3
2x y x 2
µ ¶
0
f (x, y) =
3x 2 1
µ ¶
00 2y 2x 2x 0
f (x, y) =
6x 0 0 0
Astfel rezultatele exacte sunt
µ ¶
0 4 1
f (1, 2) =
3 1
µ ¶
00 4 2 2 0
f (1, 2) =
6 0 0 0
Rezolvarea Scilab este
50 CAPITOLUL 5. DERIVARE NUMERICĂ
deff(’q=fct(p)’,[’x=p(1)’,’y=p(2)’,’q(1)=x.^2.*y’,’q(2)=x.^3+y’])
p=[1;2]
[J,H]=numderivative(fct,p)
H =
4. 2. 2. 0.
6. 0. 0. 0.
J =
4. 1.
3. 1.
Capitolul 6
Cap. 5 Cap. 7
coef =lq(m,x,y)
unde
51
52 CAPITOLUL 6. METODA CELOR MAI MICI PĂTRATE
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);
exec(’e:\scilab\lq.sci’,-1)
c1=lq(1,x,y)
c1=
1.047619
1.110D-16
c2=lq(2,x,y)
c2=
0.3883622
1.110D-16
0.4494933
//
// Reprezentarea grafica
//
deff(’y=p1(x)’,[’y=c1(1)+c1(2)*x’])
deff(’y=p2(x)’,[’y=c2(1)+c2(2)*x+c2(3)*x.*x’])
y1=p1(x);
y2=p2(x);
xx=[x’,x’,x’];
yy=[y’,y1’,y2’];
plot2d(xx,yy,[1,2,3],’121’,’abs@p1@p2’)
53
p 1 (x) = 1.047619
şi respectiv
p 2 (x) = 0.3883622 + 0.4494933x 2
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
unde
t=0:5;
y=2*exp(-t);
În fişierul fct.sce se definesc funcţiile f c t (x, t , y) şi f 1(t , x). Funcţia f 1 fix-
ează expresia funcţiei de aproximare ϕ(t , x 1 , . . . , x n ) iar funcţia f c t calculează
tabloul erorilor corespunzătoare datelor problemei.
Necunoscutele a, b devin componentele vectorului x (a = x 1 , b = x 2 ).
Fişierul fct.sce este
1 function u= f c t ( x , t , y )
2 u= f1 ( x , t )−y ;
3 endfunction
5 function y= f1 ( x , t )
6 y=x ( 1 ) * exp ( x ( 2 ) * t ) ;
7 endfunction
Rezolvarea este
exec(’. . .\fct.sce’,-1)
x=[2.5,-0.5];
[fopt,xopt,gopt]=leastsq(list(fct,t’,y’),x)
gopt =
55
0. 0.
xopt =
2. - 1.
fopt =
0.
∂f 1 ∂f 1 ∂f 1 ∂f 1
Dacă, în plus, se furnizează gradientul funcţiei f 1: d f 1 = ( ,
∂a ∂b
) = ( ∂x1 , ∂x2 ) =
(e bt , at e bt ) în fişierul fct1.sce, codul acestuia va fi
1 function u= f c t 1 ( x , t , y )
2 u= f1 ( x , t )−y ;
3 endfunction
5 function y= f1 ( x , t )
6 y=x ( 1 ) * exp ( x ( 2 ) * t ) ;
7 endfunction
9 function u=df1 ( x , t , y )
10 s=exp ( x ( 2 ) * t ) ;
11 u=[ s , x ( 1 ) . * t . * s ] ;
12 endfunction
atunci apelarea va fi
[fopt,xopt,gopt]=leastsq(list(fct1,t’,y’),df1,x)
unde
6 function y= f1 ( x , t )
7 y=x ( 1 ) * exp ( x ( 2 ) * t ) ;
8 endfunction
x=[2.5,-0.5];
exec(’. . .\fctlm.sce’,-1)
[xopt,v]=lsqrsolve(x,fctlm,6)
v =
0
0
0
0
0
0
xopt =
2. - 1.
Capitolul 7
Integrare numerică
Cap. 6 ??
57
58 CAPITOLUL 7. INTEGRARE NUMERICĂ
Rezolvările sunt:
1.
I=integrate(’16*x.^15’,’x’,0,1)
I=
1.
2.
deff(’y=f(x)’,’y=16*x.^15’)
[x,err]=intg(a,b,f)
err=
1.110 E-14
x=
1.
2.
intg(a,b,list( f , p 1 , p 2 , . . .))
calculează integrala
Z b
f (x, p 1 , p 2 , . . .)dx
a
integrala ZZ
f c t (x, y)dxdy
D
este calculată de programul [i nt eg , er ]=integr( f c t , a, b, f i n f , f sup, t ol ).
Semnificaţia parametrilor formali este:
• f c t funcţia de integrat;
• er – indicatorul de răspuns:
5. Datele problemei:
1 function [ f c t , a , b , f i n f , fsup , t o l ]= datas ( )
2 a=. . . ;
3 b= . . . ;
4 d e f f ( ’ z= f c t ( x , y ) ’ , ’ z = . . . ’ ) ;
5 d e f f ( ’ y= f i n f ( x ) ’ , ’ y = . . . ’ ) ;
6 d e f f ( ’ y=fsup ( x ) ’ , ’ y = . . . ’ ) ;
7 tol =. . . ;
8 endfunction
6. Apelarea aplicaţiei:
1 function a p l i c a t i a ( cale )
2 exec ( cale+ ’ \ datas . s c i ’ , −1)
3 exec ( cale+ ’ \ i n t e g r . s c i ’ , −1)
4 [ f c t , a , b , f i n f , fsup , t o l ]= datas ( ) ;
5 [ integ , er ]= i n t e g r ( f c t , a , b , f i n f , fsup , t o l ) ;
6 p r i n t f ( ’ error_code = %d\n ’ , er )
7 p r i n t f ( ’ computed_integral = %f \n ’ , integ )
8 endfunction
Codurile funcţiilor init, tab, integr2, prodscal sunt editate în fişierul integr.sci,
după codul funcţiei integr.
Utilizatorul trebuie să precizeze datele problemei în funcţia Scilab datas.
RR
Exemplul 7.2 Să se calculeze D x ydxdy unde domeniul D este delimitat de curbele
p
y = x 2 şi y = x.
exec(’c:\lucru\scilab\aplicatia.sci’,-1)
aplicatia(’c:\lucru\scilab’)
Rezultatele sunt
erro_code = 0
computed_integral = .0833333
Part II
CULEGERE DE PROBLEME
63
Capitolul 8
10 6 −2 1
10 10 −5 0
1.
−2 2 −2 1
1 3 −2 3
5 3 −11
2. 4 −5 4
3 −13 19
2 −1 3 4
4 −2 5 6
3.
6 −3 7 8
8 −4 9 10
1 1 0 0 0 0
1 2 1 0 0 0
0 1 3 1 0 0
4.
0 0 1 4 1 0
0 0 0 1 5 1
7
0 0 0 0 1 33
64
65
4 5 2
1. 3 0 3
0 4 6
2 1 3
2. 1 3 1
2 8 5
3 4 7 −2
5 4 9 3
3.
1 −1 0 3
1 −1 0 0
x1 − 2x 2 + 3x 3 + 4x 4 = 22
−4x 1 + x2 + 2x 3 + 3x 4 = 6
2.
3x 1 + 4x 2 − x3 + 2x 4 = −4
2x 1 + 3x 2 + 4x 3 − x4 = 6
6x 4y + z + 2t 3
+ =
6x + 5y + 3z + 5t = 6
3.
12x + 8y + z + 5t = 8
6x + 5y + 3z + 7t = 8
x + 2y + 3z 4t
+ = −4
x + y + 2z + 3t = −2
4.
x + 3y + z + 2t = −3
x + 3y + 3z + 2t = −5
2x + y + z + t 1
=
3x − 2y − 5z + 4t = −30
5.
x + 3y + 2z − 3t = 17
x − y + z − t = 2
66 CAPITOLUL 8. METODE NUMERICE ÎN ALGEBRA LINIARĂ
x + y + z + t 4
=
2x + 2z + t = 4
6.
−2x + 2y − t = 4
3x + y − z = 0
2x 1 + 3x 2 + 2x 3 + x4 2
=
x1 + x2 + 3x 3 + 2x 4 = 6
7.
−3x 1 + 2x 2 − x3 − x4 = 0
x1 − x2 + x3 + 3x 4 = 6
x y + z + t 2
+ =
2y + 2z + t = 2
8.
−2x + 2y − t = 2
3x + y − z = 2
x + y + 2z + 3t = 2
3x − y − 4z − 6t = 0
9.
2x + 3y − 6z − 9t = −7
x − 2y + 8z − 12t = 3
x + y z − 2t
+ + 7u = 10
2x + 5z − 2u = 32
10. 3x + y − t = 1
2y − 5z + u = −39
x − y + 3t = 7
2x + 3y − z + t = 5
x − y + 2z − 2t = −5
11.
3x + y + 2z − 2t = −3
−3x − y − 2z + 2t = 3
x + 2y + 3z + 4t = 30
2x − 3y + 5z − 2t = 3
12.
3x + 4y − 2z − t = 1
4x − y + 6z − 3t = 8
x + 2y + 3z − 2t = 6
2x − y − 2z − 3t = 8
13.
3x + 2y − z + 2t = 4
2x − 3y + 2z + t = −8
2x − y + z − t = 1
2x − y − 3t = 2
14.
3x − y + t = −3
2x + 2y − 2z + 5t = −6
67
x + 2y + 3z + 4t + 5u = 13
2x + y + 2z + 3t + 4u = 10
15. 2x + 2y + z + 2t + 3u = 11
2x 2y 2z t 2u = 6
+ + + +
2x + 2y + 2z + 2t + u = 3
x + 2y − 3z + 4t − u = −1
2x − y + 3z − 4t + 2u = 8
16. 3x + y − z + 2t − u = 3
4x + 3y + 4z + 2t + 2u = −2
x − y − z + 2t − 3u = −3
x + y + z + t 10
=
x + y − z + 2t = −8
17.
5x + 5y − z − 4t = −4
x + y + 3z + 4t = 28
2x − 3y + z
= −1
x + 2y − 3z = 0
3.
x − 12y + 11z = −1
4x − 15y + 9z = 0
5x + 3y − 11z = 13
4. 4x − 5y + 4z = 18
3x − 13y + 19z = 22
Capitolul 9
1. x 6 − 2x 5 + x 3 − 6x + 6 = 0
2. 2x 6 − x 5 − 5x 4 + 2x 3 + 20x 2 − 9x − 9 = 0
4. x 5 − x 4 − 2x 3 + 4x 2 − 4 = 0
5. x 5 − 4x 4 − 9x 3 + 18x 2 + 14x − 20 = 0
6. x 6 − 5x 5 + 5x 4 − 2x 3 + 13x 2 + 3x + 9 = 0
8. x 6 − 6x 5 + 8x 4 + 3x 3 − 16x 2 + 18x − 8 = 0
9. x 5 − 3x 4 + 2x 3 − x 2 + 5x − 2 = 0
10. x 5 − 4x 3 − 8x 2 + 32 = 0
11. x n − nx + 1 = 0, n = 3, 4, 5
12. x n − (1 + x + . . . + x n−1 ) = 0, n = 3, 4, 5
14. x n+1 − x n − 1 = 0, n = 2, 3, 4
68
69
1. 2x − ln x − 4 = 0
2. ln 8x − 9x + 3 = 0
3. x − 2 cos x = 0
4. e −0.5x − x = 0
1
5. ln x = x
6. ln x = x 2 − 1
7. x 2 − 2 cos x = 0
8. x ln x − 14 = 0
9. x x + 2x − 6 = 0
10. e x + e −3x − 4 = 0
11. ln 7x − 8x + 1 = 0
12. x + e x = e
2x 3 − y 2 − 1 = 0
½
1.
x y3 − y − 4 = 0
70 CAPITOLUL 9. SISTEME ŞI ECUAŢII ALGEBRICE
ex − y = 0
½
2.
x − e −y = 0
y
e −x = x 2 − y + 1
½
3.
(x + 0.5)2 + y 2 = 0.6
2 2 2
x +y +z =3
4. x y + xz − 3y z = 1
2
x + y 2 − z2 = 1
x2 + y 2 − 1 = 0
½
5.
x3 − y = 0
2x 2 − x y − 5x + 1 = 0
½
6.
x + 3 ln x − y 2 = 0
2x 2 − x y − y 2 + 2x − 2y + 6 = 0
½
7.
y −x −1 = 0
x3 − y 2 − 1 = 0
½
8.
x y3 − y − 4 = 0
2 2 2
x −x −y −y +z =0
9. y − ex = 0
z − ln(y) = 0
x2 − x − y 2 = 1
½
10.
y − ex = 0
x + 3 ln x − y 2 = 0
½
11.
2x 2 − x y − 5x + 1 = 0
2
x + x − 2y z − 0.1 = 0
12. y − y 2 + 3xz + 0.2 = 0
z − z 2 + 2x y − 0.3 = 0
x3 − y + 1 = 0
½
13.
0.25x 2 + y 2 − 1 = 0
x − cos(x − y − z) = 0
14. y − cos(y − x − z) = 0
z − cos(z − x − y) = 0
½
tan(x) − cos(1.5y) = 0
15.
2y 3 − x 2 − 4x − 3 = 0
71
3x 2 + 14y 2 − 1 = 0
½
16.
sin(3x + 0.1y) + x = 0
1 + x 2 − y 2 + e x cos y = 0
½
x 0 = −1
17.
2x y + e x sin y = 0 y0 = 4
sin x + cos y + e x y = 0
½
18.
arctan (x + y) − x y = 0
72
73
Capitolul 10
Probleme de interpolare
f (x) = x 4 − 10x 3 + 2x 2 + 3x + 1
2. x i = 1 + i , i ∈ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
z = 4.5
f (x) = lg(x)
3. x i = e i , i ∈ {0, 1, 2, 3, 4, 5}
z = 1.7
f (x) = e x
4. x i = −3 + i , i ∈ {0, 1, 2, 3, 4, 5, 6}
z = 1.5
2
f (x) = x x+1
8 x i = 1 + 5i , i ∈ {0, 1, 2, 3, 4, 5}
z = 1.3, z = 1.5, z = 1.7
II. Să se deducă expresia polinomului de interpolare pentru datele problemei
I.
III. Să se calculeze valoarea funcţiei spline cubice de interpolare pentru datele
problemei I.
Capitolul 11
Derivare numerică
1. f (x) = 2x − ln x − 4 x0 = 1
2. f (x) = ln 8x − 9x + 3 x0 = 1
3. f (x) = x − 2 cos x x0 = 1
4. f (x) = e −0.5x − x x0 = 1
5. f (x) = ln x − x1 x0 = 1
6. f (x) = ln x − x 2 + 1 x0 = 1
7. f (x) = x 2 − 2 cos x x0 = 1
8. f (x) = x x + 2x − 6 x0 = 2
9. f (x) = e x + e −3x − 4 x0 = 1
75
76 CAPITOLUL 11. DERIVARE NUMERICĂ
2x 3 − y 2 − 1
µ ¶
1. f (x, y) = (x, y) = (1, 2)
x y3 − y − 4
x2 − y
µ ¶
2. f (x, y) = (x, y) = (1, 2)
x(y + 1)
tan(x y) − x 2
µ ¶
3. f (x, y) = (x, y) = (1, 2)
0.5x 2 + 2y 2 − 1
y
e −x − x 2 − y − 1
µ ¶
4. f (x, y) = (x, y) = (2, 2)
(x + 0.5)2 + y 2 − 0.6
x 3 + y 3 − 6x + 3
µ ¶
5. f (x, y) = (x, y) = (1, 2)
x 3 − y 3 − 6y + 2
x2 + y 2 − 1
µ ¶
6. f (x, y) = (x, y) = (1, 2)
x3 − y
2x 2 − x y − 5x + 1
µ ¶
7. f (x, y) = (x, y) = (1, 2)
x + 3 ln x − y 2
2x 2 − x y − y 2 + 2x − 2y + 6
µ ¶
8. f (x, y) = (x, y) = (1, 2)
y −x −1
Capitolul 12
77
Capitolul 13
Integrare numerică
R1 Rπ
1. x 2 e x dx 2. x 2 cos xdx
0 0
R5 p R2
3. x 2 − 9dx 4. x 2 ln xdx
4 1
π π
R4 2
R2 sin 2x
5. x tan xdx 6. 1+sin2 x
dx
0 0
R2 p R1 2
7. 4x − x 2 dx 8 arcsin xx 2 +1
−1
dx
0 0
π
Rπ 2
R2 1+sin 2x+cos 2x
9. cos x cos 4xdx 10. sin x+cos x
dx
0 π
6
R1 p R1
p 2x dx
2
11. x3 1 + e x dx 12.
−1 0 3+4x 2
p π
R3 R4 tan x
13. arctan x1 dx 14. p dx
p 1+ 2 cos x
3 0
3
78
79
x2
D : y = x1 ; y = x; x = 1; x = 2.
RR
2. y2
dxdy
D
x
D : y = x; y = (x − 1)2 .
RR
3. x 2 +y 2
dxdy
D
RR p
4. 4x 2 − y 2 dxdy D : y = x; y = 0; x = 1.
D
p 1 p dxdy D : x = 1, y = 0, x − y = 12 .
RR
5. x+ y
D
2x
RR
6. p dxdy D : y = x, x = 0, y = 1.
D 1+y 4 −x 4
(x 2 y 1 − x 2 − y 2 dxdy D : x ≥ 0, y ≥ 0, x 2 + y 2 = 1.
RR p
7.
D
RR p
8. x y − y 2 dxdy D este triunghiul cu vârfurile
D
O(0, 0), A(10, 1), B (1, 1).
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] CIRA O., MĂRUŞTER Ş., 2008, Metode numerice pentru ecuaţii neliniare.
Ed. Matrix-Rom, Bucureşti.
[3] CURTEANU S., 2001, Calculul numeric şi simbolic în Mathcad. Ed. Matrix-
Rom, Bucureşti.
[4] DINU M., LINCĂ Gh., 1999, Algoritmi şi teme speciale de analiză numerică.
Ed. Matrix rom, Bucureşti.
[5] GAVRILĂ C., 2007, SCILAB Aplicaţii, Modele şi simulare Scicos Ed. Matrix-
Rom, Bucureşti.
[6] KINCAID D., CHENEY W., 1991, Numerical Analysis. Brooks / Cole, Pacific
Grove, Ca.
[7] 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.
[8] MARINESCU GH., RIZZOLI I., POPESCU I., ŞTEFAN C., 1987, Probleme de
analiză numerică rezolvate cu calculatorul. Ed. Acad. R.S.R., Bucureşti.
[9] MARTIN O., 1998, Probleme de analiză numerică. Ed. Matrix rom, Bu-
cureşti.
[11] SCHEIBER E., LIXĂNDROIU D., CISMAŞIU C., 1982, Analiză numerică. În-
drumar de laborator. Univ. Braşov (litografiat).
80
BIBLIOGRAFIE 81
[12] SCHEIBER E., SÂNGEORZAN L., GROVU M., 1993, Analiză numerică. În-
drumar de laborator. Univ. "Transilvania" Braşov (litografiat).
[13] SCHEIBER E., 2010, Java în calculul ştiinţific. Ed. Univ. Transilvania
Braşov.