You are on page 1of 60

Matlab Syllabus

16 mai 2004

Table des mati`


eres
Cours

1 backward col.m

2 cout.m

3 der an2.m

4 der an.m

5 diff.m

6 dividif.m

7 f306.m

8 f arret.m

9 fix2real.m

10 fixpt.m

11 flo2real.m

12 floset.m

13 fLV.m

14 fnewton.m

15 forward col.m

16 forward row.m

17 f test.m

18 ftest.m

19 f test pt.m

20 fzero root.m

`
TABLE DES MATIERES

Syllabus Matlab

21 incr.m

22 interp ls.m

23 invpower.m

24 kern2.m

10

25 kern.m

10

26 loga.m

10

27 ls pol.m

10

28 lu kji.m

11

29 mat square.m

11

30 model2.m

11

31 model.2m.m

11

32 model.m

12

33 phi.m

12

34 plot circle.m

12

35 plot fun.m

12

36 plot line2.m

13

37 plot line.m

13

38 pol lag.m

13

39 powerm.m

14

40 real2f.m

14

41 root iter.m

15

42 round2flo.m

17

43 s ab stababs.m

18

44 s arret.m

18

45 s assoc.m

19

46 s compr.m

19

47 s coniug2.m

20

48 s coniug.m

20

49 s cost.m

21

50 s dist flo.m

22

Page 2

Syllabus Matlab

`
TABLE DES MATIERES

51 s err arr.m

23

52 s errgen.m

23

53 s euler arr.m

24

54 s euler.m

24

55 s euler pc.m

25

56 s eul stababs.m

26

57 s exemple.m

26

58 s fillin.m

27

59 s fixpt.m

27

60 s floset.m

27

61 s gausei2.m

27

62 s gausei.m

28

63 s gers2.m

29

64 s gers.m

29

65 s grad al.m

30

66 s gradient.m

30

67 s half.m

31

68 s iter.m

31

69 s jacobi2.m

32

70 s jacobi.m

32

71 s lag.m

33

72 s least2.m

33

73 s least.m

34

74 s LotkaVolterra.m

34

75 s lu.m

34

76 s mil stababs.m

35

77 s nodal.m

36

78 sol an2.m

36

79 sol an.m

36

80 s pca.m

36

Page 3

`
TABLE DES MATIERES

Syllabus Matlab

81 spcub.m

37

82 s pol lag.m

37

83 s pol new2.m

38

84 s pol new.m

39

85 s power.m

39

86 spy lu kji.m

40

87 s rbf2.m

40

88 s rbf.m

41

89 s refine.m

42

90 s rk2.m

43

91 s rk2 stab.m

44

92 s rk4.m

44

93 s runge.m

45

94 s ru spl.m

46

95 s spline2.m

46

96 s spline.m

47

97 s stable.m

47

98 s taylor.m

47

99 s triang.m

48

100s unstable2.m

48

101s unstable.m

49

TP

49

102ex5.m

49

103ex6.m

50

104ex7.m

50

105ex8.m

50

106ex9.m

51

107ex10.m

51

108ex11.m

51

Page 4

`
TABLE DES MATIERES

Syllabus Matlab

109ex12.m

51

Autres

52

110gacol.m

52

111garow.m

52

112givcos.m

52

113gseid.m

52

114hessqr.m

53

115houshess.m

53

116jacobi.m

54

117prodgiv.m

54

118qrgivens.m

54

119secant.m

55

120vhouse.m

55

Page 5

Syllabus Matlab

Cours
1

backward col.m
Contenu du fichier backward col.m :

10

12

14

16

18

function [ b]= b a c k w a r d c o l (U, b )


[ n]= m a t s q uare (U ) ;
l = min( diag ( abs (U ) ) ) ;
T=t r i u (U ) ;
i f sum(sum(T = U)) >0
error ( Error : the matrix is not upper triagular ) ;
end
i f l == 0
disp ( Error : the matrix is singular ) ; b = [ ] ;
break
end
for j = n : 1:2 ,
b ( j )=b ( j ) /U( j , j ) ;
b ( 1 : j 1)=b ( 1 : j 1)b ( j ) U( 1 : j 1, j ) ;
end ;
b ( 1 ) = b ( 1 ) /U( 1 , 1 ) ;
return

cout.m
Contenu du fichier cout.m :

function f=c o u t ( n )
X=rand ( n , n ) ;
flops (0)
l u k j i (X ) ;
f=f l o p s ;

der an2.m
Contenu du fichier der an2.m :

function y=d e r a n ( t , y )
global lambda
y=lambda y ;

der an.m
Contenu du fichier der an.m :

function y=d e r a n ( t , y )
2

y=2ty ;

diff.m
Contenu du fichier diff.m :

Page 6

DIFF.M

Syllabus Matlab

FIX2REAL.M

close a l l
clear a l l
y (1)=1;
y (2)=1;
h=0.001;

f o r ( i =2:30000)
y ( i +1)=2hy ( i )+y ( i 1);
end

10

12

plot ( y )

dividif.m
Contenu du fichier dividif.m :

10

function [ d]= d i v i d i f ( x , y )
[ n ,m]= s i z e ( y ) ;
i f n == 1 , n = m; end
n = n1;
d = zeros ( n+1,n + 1 ) ;
d (: ,1) = y ;
f o r j = 2 : n+1
f o r i = j : n+1
d ( i , j ) = ( d ( i 1, j 1)d ( i , j 1))/( x ( i j +1)x ( i ) ) ;
end
end
return

f306.m
Contenu du fichier f306.m :

function f v=f 3 0 6 ( x ) ;
2

f v=xcos ( x ) ;

f arret.m
Contenu du fichier f arret.m :

function [ y , dy]= f a r r e t ( x )
e t a=1e 9;
y=exp(x) e t a ;
dy=exp(x ) ;

fix2real.m
Contenu du fichier fix2real.m :

function r=f i x 2 r e a l ( xn , xm, b , s )


% f i x 2 r e a l : from Fi x e d P o i n t n o t a t i o n t o r e a l n o t a t i o n
%
%
FIX2REAL(XN,XM, B, S )
%
XN [ 1 ,N ] v e c t o r o f d i g i t s b e f o r e t h e d e c i m a l p o i n t
%
XM [ 1 ,M] v e c t o r o f d i g i t s a f t e r t h e d e c i m a l p o i n t
%
B
[1 ,1] base

Page 7

Syllabus Matlab

10

10

FIXPT.M

%
S [ 1 , 1 ] signum : 0 f o r p o s i t i v e , 1 f o r n e g a t i v e
%
Returns R [ 1 , 1 ] r e a l nummber i n d e c i m a l f o rm a t
%
% N.B . xn ( 1 ) i s t h e most s i g n i f i c a n t d i g i t

12

14

16

18

20

22

24

26

28

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002
n=length ( xn ) ;
m=length (xm ) ;
r =0;
I=find ( [ xn xm ]>=b ) ;
i f length ( I )>0
error ( wrong input : no element of xn and xm should be larger than b ) ;
end
i f ( s =0 & s =1)
error ( wrong input s: should be 0 or 1 ) ;
end

30

36

f o r ( i =1:n )
r=r+xn ( i ) b ( ni ) ;
end
f o r ( i =1:m)
r=r+xm( i ) b( i ) ;
end

38

r=r ( 1) s ;

32

34

10

fixpt.m
Contenu du fichier fixpt.m :

function [ k , p , e r r , P]= f i x p t ( g , p0 , t o l , max1 )


2

10

12

14

16

18

P(1)= p0 ;
g r a p h i c =1;
i f ( graphic )
zoom on
hold o f f
d e l t a =2;
p l o t f u n ( g , p0d e l t a , p0+d e l t a , - ) ;
hold on
p l o t l i n e ( p0d e l t a , p0d e l t a , p0+d e l t a , p0+d e l t a , r- )
delta2 =0.2;
xlabel ( x ) ;
ylabel ( y(x) ) ;
pause
end

%
% p l o t y=x

f o r k=2:max1
P( k)= f e v a l ( g , P( k 1 ) ) ;

20

i f ( graphic )
22

24

plot (P( k 1) , f e v a l ( g , P( k 1)) , * )


axis ( [ min(P)abs ( d e l t a 2 min(P ) ) max(P)+abs ( d e l t a 2 max(P ) ) . . .

Page 8

Syllabus Matlab

FLOSET.M

min(P)abs ( d e l t a 2 min(P ) ) max(P)+abs ( d e l t a 2 max(P ) ) ] )


t i t l e ( [ Fixed point iteration Iteration = num2str ( k 2 ) ] )
pause
p l o t l i n e (P( k 1) ,P( k ) , P( k ) ,P( k ) , : )
p l o t l i n e (P( k ) , f e v a l ( g , P( k ) ) , P( k ) ,P( k ) , : )

26

28

end

30

e r r=abs (P( k)P( k 1 ) ) ;


r e l e r r=e r r / ( abs (P( k))+ eps ) ;
p=P( k ) ;
i f ( e r r <t o l ) | ( r e l e r r <t o l ) , break ; end

32

34

36

12

end
P=P ;

11

flo2real.m
Contenu du fichier flo2real.m :

10

function r=f l o 2 r e a l (m, e , b , s )


% f l o 2 r e a l : from F l o a t i n g P o i n t n o t a t i o n t o r e a l n o t a t i o n
%
%
FLO2REAL(M, E, B, S )
%
M [ 1 ,T ] m a n t i s s a v e c t o r o f d i g i t s
% E [1 ,1] exponent
%
B [1 ,1] base
%
S [ 1 , 1 ] signum : 0 f o r p o s i t i v e , 1 f o r n e g a t i v e
%
Returns R [ 1 , 1 ] r e a l nummber i n d e c i m a l f o rm a t
%
% N.B . m( 1 ) i s t h e most s i g n i f i c a n t d i g i t

12

14

16

18

20

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002
i f (sum( abs ( s+sum( abs (m))+ e ))==0)
r =0;
return ;
end

22

24

26

28

t=length (m) ;
r =(1) s ( b e ) ;
sm=0;
f o r ( i =1: t )
sm=sm+m( i ) b( i ) ;
end
r=r sm ;

12

floset.m
Contenu du fichier floset.m :

function [ F , c a r d ]= f l o s e t ( b , t , L ,U)
% f l o s e t : r e t u r n s t h e s e t o f r e a l numbers r e p r e s e n t e d by t h e f l o a t i n g p o i n t n o t a t i o n
%
%
FLOSET(B, T, L ,U)
%
%
b [1 ,1] base
%
t [ 1 , 1 ] number o f s i g n i f i c a n t d i g i t s

Page 9

Syllabus Matlab

10

12

%
%
%
%
%

14

L [ 1 , 1 ] l o w e r bound e x p o n e n t
U [ 1 , 1 ] upper bound e x p o n e n t
Returns :
F [ card , 1 ]
s e t o f r e a l numbers r e p r e s e n t e d by t h e n o r m a l i s e d f l o a t i n g p o i n t
ca rd : c a r d i n a l i t y o f t h e s e t F

14

16

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

18

20

22

24

26

28

30

32

34

36

38

F(1 ,1)=0; % zero


f =1;
f o r ( i =1: t )
i n v b ( 1 , i )=b( i ) ;
end
for ( s = [ 0 , 1 ] ) % 2 i t e r a t i o n s
f o r ( e=L :U) % (UL+1) i t e r a t i o n s
a=zeros ( t , 1 ) ;
a (1)=1;
f=f +1;
F( f ,1)=( 1) s ( b e ) ( i n v b a ) ;
f o r ( i =2:( b1)b ( t 1))
a=i n c r ( a , b ) ;
f=f +1;
F( f ,1)=( 1) s ( b e ) ( i n v b a ) ;
end
end
end

40

42

F=sort (F ) ;
c a r d=length (F ) ;

13

fLV.m
Contenu du fichier fLV.m :

function f=fLV ( t , x )
2

f=zeros ( 2 , 1 ) ;
a =2;
b=0.001;
c =10;
e =0.002;
f (1)= ax(1) bx ( 1 ) x ( 2 ) ;
f (2)= c x (2)+ e x ( 1 ) x ( 2 ) ;

14

fnewton.m
Contenu du fichier fnewton.m :

FNEWTON.M

function [ r e s , i t ]= fnewton ( func , dfunc , x , t o l )


% Finds a r o o t o f f ( x ) = 0 u s i n g Newton s method .
% Example c a l l : [ r e s , i t ]= fn ewt on ( func , dfunc , x , t o l )
% The u s e r d e f i n e d f u n c t i o n f u n c i s t h e f u n c t i o n f ( x ) ,
% The u s e r d e f i n e d f u n c t i o n d f u n c i s d f / dx .

Page 10

Syllabus Matlab

16

% x i s an i n i t i a l s t a r t i n g v a l u e , t o l i s r e q u i r e d a c c u r a c y .

i t = 0 ; x0=x ;
d=f e v a l ( func , x0 ) / f e v a l ( dfunc , x0 ) ;
while abs ( d)> t o l
x1=x0d ;
i t=i t +1;
x0=x1 ;
d=f e v a l ( func , x0 ) / f e v a l ( dfunc , x0 ) ;
end ;
r e s=x0 ;

10

12

14

16

15

forward col.m
Contenu du fichier forward col.m :

function [ b]= f o r w a r d c o l (L , b )
[ n]= m a t s q uare (L ) ;
T=t r i l (L ) ;

i f sum(sum(T = L)) >0


error ( Error : the matrix is not lower triagular ) ;
end

10

12

14

16

18

l = min( diag ( abs (L ) ) ) ;


i f l == 0
disp ( Error : the matrix is singular ) ; b = [ ] ;
break
end
f o r j =1:n1,
b ( j )= b ( j ) /L( j , j ) ;
b ( j +1:n)=b ( j +1:n)b ( j ) L( j +1:n , j ) ;
end ;
b ( n ) = b ( n ) /L( n , n ) ;

16

forward row.m
Contenu du fichier forward row.m :

10

12

14

16

18

function [ x]= f o r w a r d r o w (L , b )
[ n]= m a t s q uare (L ) ;
l = min( diag ( abs (L ) ) ) ;
T=t r i l (L ) ;
i f sum(sum(T = L)) >0
error ( Error : the matrix is not lower triagular ) ;
end
i f l == 0
disp ( Error : the matrix is singular ) ; x = [ ] ;
break
end
x (1) = b (1)/L ( 1 , 1 ) ;
for i = 2: n ,
x ( i ) = ( b ( i )L( i , 1 : i 1)( x ( 1 : i 1 ) ) ) /L( i , i ) ;
end
x=x ;
return

Page 11

FORWARD ROW.M

Syllabus Matlab

17

21

f test.m
Contenu du fichier f test.m :

function [ y , dy]= f t e s t ( x )
2

y=x . 3 8 ; dy=3x . 2 ;

18

ftest.m
Contenu du fichier ftest.m :

function y=f t e s t ( x )
2

y= x 3 + x 2 1 . 2 5 x 0 . 7 5

19

f test pt.m
Contenu du fichier f test pt.m :

function [ y]= f t e s t p t ( x )
2

y=log ( x + 4 ) ;

fzero root.m

20

Contenu du fichier fzero root.m :


2

while 1
approx = p l o t a p p ( f306 , 2 , 0 . 1 , 2 ) ;
% Use t h i s a p p r o x i m a t i o n use f z e r o t o f i n d e x a c t r o o t
r o o t=fzero ( f306 , approx ( 1 ) , 0 . 0 0 0 0 5 ) ;
f p r i n t f ( Exact root is %8.5f\n , r o o t ) ;
f p r i n t f ( \n ============================================ )
kont = input ( Type 1 to continue , or 0 to stop : ) ;
i f kont == 0 , break ; end

10

end

21

incr.m
Contenu du fichier incr.m :

function V1=i n c r (V, b )


% V vector with base b
% V( 1 ) mosts i g n i f i c a n t
% V( end ) l e a s t s i g n i f i c a n t
i f ( isempty ( [ find (V<0) , find (V>b 1 ) ] ) | ( isempty ( find (V<b 1 ) ) ) )
error ( Wrong input ) ;
end

12

l=length (V ) ;
i n d=find (V<=b 1);
i n d=i n d ( end ) ;

14

V1=V;

10

Page 12

INCR.M

Syllabus Matlab

23

V1( i n d )=V1( i n d )+1;


16

18

20

22

f o r ( i=l : 1 : 1 )
i f ( V1( i )>=b )
V1( i )=0;
V1( i 1)=V1( i 1)+1;
end
end

22

interp ls.m
Contenu du fichier interp ls.m :

close a l l
clear a l l
hold o f f
x =10:50;

10

12

14

Xp= 1 0 : 3 : 5 0 ;
Yp=2(Xp.2)+200 randn ( s i z e (Xp ) ) ;
plot ( x , 2 ( x . 2 ) )
hold
pause
plot (Xp , Yp , * )
a=axis ;
pause

16

18

y i n=p o l l a g (Xp , Yp , x )
plot ( x , y i n , r )
axis ( a ) ;

23

invpower.m
Contenu du fichier invpower.m :

10

12

14

16

18

20

22

function [ sigma , x , n i t e r , e r r ]= invpower (A, z0 , mu, t o l l , nmax )


n=max( s i z e (A ) ) ;
M=Amueye ( n ) ;
[ L , U, P]= lu (M) ;
q=z0 /norm( z0 ) ;
q2=q ;
err =[];
sigma = [ ] ;
r e s= t o l l +1;
n i t e r =0;
while ( r e s >= t o l l & n i t e r <= nmax )
n i t e r=n i t e r +1;
b=Pq ;
y=L \ b ; z = U \ y ;
q=z /norm( z ) ;
z=Aq ;
lam=q z ;
b=q2 ;
y=U \ b ;
w=L \ y ;
q2=(P w ) ;
q2=q2 /norm( q2 ) ;

Page 13

INVPOWER.M

Syllabus Matlab

24

26

28

30

32

27

LS POL.M

c o s t h e t a=abs ( q2 q ) ;
i f ( c o s t h e t a >= 5e 2) ,
r e s=norm( zlamq ) / c o s t h e t a ;
e r r =[ e r r ; r e s ] ;
sigma =[ sigma ; lam ] ;
else ,
disp ( Multiple eigenvalue ) ; break ;
end
x=q ;
end
return

24

kern2.m
Contenu du fichier kern2.m :

function K=kern ( x , c e n t e r , ba s e )
% X[N, n ]
% c e n t e r [m, n ]

K=exp( (( x(1) c e n t e r ( 1 ) ) / b a s e ) . 2 ) exp( (( x(2) c e n t e r ( 2 ) ) / b a s e ) . 2 ) ;

25

kern.m
Contenu du fichier kern.m :

function K=kern ( x , c e n t e r , ba s e )
% X[N, n ]
% c e n t e r [m, n ]

K=exp( (( xc e n t e r ) / b a s e ) . 2 ) ;

26

loga.m
Contenu du fichier loga.m :

function l=l o g a ( a , x )
2

l=log ( x ) / log ( a ) ;

27

ls pol.m
Contenu du fichier ls pol.m :

function [ y , a]= l s p o l (Xp , Yp ,m, x )


2

f o r ( i =1: length (Xp ) )


f o r ( j =1:m+1)
X( i , j )=Xp( i ) ( j 1);
end
Y( i ,1)=Yp( i ) ;
end

10

12

a=pinv (X) Y;

Page 14

Syllabus Matlab

14

16

31

y=zeros ( s i z e ( x ) ) ;
f o r ( j =1:m+1)
y=y+a ( j ) . x . ( j 1);
end

28

lu kji.m
Contenu du fichier lu kji.m :

10

function [ A] = l u k j i (A)
[ n , n]= s i z e (A ) ;
f o r k=1:n1
A( k+1:n , k)=A( k+1:n , k ) /A( k , k ) ;
f o r j=k+1:n ,
f o r i=k+1:n
A( i , j )=A( i , j )A( i , k ) A( k , j ) ;
end
end
end
return

29

mat square.m
Contenu du fichier mat square.m :

function [ n]= m at s quare (A)


[ n ,m] = s i z e (A ) ;
i f n = m
disp ( Error : the matrix should be squared ) ;
stop
end
return

30

model2.m
Contenu du fichier model2.m :

function z=model ( x , y )
2

z =0.1+(1.0+ sin ( 2 . x +3. y ) ) . / ( 3 . 5 + sin ( xy ) ) ;

31

model.2m.m
Contenu du fichier model.2m.m :

function y=model ( x )
2

y=0.1+(1+ sin ( 3 x + 2 ) ) . / ( 3 . 5 + sin (2x ) ) ;


y =0.5+0.4 sin ( 2 pi x ) ;

Page 15

MODEL.2M.M

Syllabus Matlab

32

35

model.m
Contenu du fichier model.m :

function y=model ( x )
2

y=0.1+(1+ sin ( 3 x + 2 ) ) . / ( 3 . 5 + sin (2x ) ) ;


y =0.5+0.4 sin ( 2 pi x ) ;

33

phi.m
Contenu du fichier phi.m :

function p=p h i (Y, A, b )


% Y[ n ,N]
N=s i z e (Y, 2 ) ;
f o r i =1:N
p ( i ,1)=1/2Y( : , i ) AY( : , i )Y( : , i ) b ;
end

34

plot circle.m
Contenu du fichier plot circle.m :

function p l o t c i r c l e ( x0 , y0 , R, c )
2

10

12

% ( xx0 )2+( yy0 )2=R2


i f nargin<4
c=k ;
end
x=x0R : . 0 1 : x0+R;
x=[x x0+R ] ;
plot ( x , y0sqrt ((xx0 ).2+R 2 ) , c ) ;
i f ( ishold )
hold
end

14

plot ( x , y0+sqrt ((xx0 ).2+R 2 ) , c ) ;

35

plot fun.m
Contenu du fichier plot fun.m :

function p l o t f u n ( fun , x1 , x2 , c )
2

x min=min( x1 , x2 ) ;
x max=max( x1 , x2 ) ;

d e l t a =(x maxx min ) / 5 0 0 0 ;


8

I x=x min : d e l t a : x max ;


10

plot ( I x , f e v a l ( fun , I x ) , c )

Page 16

PLOT FUN.M

Syllabus Matlab

36

38

plot line2.m
Contenu du fichier plot line2.m :

function p l o t l i n e ( x , y ,m, c )
2

x min=xabs ( x ) 5;
x max=x+abs ( x )+5;

d e l t a =(x maxx min ) / 5 0 ;


8

I x=x min : d e l t a : x max ;


10

plot ( I x , y+m. ( I x x ) , c )

plot line.m

37

Contenu du fichier plot line.m :


function p l o t l i n e ( x1 , y1 , x2 , y2 , c )
2

i f ( x1 = x2 )
x min=min( x1 , x2 ) ;
x max=max( x1 , x2 ) ;

d e l t a =(x maxx min ) / 5 0 ;

I x=x min : d e l t a : x max ;

10

12

14

16

plot ( I x , ( y2y1 ) / ( x2x1 ) . ( I x x1)+y1 , c )


e l s e i f ( y1==y2 )
plot ( x1 , y1 , * ) ;
else
y min=min( y1 , y2 ) ;
y max=max( y1 , y2 ) ;

18

d e l t a =(y maxy min ) / 5 0 ;


20

I y=y min : d e l t a : y max ;


22

plot ( x1 o n e s ( s i z e ( I y ) ) , I y , c )
24

end

38

pol lag.m
Contenu du fichier pol lag.m :

function y=p o l l a g (X, Y, x )


2

on=o n e s ( s i z e ( x ) ) ;
4

f o r ( i =1: length (X) )


p ( i , : ) =Y( i ) on ;
f o r ( j =1: length (X) )

10

i f ( j = i )

Page 17

POL LAG.M

Syllabus Matlab

16

REAL2F.M

p ( i , : ) = p ( i , : ) . ( xX( j ) ) . / ( X( i )X( j ) ) ;

12

14

40

end
end
end
y=sum( p ) ;

39

powerm.m
Contenu du fichier powerm.m :

10

12

14

16

18

20

22

24

26

28

function [ nu1 , x1 , n i t e r , e r r ]=powerm (A, z0 , t o l l , nmax )


q=z0 /norm( z0 ) ;
q2=q ;
err =[];
nu1 = [ ] ;
r e s= t o l l +1;
n i t e r =0;
z=Aq ;
while ( r e s >= t o l l & n i t e r <= nmax )
q=z /norm( z ) ;
z=Aq ;
lam=q z ;
x1=q ;
z2=q2 A;
q2=z2 /norm( z2 ) ;
q2=q2 ;
y1=q2 ;
c o s t h e t a=abs ( y1 x1 ) ;
i f ( c o s t h e t a >= 5e 2) ,
n i t e r=n i t e r +1;
r e s=norm( zlamq ) / c o s t h e t a ;
e r r =[ e r r ; r e s ] ;
nu1=[nu1 ; lam ] ;
else
disp ( Multiple eigenvalue ) ; break ;
end
end
return

40

real2f.m
Contenu du fichier real2f.m :

function [ s ,m, e ]= r e a l 2 f ( r , b , t , l , u )
2

10

12

14

% r e a l 2 f : rounds t o t h e n e a r e s t f l o a t i n g p o i n t . I t r e t u r n s e r r o r i n c a s e o f o v e r f l o w / u n d e
%
%
REAL2F(R, B, T, L ,U)
%
R [1 ,1] real input
%
B [1 ,1] base
%
T [ 1 , 1 ] number o f s i g n i f i c a n t d i g i t s
% L [ 1 , 1 ] l o w e r bound e x p o n e n t
%
U [ 1 , 1 ] upper bound e x p o n e n t
%
%
Returns :
%
M [ 1 ,T ] m a n t i s s a v e c t o r o f d i g i t s
% E [1 ,1] exponent
%
S [ 1 , 1 ] signum : 0 f o r p o s i t i v e , 1 f o r n e g a t i v e

16

Page 18

Syllabus Matlab

18

41

ROOT ITER.M

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

20

22

24

26

28

30

32

i f ( r==0)
s =0;
m=0;
e =0;
return ;
end
s=r <0;
r=abs ( r ) ;
m=zeros ( t , 1 ) ;
i f ( r < b ( l 1))
error ( underflow ) ;
end

34

36

i f ( r>bu(1b( t ) ) )
error ( overflow ) ;
end

38

40

42

44

e=c e i l ( l o g a ( b , r ) ) ;
f=r ( b ( te ) ) ;
f o r ( i =1: t 1)
m( i )= f l o o r ( f / ( b ( ti ) ) ) ;
f=f b ( ti ) m( i ) ;

46

48

50

52

54

56

end
i f ( round ( f )<b )
m( t )=round ( f ) ;
e l s e i f ( t ==1 | isempty ( find (m( 1 : end1)<b 1)))
e=e +1;
m( 1 ) = 1 ;
m( 2 : end)=0;
else
m=i n c r ( [m( 1 : end 1 ) ; b 1] , b ) ;
end

41

root iter.m
Contenu du fichier root iter.m :

function [ xvect , x d i f , fx , n i t ]= r o o t i t e r ( a , b , meth , nmax , t o l l , fun , g r a p h i c , t e s t )


2

10

12

14

% meth : 1 f o r corde , 2 f o r s e c a n t , 3 f o r r e g u l a f a l s i
% t e s t : 1 f o r i n c r e m e n t and 2 f o r r e s i d u a l
i f ( graphic )
hold o f f
d e l t a =2;
p l o t f u n ( fun , a , b , - ) % p l o t f u n c t i o n
hold on
p l o t l i n e ( a , 0 , b , 0 , k- ) % p l o t xa x i s
delta2 =0.2;
zoom on
pause

Page 19

Syllabus Matlab

41

end
16

18

i f ( nargin <8)
t e s t =1;
end

20

22

24

26

28

30

32

x=a ;
f a=f e v a l ( fun , a ) ;
x=b ;
f b=f e v a l ( fun , b ) ;
m=(fbf a ) / ( ba ) ;
s t o p= t o l l +1;
n i t =2;
i f ( meth<4)
x=af a /m;
else
x=a ;
end

%c o r d e

34

36

f x =[ f a ; f b ; f e v a l ( fun , x ) ] ;
x v e c t =[a ; b ; x ] ;
xdif =[];

38

40

42

44

i f ( g r a p h i c & meth<4)
plot ( a , fa , * ) ;
plot ( b , fb , * ) ;
p l o t l i n e ( a , fa , b , fb , r ) ; % p l o t c o r d e
plot ( x , 0 , * ) ;
% plot intersection
pause
end

46

48

50

52

while ( n i t < nmax & s t o p > t o l l )


% x k > x ( k )
% x k 1 > x ( k+1)
n i t=n i t +1;
x k=x v e c t ( n i t ) ;
f x k=f e v a l ( fun , x k ) ;

54

56

58

60

62

64

i f ( meth==2) %s e c a n t method
m=( f x k f x ( n i t 1))/( x kx v e c t ( n i t 1 ) ) ;
e l s e i f ( meth==3) % Regula F a l s i
I k=find ( f x k fx <0);
i f ( length ( I k ) >0)
m=( f x k f x ( I k ( end ) ) ) / ( x kx v e c t ( I k ( end ) ) ) ;
end
e l s e i f ( meth==4) % Newton
[ d ,m]= f e v a l ( fun , x k ) ;
end

66

72

x k1=x kf x k /m;
i f ( t e s t ==1)
s t o p=abs ( x k1x k ) ;
else
s t o p=abs ( f x k ) ;
end

74

x d i f =[ x d i f ; s t o p ] ;

68

70

Page 20

ROOT ITER.M

Syllabus Matlab

42

ROUND2FLO.M

i f ( graphic )
axis ( [ min( x v e c t )abs ( d e l t a 2 min( x v e c t ) ) max( x v e c t )+abs ( d e l t a 2 max( x v e c t ) ) . . .
min( f x )abs ( d e l t a 2 min( f x ) ) max( f x )+abs ( d e l t a 2 max( f x ) ) ] )

76

78

p l o t l i n e ( x k , 0 , x , f e v a l ( fun , x k ) , : ) % p l o t f u n c t i o n v a l u e
plot ( x k , f x k , * )
p l o t l i n e 2 ( x k , f x k ,m, r ) ;
% p l o t corde
plot ( x k1 , 0 , * ) ;
i f ( meth==1)
t i t l e ( [ Corde method : Iteration = num2str ( n i t 2 ) ] )
e l s e i f ( meth==2)
t i t l e ( [ Secant method : Iteration = num2str ( n i t 2 ) ] )
e l s e i f ( meth==3)
t i t l e ( [ Regula falsi method : Iteration = num2str ( n i t 2 ) ] )
e l s e i f ( meth==4)
t i t l e ( [ Newton method : Iteration = num2str ( n i t 2 ) ] )
end

80

82

84

86

88

90

92

pause

94

end
96

x=x k1 ;
x v e c t =[ x v e c t ; x ] ;
f x =[ f x ; f e v a l ( fun , x ) ] ;

98

100

end
return

42

round2flo.m
Contenu du fichier round2flo.m :

function M x t i l d e=r o u n d 2 f l o (Mx, b , t )


% round2flo : returns the r e a l cooresponding to the nearest f l o a t i n g point
%
%
ROUND2FLO(Mx, B,T)
%
Mx [ 1 , 1 ] r e a l number
%
B [1 ,1] base
%
T [ 1 , 1 ] number o f s i g n i f i c a n t d i g i t s
%
Returns M x t i l d e [ 1 , 1 ] r e a l nummber i n d e c i m a l f o rm a t
%

10

12

14

16

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002
i f ( t < 1 | b<2)
error ( Wrong input )
end

18

[ N, n]= s i z e (Mx) ;
20

22

24

26

f o r ( i =1:N)
f o r ( j =1:n )
x=Mx( i , j ) ;
[ s ,m, e ]= r e a l 2 f ( x , b , t , 5 2 , 5 2 ) ;
M x t i l d e ( i , j )= f l o 2 r e a l (m, e , b , s ) ;
end
end

Page 21

% plot in

Syllabus Matlab

43

44

S ARRET.M

s ab stababs.m
Contenu du fichier s ab stababs.m :

clear a l l

for ( h = 0 . 1 : . 1 : 1 )
clear y
c l e a r yt
close a l l

hold o f f
T=15;
t = 0 : . 1 :T;
lambda=1;
plot ( t , exp ( lambda t ) ) ;

10

12

14

c o n t =1;
y ( c o n t )=1;
yt ( c o n t )=0;

16

18

22

y ( c o n t+1)=y ( c o n t )+h lambda y ( c o n t ) ;


yt ( c o n t+1)=h ;
c o n t =2;

24

r=roots ( [ 2 3 hlambda 2 lambda h ] )

20

26

f o r ( th =(2h ) : h :T)
yt ( c o n t+1)=th ;
y ( c o n t+1)=y ( c o n t )+0.5 h ( 3 lambda y ( c o n t )lambda y ( cont 1 ) ) ;
c o n t=c o n t +1;
end

28

30

32

34

hold

36

plot ( yt , y , r )
t i t l e ( [ h= num2str ( h ) ,
xlabel ( time )
pause

38

r_0= num2str ( r ( 1 ) ) r_1= num2str ( r ( 2 ) ) ] )

40

end

44

s arret.m
Contenu du fichier s arret.m :

% SCRIPT s i t e r .m
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% March 20 0 2

10

close a l l
fun=f_arret ;
a =0;
b=30;
nmax=120000;

Page 22

Syllabus Matlab

12

46

S COMPR.M

t o l l =1e 3;
log ( 1 e 9)

14

16

meth = 4 ; % meth : 1 f o r corde , 2 f o r s e c a n t , 3 f o r r e g u l a f a l s i , 4 Newton


s t o p = 2 ; % 1 f o r increment , 2 f o r r e s i d u a l

18

[ xvect , x d i f , fx , n i t ]= r o o t i t e r ( a , b , meth , nmax , t o l l , fun , 1 , s t o p )

45

s assoc.m
Contenu du fichier s assoc.m :

% SCRIPT s a s s o c
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

b=10;
t =2;
d1 = 0 . 1 6 5 6 ;
d2 = 7 . 4 4 0 9 ;
d3 = 2 . 9 1 6 8 ;

12

14

d s t a r 1=r o u n d 2 f l o ( d1 , b , t ) ;
d s t a r 2=r o u n d 2 f l o ( d2 , b , t ) ;
d s t a r 3=r o u n d 2 f l o ( d3 , b , t ) ;

16

18

20

xtemp=r o u n d 2 f l o ( d s t a r 2+d s t a r 3 , b , t ) ;
x s t a r 1=r o u n d 2 f l o ( d s t a r 1+xtemp , b , t )
xtemp=r o u n d 2 f l o ( d s t a r 1+d s t a r 2 , b , t ) ;
x s t a r 2=r o u n d 2 f l o ( d s t a r 3+xtemp , b , t )

46

s compr.m
Contenu du fichier s compr.m :

% SCRIPT s compr
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% March 20 0 2

10

12

14

16

18

20

clear a l l
close a l l
cm = [ 0 : 1 / 2 5 6 : 1 ; 0 : 1 / 2 5 6 : 1 ; 0 : 1 / 2 5 6 : 1 ] ;
colormap (cm )
a=d o u b l e ( imread ( alf.jpg ) ) ;
[m, n]= s i z e ( a ) ;
c o m p l o r i g=s i z e ( a , 1 ) s i z e ( a , 2 ) ;
image ( a ) ;
t i t l e ( [ Original uncompressed image . Size= num2str ( c o m p l o r i g ) Normalised =1 ] )
pause
[ U, S ,V]=svd ( d o u b l e ( a ) ) ;
size (S)
for ( h =1:5:100)
A=U ( : , 1 : h ) S ( 1 : h , 1 : h ) (V ( : , 1 : h ) ) ;
compl=( s i z e (U, 1 ) h+h+s i z e (V, 1 ) h )

Page 23

Syllabus Matlab

48

S CONIUG.M

22

image (A)
t i t l e ( [ Compressed image h= num2str ( h ) Size= num2str ( compl ) Normalised = num2str (
PSNR = 5 log10 ( ( 1 /mn ) sum(sum( (Aa ) . 2 ) ) )
pause

24

26

end

s coniug2.m

47

Contenu du fichier s coniug2.m :


% SCRIPT s c o n i u g
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% February 2002

10

12

14

close
clear
n=3;
A= [ 2
3

all
all

3 5; ...
2 6; ...
5 6 2];
b=[1; 3; 4];

16

18

20

22

xhat = [ 0 ; 0 ; 0 ] ;
p=zeros ( 3 , 1 1 ) ;
r=zeros ( 3 , 1 1 ) ;
r ( : , 1 ) = bA xhat ;
p(: ,1)= r ( : , 1 ) ;

24

26

f o r ( k=1:n1)
plot ( xhat ( 1 ) , xhat ( 2 ) , k* )
r ( : , k)=bA xhat ;
a l p h a=p ( : , k ) r ( : , k ) / ( p ( : , k ) Ap ( : , k ) ) ;
xnew=xhat+a l p h a p ( : , k ) ;
r ( : , k+1)=r ( : , k) a l p h a Ap ( : , k ) ;
beta=(Ap ( : , k ) ) r ( : , k +1)/(p ( : , k ) Ap ( : , k ) )
p ( : , k+1)=r ( : , k+1)betap ( : , k ) ;
pause

28

30

32

34

p l o t l i n e ( xhat ( 1 ) , xhat ( 2 ) , xnew ( 1 ) , xnew ( 2 ) , r: ) ;


xhat=xnew

36

38

end

48

s coniug.m
Contenu du fichier s coniug.m :

% SCRIPT s c o n i u g
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% February 2002

Page 24

Syllabus Matlab

49

S COST.M

10

12

close a l l
clear a l l
A= [ 2 3 ; 3 2 ] ;
b=[1; 3];

14

[ y1 , y2 ] = meshgrid ( 2 : . 5 : 2 , 2 : . 5 : 2 ) ;
16

18

z =1/2( y1 . 2 A(1 ,1)+ y2 . y1 A(2 ,1)+ y1 . y2 A(1 ,2)+ y2 . 2 A(2 ,2)) b ( 1 ) y1b ( 2 ) y2 ;

20

22

24

26

hold o f f
x=A\b
contour ( y1 , y2 , z , 3 0 )
hold on
plot ( x ( 1 ) , x ( 2 ) , r* )

28

30

32

xhat = [ 1 . 5 ; 1 . 5 ] ;
p=zeros ( 2 , 1 1 ) ;
r=zeros ( 2 , 1 1 ) ;
r ( : , 1 ) = bA xhat ;
p(: ,1)= r ( : , 1 ) ;

34

36

f o r ( k =1:3)
plot ( xhat ( 1 ) , xhat ( 2 ) , k* )
r ( : , k)=bA xhat ;
a l p h a=p ( : , k ) r ( : , k ) / ( p ( : , k ) Ap ( : , k ) ) ;
xnew=xhat+a l p h a p ( : , k ) ;
r ( : , k+1)=r ( : , k) a l p h a Ap ( : , k ) ;
beta=(Ap ( : , k ) ) r ( : , k +1)/(p ( : , k ) Ap ( : , k ) )
p ( : , k+1)=r ( : , k+1)betap ( : , k ) ;
pause

38

40

42

44

p l o t l i n e ( xhat ( 1 ) , xhat ( 2 ) , xnew ( 1 ) , xnew ( 2 ) , r: ) ;


xhat=xnew

46

48

end

49

s cost.m
Contenu du fichier s cost.m :

% SCRIPT s c o s t
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

12

clear a l l
addpath ( c:\ bontempi \prof\ calcul \ quarteroni \ Programs )
I =2:20;
f o r ( n=I )
f l ( n ,1)= c o u t ( n ) ;
bon ( n , 1 ) = 2 ( n3/3n/3)+n ( n 1)/2;

Page 25

Syllabus Matlab

14

16

18

20

50

end
plot ( I , [ f l ( I ) bon ( I ) ] )
t i t l e ( Algorithmic cost LU )
xlabel ( order of the square matrix )
ylabel ( flops )
legend ( Matlab flops , theoretical flops ) ;

50

s dist flo.m
Contenu du fichier s dist flo.m :

% SCRIPT s d i s t f l o .m
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

clear a l l
close a l l

10

b = 1 0 ; t = 1 ; L=1; U=1;
12

14

[ F , c ]= f l o s e t ( b , t , L ,U ) ;

16

F n o z e r o=F(F=0);

18

20

22

24

26

28

30

%
f p r i n t f ( 1 , Check that the minimum of F is b^(L -1)... ) ;
b (L1)
min abs F=min( abs ( F n o z e r o ) )
f p r i n t f ( 1 , Push a button to continue ) ;
pause
%
f p r i n t f ( 1 , Check that the maxium of F is b^U*(1 -b^(-t ))... ) ;
bU(1b( t ) )
max abs F=max( abs ( F n o z e r o ) )
f p r i n t f ( 1 , Push a button to continue ) ;
pause

32

%
34

36

38

F=F(F>0);
i n f r e l d i s t =b( t )
s u p r e l d i s t=b(1 t )

40

42

44

46

r e l d i s t =[NaN; d i f f (F ) . / F ( 1 : end 1 ) ] ;
hold o f f
plot (F ( 1 : end ) , abs ( r e l d i s t ) , *- )
hold
plot (F , i n f r e l d i s t o n e s ( length (F ) , 1 ) , -- )
plot (F , s u p r e l d i s t o n e s ( length (F ) , 1 ) , -- )

Page 26

S DIST FLO.M

Syllabus Matlab

48

52

xlabel ( Floating point numbers )


ylabel ( Relative Distance )

50

52

axis ( [ min(F ) max(F ) 0 1 . 2 s u p r e l d i s t ] )


min( abs ( r e l d i s t ) )
max( abs ( r e l d i s t ) )

51

s err arr.m
Contenu du fichier s err arr.m :

% SCRIPT s e r r a r r .m
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

clear a l l
close a l l
% Random number g e n e r a t i o n
r =10randn ;

12

14

16

f o r ( b =2:16)
I T =1:10;
f o r ( t=I T )
r t i l d e=r o u n d 2 f l o ( r , b , t ) ;
% rounding to the nearest f l o a t i n g point

18

e r e l ( t ,1)= abs ( ( rr t i l d e ) / r ) ;
% r o u n d i n g e r r o r c o mp ut a t io n

20

bound ( t , 1 ) = 0 . 5 b(1 t ) ;
% t h e o r e t i c a l bound c o mp ut a t i o n
i f ( e r e l ( t ) >0.5 b(1 t ) & 0 . 5 b(1 t )>1e 14)
error ( what? ) ;

22

24

26

end
end

28

30

32

34

36

%v i s u a l i s a t i o n
plot ( I T , [ e r e l bound ] )
xlabel ( Number of significant digits ) ;
legend ( Rounding error , bound ) ;
t i t l e ( [ base= num2str ( b ) ] )
pause
end

52

s errgen.m
Contenu du fichier s errgen.m :

% SCRIPT s e r r g e n
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

Page 27

S ERRGEN.M

Syllabus Matlab

54

S EULER.M

t=3
b=10;

10

12

d = 0 . 1 0 7 1 e1 ;
x=d2 1;

14

d s t a r=r o u n d 2 f l o ( d , b , t ) ;

16

f p r i n t f ( 1 , First algorithm ) ;
x 1 s t a r=r o u n d 2 f l o ( d s t a r d s t a r , b , t ) ;
x 2 s t a r=r o u n d 2 f l o ( x 1 s t a r 1,b , t ) ;

18

20

e r r 1=abs ( ( xx 2 s t a r ) / x )
pause

22

24

26

f p r i n t f ( 1 , Second algorithm ) ;
x 1 s t a r=r o u n d 2 f l o ( d s t a r 1,b , t ) ;
x 2 s t a r=r o u n d 2 f l o ( x 1 s t a r (2+ x 1 s t a r ) , b , t ) ;
e r r 2=abs ( ( xx 2 s t a r ) / x )

53

s euler arr.m
Contenu du fichier s euler arr.m :

10

clear a l l
close a l l
addpath . . \ deuxieme
hold o f f
t =0:.01:0.5;
b=10;
t t =2;
y=f e v a l ( sol_an , t ) ;
plot ( t , y )
hold

12

14

16

18

20

22

24

26

28

for ( h = [ 0 . 1 : 0 . 0 0 5 : 0 . 0 0 5 ] )
clear y hat ;
s t e p =1;
y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t =0:h : 0 . 5 )
y h a t ( s t e p +1)= r o u n d 2 f l o ( y h a t ( s t e p )+ r o u n d 2 f l o ( h d e r a n ( t , y h a t ( s t e p ) ) , b , t t ) , b , t t ) ;
s t e p=s t e p +1;
end
plot ( 0 : h : 0 . 5 , y h a t ( 1 : end1) , k- )
h
pause
end
xlabel ( t )
ylabel ( y(t) )
%l e g e n d ( A n a l y t i c a l , E u l e r h = 0 . 0 5 , E u l e r h = 0 . 1 )

54

s euler.m
Contenu du fichier s euler.m :

clear a l l

Page 28

Syllabus Matlab

close a l l
hold o f f
t =0:.01:0.5;

y=f e v a l ( sol_an , t ) ;

plot ( t , y )
hold

55

10

12

14

16

18

20

22

24

for ( h = [ 0 . 0 5 0 . 1 ] )
clear y hat ;
s t e p =1;
y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t =0:h : 0 . 5 )
y h a t ( s t e p +1)= y h a t ( s t e p )+h d e r a n ( t , y h a t ( s t e p ) ) ;
s t e p=s t e p +1;
end
i f ( h==0.05)
plot ( 0 : h : 0 . 5 , y h a t ( 1 : end1) , r -* )
else
plot ( 0 : h : 0 . 5 , y h a t ( 1 : end1) , k -* )
end

26

end
28

legend ( Analytical , Euler h=0.05 , Euler h=0.1 )

55

s euler pc.m
Contenu du fichier s euler pc.m :

clear a l l
close a l l
hold o f f
t =0:.01:0.5;

y=f e v a l ( sol_an , t ) ;

plot ( t , y )
hold

10

18

h=0.1;
clear y hat ;
s t e p =1;
y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t =0:h : 0 . 5 )
y h a t ( s t e p +1)= y h a t ( s t e p )+h d e r a n ( t , y h a t ( s t e p ) ) ;
s t e p=s t e p +1;
end

20

plot ( 0 : h : 0 . 5 , y h a t ( 1 : end1) , r-* )

12

14

16

22

24

26

clear y hat ;
s t e p =1;
y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t =0:h : 0 . 5 )
y h a t ( s t e p +1)= y h a t ( s t e p )+h d e r a n ( t , y h a t ( s t e p ) ) ;

Page 29

S EULER PC.M

Syllabus Matlab

57

y h a t ( s t e p +1)= y h a t ( s t e p )+h d e r a n ( t , y h a t ( s t e p + 1 ) ) ;

28

s t e p=s t e p +1;

30

end
32

plot ( 0 : h : 0 . 5 , y h a t ( 1 : end1) , k-* )


34

36

legend ( Analytical , Euler h=0.1 , Euler pred/corr h=0.1

56

s eul stababs.m
Contenu du fichier s eul stababs.m :

clear a l l
close a l l
hold o f f
T=5;
t = 0 : . 1 :T ;
lambda=5;
plot ( t , exp ( lambda t ) ) ;

12

c o n t =1;
y ( c o n t )=1;
yt ( c o n t +1)=0;

14

h=0.41;

16

h lambda
pause

10

18

20

22

f o r ( th=h : h :T)
yt ( c o n t+1)=th ;
y ( c o n t+1)=y ( c o n t )+h lambda y ( c o n t ) ;
c o n t=c o n t +1;
end

24

26

hold

28

plot ( yt , y , r )
t i t l e ( [ h= num2str ( h ) ,
xlabel ( time )

30

57

h* Lambda = num2str ( h lambda ) ] )

s exemple.m
Contenu du fichier s exemple.m :

clear a l l
close a l l
hold o f f
X= [ 3 : . 0 5 : 3 ] ;
Y=sin (X ) ;

10

x = 3:.05:3;
y=sin ( x ) ;
yp=p o l l a g (X, Y, x ) ;

Page 30

S EXEMPLE.M

Syllabus Matlab

12

plot ( x , y )

14

hold
plot ( x , yp )
t i t l e ( Interpolation polynomiale )

16

58

61

S GAUSEI2.M

s fillin.m
Contenu du fichier s fillin.m :

% SCRIPT s f i l l i n
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002
clear a l l
rand ( state , 0 )

10

n=100;
A=sprand ( n , n , 0 . 0 1 ) ;

12

LU=s p y l u k j i (A ) ;

59

s fixpt.m
Contenu du fichier s fixpt.m :

[ k , p , e r r , P]= f i x p t ( f_test_pt , 3 , 1 e 9 ,123)

60

s floset.m
Contenu du fichier s floset.m :

% SCRIPT s f l o s e t
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

clear a l l
close a l l
b = 1 0 ; t = 1 ; L=1;U=1;
[ f , c ]= f l o s e t ( b , t , L ,U ) ;

12

14

plot ( f , zeros ( s i z e ( f ) ) , * )
t i t l e ( [ b= num2str ( b ) t= num2str ( t )

16

c a r d =2(b1)b ( t 1)(UL+1)+1

61

s gausei2.m
Contenu du fichier s gausei2.m :

Page 31

L= num2str (L )

U= num2str (U ) ] )

Syllabus Matlab

62

% SCRIPT s g a u s e i 2
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

12

clear a l l
n=3;
A= [ 3 1 1 ; 2 6 2 ; 1 2 4 ] ;
b=[3; 10 ; 7];
D=diag ( diag (A ) ) ;
E=t r i l (A, 1 ) ;

14

16

P=DE ;
F=t r i u (A, 1 ) ;

18

N=F ;

20

x ( : , 1 ) = zeros ( n , 1 ) ;

22

24

26

28

f o r i =2:20
x ( : , i )=inv (P) (Nx ( : , i 1)+b ) ;
end
figure (2)
plot ( x )

s gausei.m

62

Contenu du fichier s gausei.m :


% SCRIPT s g a u s e i
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

12

clear a l l
n=4;
A= [ 5 7 6 5 ; 7 1 0 8 7 ; 6 8 1 0 9 ; 5 7 9 1 0 ] ;
b=[23; 32 ; 33; 31];
D=diag ( diag (A ) ) ;
E=t r i l (A, 1 ) ;

14

16

P=DE ;
F=t r i u (A, 1 ) ;

18

N=F ;

20

x=10 o n e s ( n , 1 ) ;

22

24

f o r i =1:3000
x=inv (P) (Nx+b )

26

end

Page 32

S GAUSEI.M

Syllabus Matlab

63

64

S GERS.M

s gers2.m
Contenu du fichier s gers2.m :

clear a l l
close a l l
hold o f f
n=4;
A=randn ( n , n )

[ V, L]= eig (A ) ;
8

Lambda=diag (L ) ;
10

12

plot ( r e a l ( Lambda ) , imag ( Lambda ) , rx )


hold on
p l o t c i r c l e ( 0 , 0 ,norm(A) )

14

16

18

f o r ( i =1:n )
p l o t c i r c l e (A( i , i ) , 0 ,sum( abs (A( i , [ 1 : i 1 i +1:end ] ) ) ) , r-- )
end

20

22

f o r ( j =1:n )
p l o t c i r c l e (A( j , j ) , 0 ,sum( abs (A ( [ 1 : j 1 j +1:end ] , j ) ) ) , g-- )
end

24

t i t l e ( Noir : norme . Rouge : disques lignes . Vert : disques colonnes )

64

s gers.m
Contenu du fichier s gers.m :

clear a l l
close a l l
hold o f f
n=3;
A= [ 1 0 2 3 ; 1 2 1 ; 0 1 3 ] ;

[ V, L]= eig (A ) ;
8

Lambda=diag (L ) ;
10

12

plot ( r e a l ( Lambda ) , imag ( Lambda ) , rx )


hold on
p l o t c i r c l e ( 0 , 0 ,norm(A) )

14

16

18

f o r ( i =1:n )
p l o t c i r c l e (A( i , i ) , 0 ,sum( abs (A( i , [ 1 : i 1 i +1:end ] ) ) ) , r-- )
end

20

22

24

f o r ( j =1:n )
p l o t c i r c l e (A( j , j ) , 0 ,sum( abs (A ( [ 1 : j 1 j +1:end ] , j ) ) ) , g-- )
end
xlabel ( Re(z) )
ylabel ( Im(z) )

26

t i t l e ( Noir : norme . Rouge : disques lignes . Verte : disques colonnes )

Page 33

Syllabus Matlab

65

66

S GRADIENT.M

s grad al.m
Contenu du fichier s grad al.m :

% SCRIPT s g r a d a l
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% February 2002

10

close a l l
clear a l l
A= [ 2 3 ; 3 2 ] ;
b=[1; 3];

12

14

[ y1 , y2 ] = meshgrid ( 2 : . 5 : 2 , 2 : . 5 : 2 ) ;
xhat = [ 1 . 5 ; 1 . 5 ] ;

16

z =1/2( y1 . 2 A(1 ,1)+ y2 . y1 A(2 ,1)+ y1 . y2 A(1 ,2)+ y2 . 2 A(2 ,2)) b ( 1 ) y1b ( 2 ) y2 ;
18

20

22

24

hold o f f
x=A\b
contour ( y1 , y2 , z , 3 0 )
hold on
plot ( x ( 1 ) , x ( 2 ) , r* )

26

28

f o r ( k =1:10)
plot ( xhat ( 1 ) , xhat ( 2 ) , k* )

30

r=bA xhat ;
a l p h a=r r / ( r A r ) ;
xnew=xhat+a l p h a r ;
pause

32

34

p l o t l i n e ( xhat ( 1 ) , xhat ( 2 ) , xnew ( 1 ) , xnew ( 2 ) , r: ) ;


xhat=xnew

36

38

end

66

s gradient.m
Contenu du fichier s gradient.m :

% SCRIPT s g r a d i e n t
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% February 2002

10

12

A= [ 5 0 . 1 ; 0 . 1 5 ] ;
b=[1; 3];
[ y1 , y2 ] = meshgrid ( 2 : . 0 1 : 2 , 2 : . 0 1 : 2 ) ;

14

Page 34

Syllabus Matlab

68

S ITER.M

z =1/2( y1 . 2 A(1 ,1)+ y2 . y1 A(2 ,1)+ y1 . y2 A(1 ,2)+ y2 . 2 A(2 ,2)) b ( 1 ) y1b ( 2 ) y2 ;
16

18

20

figure (1)
h=mesh( y1 , y2 , z )
xlabel ( x1 ) ;
ylabel ( x2 ) ;
z l a b e l ( PHI ) ;

22

24

26

28

figure (2)
hold o f f
x=A\b
contour ( y1 , y2 , z )
hold on
plot ( x ( 1 ) , x ( 2 ) , r* )

s half.m

67

Contenu du fichier s half.m :


% SCRIPT s h a l f
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

b =10 , t=4
d1 =0.8717
d2 =0.8719

12

14

16

d s t a r 1=r o u n d 2 f l o ( d1 , b , t ) ;
d s t a r 2=r o u n d 2 f l o ( d2 , b , t ) ;
f p r i n t f ( 1 , First algorithm ) ;
x s t a r 1=r o u n d 2 f l o ( r o u n d 2 f l o ( d s t a r 1+d s t a r 2 , b , t ) / 2 , b , t )

18

pause
20

22

f p r i n t f ( 1 , Second algorithm ) ;
xtemp=r o u n d 2 f l o ( r o u n d 2 f l o ( d s t a r 2 d s t a r 1 , b , t ) / 2 , b , t )
x s t a r 2=r o u n d 2 f l o ( d s t a r 1+xtemp , b , t )

68

s iter.m
Contenu du fichier s iter.m :

% SCRIPT s i t e r .m
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% March 20 0 2

10

12

close a l l
fun=f_test ;
a =1;
b=3.9;
x0 = 1 . 1 ;
nmax=120000;

Page 35

Syllabus Matlab

70

t o l l =1e 6;
14

16

18

% meth : 1 f o r corde , 2 f o r s e c a n t , 3 f o r r e g u l a f a l s i , 4 Newton


meth=4;
[ xvect , x d i f , fx , n i t ]= r o o t i t e r ( a , b , meth , nmax , t o l l , fun , 1 )

69

s jacobi2.m
Contenu du fichier s jacobi2.m :

% SCRIPT s j a c o b i 2
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

clear a l l
n=3;
A= [ 3 1 1 ; 2 6 2 ; 1 2 4 ] ;
b=[3; 10 ; 7];

14

D=diag ( diag (A ) ) ;
P=D;
N=DA;

16

x ( : , 1 ) = zeros ( n , 1 ) ;

12

18

20

22

24

f o r i =2:20
x ( : , i )=inv (P) (Nx ( : , i 1)+b ) ;
end
figure (1)
plot ( x )

70

s jacobi.m
Contenu du fichier s jacobi.m :

% SCRIPT s j a c o b i
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

clear a l l
n=4;
A= [ 5 7 6 5 ; 7 1 0 8 7 ; 6 8 1 0 9 ; 5 7 9 1 0 ] ;
b=[23; 32 ; 33; 31];

14

D=diag ( diag (A ) ) ;
P=D;
N=DA;

16

x=zeros ( n , 1 ) ;

12

18

f o r i =1:10

Page 36

S JACOBI.M

Syllabus Matlab

x=inv (P) (Nx+b )


pause

20

22

72

end

71

s lag.m
Contenu du fichier s lag.m :

clear a l l
close a l l
hold o f f
X= [ 3 : 1 : 3 ] ;
Y=X. 3 ;

Y=log (X.2+1)
x = 3:.01:3;
y=log ( x .2+1)

10

yp=p o l l a g (X, Y, x ) ;
12

plot ( x , y )
14

16

18

hold
plot (X, Y, k* )
plot ( x , yp , g )
legend ( funct , points , interp )

72

s least2.m
Contenu du fichier s least2.m :

clear a l l
close a l l
hold o f f
Xp = [ 1 3 4 6 7 ] ;
Yp=[ 2.1 0.9 0.6
x =1:7;
m=1;

0.6

0.9];

10

12

14

f o r ( i =1: length (Xp ) )


f o r ( j =1:m+1)
X( i , j )=Xp( i ) ( j 1);
end
Y( i ,1)=Yp( i ) ;
end

16

a=pinv (X) Y;
18

20

22

24

26

28

y=zeros ( s i z e ( x ) ) ;
f o r ( j =1:m+1)
y=y+a ( j ) . x . ( j 1);
end
plot (Xp , Yp , * )
hold
plot ( x , y )
xlabel ( x )
ylabel ( y )
t i t l e ( [ Polynomial order m= , num2str (m) ] ) ;

Page 37

S LEAST2.M

Syllabus Matlab

73

75

s least.m
Contenu du fichier s least.m :

clear a l l
close a l l
hold o f f
Xp = [ 0 : . 5 : 1 . 5 ] ;
Yp=sin (Xp ) ;
x=0:.01:1.5;
m=2;

10

12

14

f o r ( i =1: length (Xp ) )


f o r ( j =1:m+1)
X( i , j )=Xp( i ) ( j 1);
end
Y( i ,1)=Yp( i ) ;
end

16

a=pinv (X) Y;
18

20

22

24

26

28

y=zeros ( s i z e ( x ) ) ;
f o r ( j =1:m+1)
y=y+a ( j ) . x . ( j 1);
end
plot (Xp , Yp , * )
hold
plot ( x , y )
xlabel ( x )
ylabel ( y )
t i t l e ( [ Polynomial order m= , num2str (m) ] ) ;

74

s LotkaVolterra.m
Contenu du fichier s LotkaVolterra.m :

clear a l l
close a l l
simtime=input ( enter runtime = ) ;

initx =[5000 100] ;


[ t , x]=ode23 ( fLV , [ 0 , simtime ] , i n i t x ) ;
plot ( t , x ) ;
xlabel ( time ) ;
ylabel ( population ) ;

10

12

legend ( Rabbits , Foxes )

75

s lu.m
Contenu du fichier s lu.m :

% SCRIPT s l u
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

Page 38

S LU.M

Syllabus Matlab

76

A= [ 4 , 8 , 1 2 ; 3 8 1 3 ; 2 9 1 8 ] ;
8

LU=l u k j i (A ) ;
10

14

L=t r i l (LU, 1 ) ;
f o r ( i =1: s i z e (A, 1 ) )
L( i , i )=1;
end

16

U=t r i u (LU ) ;

18

b=[4;5;11];
y=f o r w a r d c o l (L , b )

12

20

x=b a c k w a r d c o l (U, y )

76

s mil stababs.m
Contenu du fichier s mil stababs.m :

clear a l l
close a l l
hold o f f
T=5;
t = 0 : . 1 :T ;
lambda=5;
plot ( t , exp ( lambda t ) ) ;
h=0.05;

10

12

c o n t =1;
y ( c o n t )=1;
yt ( c o n t )=0;

14

16

y ( c o n t+1)=y ( c o n t )+h lambda y ( c o n t ) ;


yt ( c o n t+1)=h ;
c o n t =2;

18

roots ( [ 1 2 h lambda 1 ] )
20

22

24

26

28

h lambda
pause
f o r ( th =(2h ) : h :T)
yt ( c o n t+1)=th ;
y ( c o n t+1)=y ( cont 1)+2h lambda y ( c o n t ) ;
c o n t=c o n t +1;
end

30

hold
32

34

plot ( yt , y , r )
t i t l e ( [ h= num2str ( h ) ,
xlabel ( time )

h* Lambda = num2str ( h lambda ) ] )

Page 39

S MIL STABABS.M

Syllabus Matlab

77

80

S PCA.M

s nodal.m
Contenu du fichier s nodal.m :

clear a l l
close a l l
hold o f f
X= [ 3 : 1 : 3 ] ;
x= 3.4:.01:3.4;

plot ( x , zeros ( s i z e ( x ) ) , k )
hold
plot ( x , ( xX( 1 ) ) . ( xX( 2 ) ) . ( xX( 3 ) ) . ( xX( 4 ) ) . ( xX( 5 ) ) . ( xX( 6 ) ) . ( xX( 7 ) ) , b )

78

sol an2.m
Contenu du fichier sol an2.m :

function y=s o l a n 2 ( t )
global lambda
y=exp ( lambda t ) ;

79

sol an.m
Contenu du fichier sol an.m :

function y=s o l a n ( t )
2

y=3exp( t )2 t +2;

80

s pca.m
Contenu du fichier s pca.m :

% SCRIPT s p c a
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% March 20 0 2

10

12

14

16

18

20

22

clear a l l
close a l l
cm = [ 0 : 1 / 2 5 6 : 1 ; 0 : 1 / 2 5 6 : 1 ; 0 : 1 / 2 5 6 : 1 ] ;
colormap (cm )
a=d o u b l e ( imread ( alf.jpg ) ) ;
[m, n]= s i z e ( a ) ;
c o m p l o r i g=s i z e ( a , 1 ) s i z e ( a , 2 ) ;
image ( a ) ;
t i t l e ( [ Original uncompressed image . Size= num2str ( c o m p l o r i g ) Normalised =1 ] )
pause
me=mean( a ) ;
C=ao n e s (m, 1 ) me ;
[ v e c t e u r s , v a l e u r s ]= eig (C C ) ;
DD=diag ( v a l e u r s ) ;
[ o , ord ]= sort(DD) ;

24

v a l e u r s o r d=DD( ord ) ;

Page 40

Syllabus Matlab

26

82

S POL LAG.M

v e c t e u r s o r d=v e c t e u r s ( : , ord ) ;

28

for h =1:10: n
30

32

a compr=C v e c t e u r s o r d ( : , 1 : h ) ;
[ m compr , n compr ]= s i z e ( a compr ) ;
compl=m compr n compr ;

34

a decompr=(a compr v e c t e u r s o r d ( : , 1 : h ) ) ;
36

38

40

image ( a decompr+o ne s (m, 1 ) me)


t i t l e ( [ Compressed image . Size= num2str ( compl ) Normalised = num2str ( compl / c o m p l o r i g ) ] )
pause
end

81

spcub.m
Contenu du fichier spcub.m :

function [ s , A, b]= spcub (X, Y, x )


2

n=length (X) 1;
4

f o r ( i =1:n )
h ( i )=X( i +1)X( i ) ;
end

10

12

14

16

18

A=zeros ( n+1,n +1);


b=zeros ( n + 1 , 1 ) ;
f o r ( i =1:n1)
A( i , i )=h ( i ) / 6 ;
A( i , i +1)=(h ( i )+h ( i + 1 ) ) / 3 ;
A( i , i +2)=h ( i +1)/6;
b ( i )=(Y( i +2)Y( i +1))/h ( i +1)(Y( i +1)Y( i ) ) / h ( i ) ;
end
A( n , 1 ) = 1 ;
A( n+1,n+1)=1;

20

22

24

26

28

30

M=A\b ;
s =[];
f o r ( i =1:n )
Ct=Y( i )M( i ) h ( i ) 2 / 6 ;
C=(Y( i +1)Y( i ) ) / h ( i )h ( i ) / 6 (M( i +1)M( i ) ) ;
i f ( i <n )
xs=x ( x>=X( i ) & x<X( i + 1 ) ) ;
else
xs=x ( x>=X( i ) & x<=X( i + 1 ) ) ;
end
s =[ s M( i ) (X( i +1)xs ) . 3 / ( 6 h ( i ))+M( i +1)( xsX( i ) ) . 3 / ( 6 h ( i ))+C ( xsX( i ))+Ct ] ;

32

end

82

s pol lag.m
Contenu du fichier s pol lag.m :

Page 41

Syllabus Matlab

hold o f f
X= [ 3 : 1 : 3 ] ;

x = 3:.01:3;

83

S POL NEW2.M

on=o n e s ( s i z e ( x ) ) ;
f o r ( i =1: length (X) )

p=on ;
f o r ( j =1: length (X) )

10

12

14

16

18

20

22

i f ( j = i )
p=p . ( xX( j ) ) . / ( X( i )X( j ) ) ;
end
end
plot ( x , p )
i f ( i ==1)
hold on
end
plot ( x , zeros ( s i z e ( x ) ) ) ;
plot (X( i ) , 1 , k* ) ;
pause
end

83

s pol new2.m
Contenu du fichier s pol new2.m :

10

12

14

clear a l l
close a l l
hold o f f
X= [ 0 : . 5 : 2 ] ;
Y=sin (X ) ;
x=0:.01:2;
d=d i v i d i f (X , Y ) ;
n=length (X ) ;
y=d ( 1 , 1 )
f o r ( i =2:n )
p=(xX ( 1 ) ) ;
f o r ( j =2: i 1)
p=p . ( xX( j ) ) ;
end

16

18

20

22

24

26

28

y=y+pd ( i , i ) ;
end
%p l o t ( x , y )

plot ( x , 0 . 9 5 8 9 x 0.2348 x . ( x 0.5) 0.1182 x . ( x 0 . 5 ) . ( x 1)+0.0336 x . ( x 0 . 5 ) . ( x 1 ) . ( x


hold
plot (X, Y, k* )
t i t l e ( Divided differences : interpolating polynomial )
xlabel ( x )
ylabel ( f(x) )

Page 42

Syllabus Matlab

84

85

s pol new.m
Contenu du fichier s pol new.m :

10

12

14

clear a l l
close a l l
hold o f f
X= [ 3 : 1 : 3 ] ;
Y=X. 2 ;
x = 3:.01:3;
d=d i v i d i f (X , Y ) ;
n=length (X ) ;
y=d ( 1 , 1 )
f o r ( i =2:n )
p=(xX ( 1 ) ) ;
f o r ( j =2: i 1)
p=p . ( xX( j ) ) ;
end

16

18

20

22

y=y+pd ( i , i ) ;
end
plot ( x , y )
hold
plot (X, Y, k* )

85

s power.m
Contenu du fichier s power.m :

% SCRIPT s p o w e r
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% March 20 0 2

10

12

clear a l l
close a l l
hold o f f
A= [ 2 1 7 1 ; 5 7 7 ; 4 4 2 0 ] ;
z0 = [ 1 , 1 , 1 ] ;
nmax=10000;
t o l l =1e 4;

14

[ nu1 , x1 , n i t e r , e r r ]=powerm (A, z0 , t o l l , nmax )


16

18

20

22

24

26

[ sigma , x , n i t e r , e r r ]= invpower (A, z0 , 0 , t o l l , nmax ) ;


plot ( nu1 )
hold
plot ( sigma , r )
xlabel ( Iterations )
ylabel ( Extremes eigenvalues )
axis ( [ 1 n i t e r 0 2 5 ] )

Page 43

S POWER.M

Syllabus Matlab

86

87

spy lu kji.m
Contenu du fichier spy lu kji.m :

function [ A] = s p y l u k j i (A)
[ n , n]= s i z e (A ) ;
f o r k=1:n1
A( k+1:n , k)=A( k+1:n , k ) /A( k , k ) ;
f o r j=k+1:n ,
f o r i=k+1:n
A( i , j )=A( i , j )A( i , k ) A( k , j ) ;

10

12

14

end
end
spy (A ) ;
t i t l e ( [ Iteration no. num2str ( k ) ] )
pause
end
return

87

s rbf2.m
Contenu du fichier s rbf2.m :

clear a l l
close a l l
n=400;
Xp=4rand ( n , 2 ) 2 ;
Yp=model2 (Xp ( : , 1 ) , Xp ( : , 2 ) ) ;
[ mx, my] = meshgrid (Xp , Yp ) ;

mz=model2 (mx, my ) ;

10

12

mesh(mx, my, mz)


f o r mx=2:8
hold o f f

14

16

qq=linspace ( 2 , 2 , 3 2 ) ;
[ Q1 , Q2]=meshgrid ( qq ) ;
x=[Q1 ( : ) Q2 ( : ) ] ;

18

20

22

24

26

28

30

32

34

36

z=model2 ( x ( : , 1 ) , x ( : , 2 ) ) ;
Z=reshape ( z , 3 2 , 3 2 ) ;
figure ( 2 )
mesh(Q1 , Q2 , Z)
D=x ( end)x ( 1 ) ;
d e l t a=D/ (mx ) ;
c c=linspace ( 2 ,2 ,mx ) ;
[ C1 , C2]=meshgrid ( c c ) ;
C=[C1 ( : ) C2 ( : ) ]
;
B=d e l t a o n e s ( s i z e (C, 1 ) , 1 ) ;
m=s i z e (C, 1 )
f o r ( i =1: length (Xp ) )
f o r ( j =1:m+1)
i f ( j ==1)
X( i , j )=1;
else
X( i , j )= kern2 (Xp( i , : ) , C( j 1 , : ) ,B( j 1 ) ) ;
end

Page 44

S RBF2.M

Syllabus Matlab

88

38

end
Y( i ,1)=Yp( i ) ;
end

40

42

44

a=pinv (X) Y;

46

z r b f=zeros ( s i z e ( x , 1 ) , 1 ) ;

48

f o r ( i =1: length ( x ) )
z r b f ( i )=a ( 1 ) ;
f o r ( j =1:m)
z r b f ( i )= z r b f ( i )+a ( j +1) kern2 ( x ( i , : ) , C( j , : ) , B( j ) ) ;

50

52

end
end

54

56

58

RMSE rbf=sqrt (sum( ( zz r b f ) . 2 ) / length ( z ) )


figure ( 1 )

60

Z r b f=reshape ( z r b f , 3 2 , 3 2 ) ;
mesh(Q1 , Q2 , Z r b f )

62

64

pause

66

end

88

s rbf.m
Contenu du fichier s rbf.m :

clear a l l
close a l l
Xp = 0 : . 1 : 1 ;
Yp=model (Xp)+0.0 randn ( s i z e (Xp ) ) ;
f o r m=2: length (Xp)
hold o f f

10

12

%p l o t ( x ,0.1+(1+ s i n (2 x +6))./(3.5+ s i n ( x 2)))


%Yp=s i n (Xp ) ;
%y=x ;
%p l o t ( y ,0.1+(1+ s i n (3 y +2))./(3.5+ s i n (2y ) ) )

14

16

18

20

x=0:.01:1.0;
y=model ( x ) ;
D=x ( end)x ( 1 ) ;
d e l t a=D/ (m1);
B=d e l t a o n e s (m, 1 ) ;

22

C=x ( 1 ) : d e l t a : x ( end )
24

Page 45

S RBF.M

Syllabus Matlab

89

f o r ( i =1: length (Xp ) )


f o r ( j =1:m+1)
i f ( j ==1)
X( i , j )=1;
else
X( i , j )= kern (Xp( i ) ,C( j 1) ,B( j 1 ) ) ;
end

26

28

30

32

end
Y( i ,1)=Yp( i ) ;
end

34

36

38

a=pinv (X) Y;
40

y r b f=zeros ( s i z e ( x ) ) ;
42

f o r ( i =1: length ( x ) )
y r b f ( i )=a ( 1 ) ;
f o r ( j =1:m)
y r b f ( i )= y r b f ( i )+a ( j +1) kern ( x ( i ) ,C( j ) ,B( j ) ) ;

44

46

end
end

48

50

plot (Xp , Yp , * )
hold
plot ( x , y r b f )
xlabel ( x )
ylabel ( y )

52

54

56

58

[ y p o l , a]= l s p o l (Xp , Yp ,m, x ) ;


RMSE rbf=sqrt (sum( ( yy r b f ) . 2 ) / length ( y ) ) ;
RMSE pol=sqrt (sum( ( yy p o l ) . 2 ) / length ( y ) ) ;

60

62

t i t l e ( [ Number of basis functions m=num2str (m ) ] ) ;


64

66

legend ( Points , RBF ) ;


pause
end

89

s refine.m
Contenu du fichier s refine.m :

% SCRIPT s r e f i n e
2

10

12

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002
clear a l l
global b a s e
global t
b a s e =2;
t =15;
n=4;
rand ( state , 0 )

Page 46

S REFINE.M

Syllabus Matlab

14

90

A= [ 5 7 6 5 ; 7 1 0 8 7 ; 6 8 1 0 9 ; 5 7 9 1 0 ] ;
b=[23; 32 ; 33; 31];

16

18

x=f L U s o l v e (A, b )
r=bAx ;

20

22

24

dx=f L U s o l v e (A, r ) ;
r=bA ( x+dx ) ;
x+dx
pause

s rk2.m

90

Contenu du fichier s rk2.m :

clear a l l
close a l l
hold o f f
t =0:.01:0.5;

y=f e v a l ( sol_an , t ) ;

plot ( t , y )
hold

10

12

14

16

18

for ( h =0.1 )
clear y hat ;
s t e p =1;
y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t =0:h : 0 . 5 )
y h a t ( s t e p +1)= y h a t ( s t e p )+h d e r a n ( t , y h a t ( s t e p ) ) ;
s t e p=s t e p +1;
end
plot ( 0 : h : 0 . 5 , y h a t ( 1 : end1) , r -* )

20

22

24

26

28

30

clear y hat ;
s t e p =1;
y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t =0:h : 0 . 5 )
c2 =1;
b1 =1/2;
b2 =1/2;
K1=d e r a n ( t , y h a t ( s t e p ) ) ;
K2=d e r a n ( t+h c2 , y h a t ( s t e p )+h c2 K1 ) ;

32

y h a t ( s t e p +1)= y h a t ( s t e p )+h ( b1K1+b2K2 ) ;


s t e p=s t e p +1;

34

end
36

plot ( 0 : h : 0 . 5 , y h a t ( 1 : end1) , k -* )
38

40

42

end
xlabel ( t )
ylabel ( y(t) )

44

Page 47

S RK2.M

Syllabus Matlab

92

S RK4.M

legend ( Analytical , Euler h=0.1 , RK2 h=0.1 )

91

s rk2 stab.m
Contenu du fichier s rk2 stab.m :

clear a l l
close a l l
global lambda

10

12

14

T=15;
f o r ( h = 0 . 1 : 0 . 1 :T)
t = 0 : . 0 1 :T ;
hold o f f
lambda=1;
R=abs(1+h lambda +0.5( h lambda ) 2 ) ;
i f ( R>=1)
f p r i n t f ( 1 , Unstable ) ;
else
f p r i n t f ( 1 , Stable \n ) ;

16

end
18

20

y=f e v a l ( sol_an2 , t ) ;
22

24

26

28

30

32

34

plot ( t , y )
hold
clear y hat ;
s t e p =1;
y h a t ( s t e p )= s o l a n 2 ( 0 ) ;
f o r ( t =0:h : T)
c2 =1;
b1 =1/2;
b2 =1/2;
K1=d e r a n 2 ( t , y h a t ( s t e p ) ) ;
K2=d e r a n 2 ( t+h c2 , y h a t ( s t e p )+h c2 K1 ) ;

36

y h a t ( s t e p +1)= y h a t ( s t e p )+h ( b1K1+b2K2 ) ;


s t e p=s t e p +1;

38

end
40

plot ( 0 : h : T, y h a t ( 1 : end1) , k-* )


42

44

46

48

xlabel ( t )
ylabel ( y(t) )
t i t l e ( [ Runge -Kutta 2nd order:h = num2str ( h )
pause
end

92

s rk4.m
Contenu du fichier s rk4.m :
Page 48

|R(h \ lambda )|= num2str (R ) ] ) ;

Syllabus Matlab

clear a l l
close a l l
hold o f f
t =0:.01:0.6;

y=f e v a l ( sol_an , t ) ;

plot ( t , y )
hold

93

10

12

14

16

18

20

for ( h =0.15 )
clear y hat ;
s t e p =1;
y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t =0:h : 0 . 6 )
c2 =1;
b1 =1/2;
b2 =1/2;
K1=d e r a n ( t , y h a t ( s t e p ) ) ;
K2=d e r a n ( t+h c2 , y h a t ( s t e p )+h c2 K1 ) ;
y h a t ( s t e p +1)= y h a t ( s t e p )+h ( b1K1+b2K2 ) ;
s t e p=s t e p +1;

22

24

end
plot ( 0 : h : 0 . 6 , y h a t ( 1 : end1) , r -* )

26

28

30

32

34

36

38

clear y hat ;
s t e p =1;
y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t =0:h : 0 . 6 )
c2 =1;
b1 =1/2;
b2 =1/2;
K1=d e r a n ( t , y h a t ( s t e p ) ) ;
K2=d e r a n ( t+h / 2 , y h a t ( s t e p )+h/2K1 ) ;
K3=d e r a n ( t+h / 2 , y h a t ( s t e p )+h/2K2 ) ;
K4=d e r a n ( t+h / 2 , y h a t ( s t e p )+hK3 ) ;
y h a t ( s t e p +1)= y h a t ( s t e p )+h / 6 (K1+2K2+2K3+K4 ) ;
s t e p=s t e p +1;

40

42

end
plot ( 0 : h : 0 . 6 , y h a t ( 1 : end1) , k -* )

44

46

48

end
xlabel ( t )
ylabel ( y(t) )

50

legend ( Analytical , RK2 h=0.2 , RK4 h=0.2 )

93

s runge.m
Contenu du fichier s runge.m :

close a l l
clear a l l
hold o f f
X= [ 3 : 0 . 5 : 3 ] ;

Page 49

S RUNGE.M

Syllabus Matlab

95

Y=1./(1+X . 2 ) ;
x = 3:.01:3;
y=1./(1+x . 2 ) ;

yp=p o l l a g (X, Y, x ) ;
10

plot ( x , y )
12

14

16

hold
plot (X, Y, k* )
plot ( x , yp , g )
legend ( funct , points , interp )

94

s ru spl.m
Contenu du fichier s ru spl.m :

clear a l l
close a l l
hold o f f
X= [ 3 : . 1 : 3 ] ;
Y=X. 3 ;
Y=1./(1+X . 2 ) ;
x = 3:.01:3;
y=1./(1+x . 2 ) ;

10

ys=spcub (X, Y, x )
12

plot ( x , y )
14

16

18

hold
plot (X, Y, k* )
plot ( x , ys , g )
legend ( funct , points , spline )

95

s spline2.m
Contenu du fichier s spline2.m :

clear a l l
close a l l
hold o f f
X= [ 0 : . 5 : 2 ] ;
Y=sin (X ) ;

x=0:.01:2;
y=sin ( x ) ;
[ ys , A, b]= spcub (X, Y, x )

10

plot ( x , y )
12

14

16

hold
plot (X, Y, k* )
plot ( x , ys , g )
legend ( funct , points , spline )

Page 50

S SPLINE2.M

Syllabus Matlab

96

98

s spline.m
Contenu du fichier s spline.m :

clear a l l
close a l l
hold o f f
X= [ 3 : . 3 : 3 ] ;
Y=X. 3 ;

x = 3:.01:3;
y=x . 3 ;

10

ys=spcub (X, Y, x )

12

plot ( x , y )

14

hold
plot (X, Y, k* )
plot ( x , ys , g )
legend ( funct , points , spline )

16

s stable.m

97

Contenu du fichier s stable.m :


% SCRIPT s s t a b l e
2

10

12

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002
clear a l l
close a l l
hold o f f
N=10;
E(N) = 0 . 0 ;
f o r ( i=N: 1 : 2 )
E( i 1)=(1E( i ) ) / i ;
end

14

16

18

20

plot (E)
E(N)=0.0+1 e 3;
f o r ( i=N: 1 : 2 )
E( i 1)=(1E( i ) ) / i ;
end

22

hold
24

26

plot (E , r )
xlabel ( No . iterations )
ylabel ( E )

98

s taylor.m
Contenu du fichier s taylor.m :

clear a l l
close a l l

Page 51

S TAYLOR.M

Syllabus Matlab

100

hold o f f
t =0:.01:0.5;

y=f e v a l ( sol_an , t ) ;

plot ( t , y )
hold

10

plot ( t ,1+1 t 1.5 t . 2 + 0 . 5 t . 3 , r-- )


12

legend ( Analytical , Taylor )

s triang.m

99

Contenu du fichier s triang.m :


% SCRIPT s t r i a n g
2

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002

10

12

14

A= [ 2 0 0 ; 4 2 0 ;
b=[4;2; 4];
f o r w a r d c o l (A, b )
A= [ 2 0 0 ; 4 2 0 ;
b=[4;2; 4];
f o r w a r d r o w (A, b )

4 ,8 , 12];
% Quarteroni s book
4 ,8 , 12];
% Quarteroni s book

16

18

A= [ 4 , 8 , 1 2 ; 0 2 4 ; 1 0 2 ] ;
b=[4;2;4];
b a c k w a r d c o l (A, b ) % Quarteroni s book

100

s unstable2.m

Contenu du fichier s unstable2.m :


2

clear a l l
close a l l
hold o f f
X= [ 1 : . 1 : 0 . 5 ] ;
randn ( state , 0 ) ;
Y=sin ( 2 pi X)+0.1 randn ( s i z e (X ) ) ;
x= 1:.01:0.5;
y=sin ( 2 pi x ) ;
yp=p o l l a g (X, Y, x ) ;

10

f o r m=1: length (X)1


12

[ y l s , a]= l s p o l (X, Y,m, x ) ;


14

plot ( x , y )
16

18

hold
plot (X, Y, k* )
plot ( x , yp , g )

Page 52

S UNSTABLE2.M

Syllabus Matlab

102

20

plot ( x , y l s , k )

22

legend ( funct , points , interp , least squares , 4 )


t i t l e ( [ Least - squares order = num2str (m) ] )
axis ( [ 1 0 . 5 3 2 . 5 ] )
pause

24

26

end

101

s unstable.m

Contenu du fichier s unstable.m :


% SCRIPT s u n s t a b l e
2

10

12

% Cours de C a l c u l e numrique MATH 0 3 1


% C o p y r i g h t ( c ) Gianluca Bontempi , DIULB
% January 2002
clear a l l
close a l l
hold o f f
N=10;
E( 1 ) = 0 . 3 6 7 8 7 9 ;
f o r ( i =1:N)
E( i +1)=1( i +1)E( i ) ;
end

14

16

18

20

plot (E)
E(1)=E(1)+1 e 6;
f o r ( i =1:N)
E( i +1)=1( i +1)E( i ) ;
end

22

hold
24

26

plot (E , r )
xlabel ( No . iterations )
ylabel ( E )

TP
102

ex5.m

Contenu du fichier ex5.m :


2

function ex5 (A, B)


%ex5 : e r a l i s e l a s o l u t i o n de l e x e r c i c e
%A f f i c h e l a somme , p u i s l a e d i f f r e n c e e t e n f i n l e p r o d u i t de A e t B
disp ( Somme des deux matrices )
AB
disp ( e Diffrence : A-B )
AB
disp ( Produit A*B )
AB

Page 53

EX5.M

Syllabus Matlab

103

105

EX8.M

ex6.m

Contenu du fichier ex6.m :


2

% S c r i p t a f f i c h a n t s u r l e meme g r a p h e l e s deux f o n c t i o n s x . x . c o s ( x )
% et x . x . sin (x)
x = 2:0.1:2;
y1=x . x . cos ( x ) ;
y2=x . x . sin ( x ) ;
plot ( x , y1 )
hold ;
plot ( x , y2 )
hold ;

104

ex7.m

Contenu du fichier ex7.m :


2

10

12

14

function r=ex7 ( n )
% F o n c t i o n e r s o l v a n t l e q u a t i o n x . x x 1
% n e s t l e Nombre d e i t r a t i o n edemands .
x=2;
x p r e c =2;
f o r ( i =1:n )
x p r e c=x ;
x=1+1./x ;
end
i f abs ( xx p r e c ) <0.0005
disp ( e Prcision satisfaisante )
else
disp ( e Prcision insatisfaisante )
end
r=x ;

105

ex8.m

Contenu du fichier ex8.m :


2

10

12

14

16

18

function r=ex8 (A)


% F o n c t i o n c a l c u l a n t l a somme de chacune d e s c o l o n n e s de A
% A d o i t e t r e une m a t r i c e ( 4 5 )
temp = [ ] ;
disp ( En utilisant deux for e imbriqus )
f o r ( i =1:5)
somme=0;
f o r ( j =1:4)
somme=somme+A( j , i ) ;
end
temp=[temp , somme ] ;
end
temp2 = [ ] ;
disp ( En utilisant la fonction sum )
f o r ( i =1:5)
temp2=[temp2 ,sum(A( : , i ) ) ] ;
end
r=temp ;

Page 54

Syllabus Matlab

106

109

EX12.M

ex9.m

Contenu du fichier ex9.m :


2

10

12

14

16

18

function r=ex9 (X, k )


% S c r i p t de l e x e r c i c e 9
[ l , c ]= s i z e (X ) ;
i f k>c | k<1
disp ( k est hors des limites du vecteur )
e l s e i f l >1
disp ( Cette fonction ne fonctionne qu avec des vecteurs , pas des matrices )
else
p r o d u i t =1;
f o r ( i =1:k1)
p r o d u i t=p r o d u i t . X( 1 , i ) ;
end
f o r ( i=k+1: c )
p r o d u i t=p r o d u i t . X( 1 , i ) ;
end
r=p r o d u i t ;
end

107

ex10.m

Contenu du fichier ex10.m :


2

function r=ex10 ( c , d , t )
% blah , blah
D=o n e s ( 1 , t ) . d ;
C=o n e s ( 1 , t 1). c ;
A=diag (D) ;
A=A+diag (C, 1 ) ;
A=A+diag (C , 1 ) ;
r=A;

108

ex11.m

Contenu du fichier ex11.m :


2

function [ x1 , x2 ]= ex11 ( a , b , c )
% C e t t e f o n c t i o n e r s o u t une q u t a t i o n du `e deuxime e d e g r
% ax . x+bx+c =0 en p r e n a n t comme e donne s e s c o e f f i c i e n t s a , b e t c
% Les s o l u t i o n s s o n t e r e n v o y e s dans x1 e t x2
d=b . b4a c ;
x1=(b+sqrt ( d ) ) / ( 2 a ) ;
x2=(bsqrt ( d ) ) / ( 2 a ) ;

109

ex12.m

Contenu du fichier ex12.m :


2

function r=ex12 (A, n )


% n e s t l e nombre d e i t r a t i o n s
% on u t i l i s e r a l a f o n c t i o n e x e r c i c e 1 0 pour ee g n r e r l a m a t r i c e
B=o n e s ( s i z e (A ) ) ;
Aexposant=o ne s ( s i z e (A ) ) ;
f o r ( i =1:n )

Page 55

Syllabus Matlab

Aexposant = Aexposant A;
B=B+Aexposant ;

10

113

end ;
r=B ;

Autres
110

gacol.m

Contenu du fichier gacol.m :


2

function [M]= g a c o l (M, c , s , j1 , j2 , i , k )


f o r j=j 1 : j 2
t 1=M( j , i ) ;
t 2=M( j , k ) ;
M( j , i )=c t1s t 2 ;
M( j , k)= s t 1+c t 2 ;
end
return

111

garow.m

Contenu du fichier garow.m :


2

function [M]= garow (M, c , s , i , k , j1 , j 2 )


f o r j=j 1 : j 2
t 1=M( i , j ) ;
t 2=M( k , j ) ;
M( i , j )=c t1s t 2 ;
M( k , j )= s t 1+c t 2 ;
end
return

112

givcos.m

Contenu du fichier givcos.m :


2

10

12

function [ c , s ]= g i v c o s ( xi , xk )
i f ( xk ==0) , c = 1 ; s = 0 ; else ,
i f abs ( xk ) > abs ( x i )
t=x i /xk ;
s =1/sqrt (1+ t 2 ) ;
c=s t ;
else
t=xk/ x i ;
c=1/sqrt (1+ t 2 ) ;
s=c t ;
end
end
return

113

gseid.m

Contenu du fichier gseid.m :

Page 56

GSEID.M

Syllabus Matlab

115

HOUSHESS.M

function X=g s e i d (A, B, P , d e l t a , max1 )


% I n p u t A i s an N x N n o n s i n g u l a r m a t r i x
%
B i s an N x 1 m a t r i x
%
P i s an N x 1 m a t r i x ; t h e i n i t i a l g u e s s
%
d e l t a i s t h e t o l e r a n c e f o r P
%
max1 i s t h e maximum number o f i t e r a t i o n s
% OutputX i s an N x 1 m a t r i x : t h e j a c o b i a p p r o x i m a t i o n t o
%
t h e s o l u t i o n s o f AX= B

10

N= length (B ) ;

12

f o r k=1:max1
f o r j =1:N
i f j==1
X(1)=(B(1) A( 1 , 2 :N) P ( 2 :N) ) /A( 1 , 1 ) ;
e l s e i f j==N
X(N)=(B(N)A(N, 1 : N1)(X( 1 : N 1 ) ) ) /A(N,N ) ;
else
% X c o n t a i n s t h k t h a p p r o x i m a t i o n s and P t h ( k 1) s t
X( j )=(B( j )A( j , 1 : j 1)X( 1 : j 1)A( j , j +1:N) P( j +1:N) ) /A( j , j ) ;
end
end
e r r=abs (norm(XP ) ) ;
r e l e r r=e r r / (norm(X)+eps ) ;
P=X ;
i f ( e r r <d e l t a ) | ( r e l e r r <d e l t a )
break
end
end
X=X ;

14

16

18

20

22

24

26

28

30

114

hessqr.m

Contenu du fichier hessqr.m :


2

10

function [ T, Q,R]= h e s s q r (A, n i t e r )


n=max( s i z e (A ) ) ;
[ T, Qhess ]= h o u s h e s s (A ) ;
f o r j =1: n i t e r
[ Q, R, c , s ] = q r g i v e n s (T ) ;
T=R;
f o r k=1:n1,
T=g a c o l (T, c ( k ) , s ( k ) , 1 , k+1,k , k +1);
end
end
return

115

houshess.m

Contenu du fichier houshess.m :


2

function [ H,Q]= h o u s h e s s (A)


n=max( s i z e (A ) ) ;
Q=eye ( n ) ;
H=A;
f o r k =1:( n 2) ,
[ v , beta]= vhouse (H( k+1:n , k ) ) ;
I=eye ( k ) ;
N=zeros ( k , nk ) ;

Page 57

Syllabus Matlab

10

12

14

16

118

m=length ( v ) ;
R=eye (m)betavv ;
H( k+1:n , k : n)=RH( k+1:n , k : n ) ;
H( 1 : n , k+1:n)=H( 1 : n , k+1:n ) R;
P=[ I , N ; N , R ] ;
Q=QP ;
end
return

116

jacobi.m

Contenu du fichier jacobi.m :


2

function X=j a c o b i (A, B, P , d e l t a , max1 )


% I n p u t A i s an N x N n o n s i n g u l a r m a t r i x
%
B i s an N x 1 m a t r i x
%
P i s an N x 1 m a t r i x ; t h e i n i t i a l g u e s s
%
d e l t a i s t h e t o l e r a n c e f o r P
%
max1 i s t h e maximum number o f i t e r a t i o n s
% OutputX i s an N x 1 m a t r i x : t h e j a c o b i a p p r o x i m a t i o n t o
%
t h e s o l u t i o n s o f AX= B

10

N= length (B ) ;

12

f o r k=1:max1
f o r j =1:N
X( j )=(B( j )A( j , [ 1 : j 1, j +1:N] ) P ( [ 1 : j 1, j +1:N] ) ) /A( j , j ) ;
end
e r r=abs (norm(XP ) ) ;
r e l e r r=e r r / (norm(X)+eps ) ;
P=X ;
i f ( e r r <d e l t a ) | ( r e l e r r <d e l t a )
break
end
end
X=X ;

14

16

18

20

22

117

prodgiv.m

Contenu du fichier prodgiv.m :


2

10

function Q=p r o d g i v ( c , s , n )
n1=n 1 ; n2=n2;
Q=eye ( n ) ; Q( n1 , n1)=c ( n1 ) ; Q( n , n)=c ( n1 ) ;
Q( n1 , n)= s ( n1 ) ; Q( n , n1)=s ( n1 ) ;
f o r k=n2 : 1 : 1 ,
k1=k + 1 ; Q( k , k)=c ( k ) ; Q( k1 , k)=s ( k ) ;
q=Q( k1 , k1 : n ) ; Q( k , k1 : n)= s ( k ) q ;
Q( k1 , k1 : n)=c ( k ) q ;
end
return

118

qrgivens.m

Contenu du fichier qrgivens.m :


2

function [ Q, R, c , s ] = q r g i v e n s (H)
[m, n]= s i z e (H ) ;

Page 58

QRGIVENS.M

Syllabus Matlab

120

f o r k=1:n1
[ c ( k ) , s ( k )]= g i v c o s (H( k , k ) ,H( k+1,k ) ) ;
H=garow (H, c ( k ) , s ( k ) , k , k+1,k , n ) ;
end
R=H ; Q=p r o d g i v ( c , s , n ) ;
return

119

secant.m

Contenu du fichier secant.m :


2

10

12

14

16

18

20

22

24

26

28

30

32

function [ xvect , x d i f , fx , n i t ]= s e c a n t (xm1 , x0 , nmax , t o l l , fun )


function [ xvect , x d i f , fx , n i t ]= s e c a n t (xm1 , x0 , nmax , t o l l , fun )
%x0 , xm1 : e donnes i n i t i a l e s
%nmax : nombre d e i t r a t i o n s maximum de l a e mthode
%t o l e t o l r a n c e du t e s t d e a r r t
%fun : l a f o n c t i o n dont on r e c h e r c h e une r a c i n e
x=xm1 ;
fxm1=eval ( fun ) ;
x v e c t =[x ] ;
f x =[fxm1 ] ;
x=x0 ;
f x 0=eval ( fun ) ;
x v e c t =[ x v e c t ; x ] ;
f x =[ f x ; f x 0 ] ;
e r r= t o l l +1;
n i t =0;
xdif =[];
while ( n i t < nmax & e r r > t o l l ) ,
n i t=n i t +1;
x=x0f x 0 ( x0xm1 ) / ( fx0fxm1 ) ;
x v e c t =[ x v e c t ; x ] ;
fnew=eval ( fun ) ;
f x =[ f x ; fnew ] ;
e r r=abs ( x0x ) ;
x d i f =[ x d i f ; e r r ] ;
xm1=x0 ;
fxm1=f x 0 ;
x0=x ;
f x 0=fnew ;
end
return

120

vhouse.m

Contenu du fichier vhouse.m :


2

10

function [ v , beta]= vhouse ( x )


n=length ( x ) ;
x=x/norm( x ) ;
s=x ( 2 : n ) x ( 2 : n ) ;
v=[1; x (2: n ) ] ;
i f ( s ==0) , beta =0;
else
mu=sqrt ( x (1)2+ s ) ;
i f ( x (1) <= 0)
v (1)= x(1) mu;
else

Page 59

VHOUSE.M

Syllabus Matlab

12

14

16

120

v(1)= s / ( x (1)+mu ) ;
end
beta=2v ( 1 ) 2 / ( s+v ( 1 ) 2 ) ;
v=v/v ( 1 ) ;
end
return

Page 60

VHOUSE.M

You might also like