syl matlab
TRANSCRIPT
-
8/19/2019 Syl Matlab
1/60
Matlab Syllabus
16 mai 2004
Table des matières
Cours 2
1 backward col.m 2
2 cout.m 2
3 der an2.m 2
4 der an.m 2
5 diff.m 2
6 dividif.m 3
7 f306.m 3
8 f arret.m 3
9 fix2real.m 3
10 fixpt.m 4
11 flo2real.m 5
12 floset.m 5
13 fLV.m 6
14 fnewton.m 6
15 forward col.m 7
16 forward row.m 7
17 f test.m 8
18 ftest.m 8
19 f test pt.m 8
20 fzero root.m 8
1
-
8/19/2019 Syl Matlab
2/60
Syllabus Matlab TABLE DES MATI ̀ERES
21 incr.m 8
22 interp ls.m 9
23 invpower.m 9
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
-
8/19/2019 Syl Matlab
3/60
Syllabus Matlab TABLE DES MATI ̀ERES
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
-
8/19/2019 Syl Matlab
4/60
Syllabus Matlab TABLE DES MATI ̀ERES
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
-
8/19/2019 Syl Matlab
5/60
Syllabus Matlab TABLE DES MATI ̀ERES
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
-
8/19/2019 Syl Matlab
6/60
Syllabus Matlab 5 DIFF.M
Cours
1 backward col.m
Contenu du fichier backward col.m :
function [ b]=backwar d col (U, b)2 [ n]=mat square (U);
l = min( diag ( abs ( U ) ) ) ;4 T=t r i u (U);
6 i f sum(sum(T ˜= U))>0error ( ’ E rr or : t he m at ri x i s n ot u pp er t ri ag ul ar ’ ) ;
8 end
10 i f l = = 0disp ( ’ E rr o r : t he m a tr i x i s s i ng u la r ’ ) ; b = [ ] ;
12 break
end14 f o r j = n: −1:2 ,
b( j )=b( j )/U( j , j ) ;16 b ( 1 : j −1)=b(1: j −1)−b ( j )∗U ( 1 : j −1 , j ) ;
end ;18 b ( 1 ) = b ( 1 ) /U ( 1 , 1 ) ;
return
2 cout.m
Contenu du fichier cout.m :
function f=cout (n )2 X=rand ( n , n ) ;
f l o p s ( 0 )4 l u k j i (X ) ;
6 f=f l o p s ;
3 der an2.m
Contenu du fichier der an2.m :
function y=der an ( t , y)2 global lambda
y=lambda∗y ;
4 der an.m
Contenu du fichier der an.m :
function y=der an ( t , y)2
y=−2∗t−y ;
5 diff.m
Contenu du fichier diff.m :
Page 6
-
8/19/2019 Syl Matlab
7/60
Syllabus Matlab 9 FIX2REAL.M
c lo se a l l
2 c l e ar a l l
y ( 1 ) = 1 ;4 y ( 2 ) = 1 ;
h = 0 . 0 0 1 ;6
f o r ( i =2:300 00)8 y ( i +1)=−2∗h∗y( i )+y ( i −1);
end
10
12 plot ( y )
6 dividif.m
Contenu du fichier dividif.m :
function [ d ]= d i v i d i f ( x , y )2 [n,m]= s i z e ( y ) ;
i f n = = 1 , n = m ; end4 n = n−1 ; d = zeros ( n +1 ,n + 1) ; d ( : , 1 ) = y ’ ;
f o r j = 2 : n+16 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 ) ) ;8 end
end
10 return
7 f306.m
Contenu du fichier f306.m :
function fv=f306 (x ) ;2
4 fv=x−co s ( x ) ;
8 f arret.m
Contenu du fichier f arret.m :
function [ y , dy]= f a r re t (x )2 eta=1e−9;
y=exp(−x)−eta ;4 dy=−exp(−x ) ;
9 fix2real.m
Contenu du fichier fix2real.m :
function r= f i x 2 r e a l ( x n , xm , b , s )2 % f i x 2 r e a l : from F ix ed P oi nt n ot a ti o n t o r e a l n ot a ti o n
% 4 % FIX2REAL(XN,XM, B, S )
% XN − [ 1 ,N ] v e ct o r o f d i g i t s b e f or e t he d ec im al p o in t 6 % XM − [ 1 ,M] v e ct o r o f d i g i t s a f t e r t he d ec im al p o in t
% B − [ 1 , 1 ] b a s e
Page 7
-
8/19/2019 Syl Matlab
8/60
Syllabus Matlab 10 FIXPT.M
8 % S − [ 1 , 1] signum : 0 f o r p o s i t i ve , 1 f o r n e g at i v e % Returns R [ 1 , 1] r e a l nummber i n d e c i m a l f orma t
10 % % N. B. xn ( 1 ) i s t he most s i g n i f i c a n t d i g i t
12
14 % Co ur s d e C a l c u l énumrique MATH 031% Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
16 % J a n u a r y 2 0 0 2
18 n=length (xn ) ;m=length (xm);
20 r=0;I=find ([ xn xm ]>= b ) ;
22 i f l en gt h ( I )>0error ( ’ wr on g i np ut : n o e le me nt o f x n a nd x m s ho ul d b e l ar ge r t ha n b ’ ) ;
24 end
26 i f ( s ˜=0 & s ˜=1)error ( ’ wr on g i np ut s : s ho ul d be 0 or 1 ’ ) ;
28 end
30
f o r ( i =1:n)32 r=r+xn ( i )∗b ˆ ( n−i ) ;
end
34 f o r ( i =1 :m)r=r+xm( i )∗bˆ(− i ) ;
36 end
38 r=r ∗(−1)ˆs ;
10 fixpt.m
Contenu du fichier fixpt.m :
function [ k , p , err , P]= f i x p t (g ,p0 , tol , max1)2
P(1)=p0 ;4 g r a p h i c = 1 ;
i f ( grap hic )6 zoom on
hold o f f 8 de lt a =2;
p l o t f u n ( g , p0−de lta , p0+delt a , ’-’ ) ; % 10 hold on
p l o t l i n e ( p0−d e l t a , p 0−del ta , p0+delt a , p0+delt a , ’ r -’ ) % p l o t y=x 12 d e l t a 2 = 0 . 2 ;
xlabel ( ’x ’ ) ;14 ylabel ( ’y(x)’ ) ;
pause
16 end
18 f o r k=2:max1P(k)=f e v a l (g ,P(k−1 ) ) ;
20
i f ( grap hic )22
plot (P(k−1) , f e v a l (g ,P(k−1 ) ) , ’* ’ )24 a xis ( [ min(P)−abs ( d e l t a 2 ∗min( P ) ) max(P)+abs ( d e l t a 2 ∗max( P ) ) . . .
Page 8
-
8/19/2019 Syl Matlab
9/60
Syllabus Matlab 12 FLOSET.M
min(P)−abs ( d e l t a 2 ∗min( P ) ) max(P)+abs ( d e l t a 2 ∗max(P) ) ] )26 t i t l e ( [ ’Fixed point iteration Iteration=’ num2str ( k −2 ) ] )
pause
28 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 ) , ’: ’ )
30 end
32 e r r =abs (P(k)−P( k−1 ) ) ;r e l e r r =e r r /( abs (P(k))+eps ) ;
34 p=P( k ) ;i f ( err
-
8/19/2019 Syl Matlab
10/60
Syllabus Matlab 14 FNEWTON.M
8 % L− [ 1 , 1 ] l o w e r b ou nd e x p on e n t % U − [ 1 , 1 ] u p pe r b ou nd e x p on e n t
10 % R et ur ns :% F − [ card , 1 ] s e t o f r e al n umbers r e pr es en te d by t h e n or ma li se d f l o a t i n g p oi nt
12 % card : c a r d i n a l i t y o f t he s e t F
14
% Co ur s d e C a l c u l énumrique MATH 03116 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 18
F ( 1 , 1 ) = 0 ; % z e ro20 f =1;
22 f o r ( i =1: t )in v b (1 , i )=bˆ(− i ) ;
24 end
26 f o r ( s = [ 0 , 1 ] ) % 2 i t e r at i o n s f o r ( e=L:U) % (U −L + 1 ) i t e r a t i o n s
28 a=zeros ( t , 1 ) ;a ( 1 ) = 1 ;
30 f=f +1;F( f ,1)= ( −1 ) ˆ s ∗(bˆe )∗ ( i n v b ∗a ) ;
32 f o r ( i =2:(b−1)∗b ˆ ( t −1) )a=in cr (a ,b ) ;
34 f=f +1;F( f ,1)=(−1 ) ˆ s ∗(bˆ e )∗ ( i n v b ∗a ) ;
36 end
38 end
end40
F=sort ( F ) ;42 card=length ( F ) ;
13 fLV.m
Contenu du fichier fLV.m :
function f=fLV( t , x)2
f=zeros ( 2 , 1 ) ;4 a=2;
b = 0 . 0 0 1 ;6 c=10;
e = 0 . 0 0 2 ;8 f (1)=a∗x(1)−b∗x ( 1 )∗x ( 2 ) ;
f(2)=−c∗x(2)+e∗x ( 1 )∗x ( 2 ) ;
14 fnewton.m
Contenu du fichier fnewton.m :
function [ r e s , i t ] = f n ew t o n ( f u n c , d f u nc , x , t o l )2 % F i n ds a r o o t o f f ( x ) = 0 u s i n g Newton ’ s me tho d .
% E xa mp le c a l l : [ r e s , i t ]= f n e w to n ( f u nc , d f un c , x , t o l )4 % The u se r d e fi n ed f u nc t i on f un c i s t h e f u nc t i on f ( x ) ,
% The u s er d e f i n ed f u n c t i on d fu nc i s d f / dx .
Page 10
-
8/19/2019 Syl Matlab
11/60
Syllabus Matlab 16 FORWARD ROW.M
6 % x i s an i n i t i a l s t a r t i ng v al ue , t o l i s r eq ui r ed a cc ur ac y .
8 i t =0; x0=x ;d=f e v a l ( func , x0)/ f e v a l ( dfunc , x0 ) ;
10 while abs ( d)> t o lx1=x0−d ;
12 i t=it +1;x0=x1 ;
14 d=f e v a l ( func , x0)/ f e v a l ( dfunc , x0 ) ;end ;
16 r es=x0 ;
15 forward col.m
Contenu du fichier forward col.m :
function [ b ]= f o r w a r d c o l ( L , b )2 [ n ] = m a t s q u a r e ( L ) ;
T=t r i l ( L ) ;4
i f sum(sum(T ˜= L))>06 error ( ’ E rr or : t he m at ri x i s n ot l ow er t ri ag ul ar ’ ) ;
end
8
l = min( diag ( abs ( L ) ) ) ;10 i f l = = 0
disp ( ’ E rr o r : t he m a tr i x i s s i ng u la r ’ ) ; b = [ ] ;12 break
end
14 f o r j =1:n−1,
b( j )= b( j )/L( j , j ) ;16 b( j +1:n)=b( j +1:n)−b ( j )∗L( j +1:n, j ) ;
end ;18 b ( n ) = b( n )/ L( n , n ) ;
16 forward row.m
Contenu du fichier forward row.m :
function [ x ]= f o r w a r d r o w ( L , b )2 [ n ] = m a t s q u a r e ( L ) ;
l = min( diag ( abs ( L ) ) ) ;4 T=t r i l ( L ) ;
6 i f sum(sum(T ˜= L))>0error ( ’ E rr or : t he m at ri x i s n ot l ow er t ri ag ul ar ’ ) ;
8 end
10 i f l = = 0disp ( ’ E rr o r : t he m a tr i x i s s i ng u la r ’ ) ; x = [ ] ;
12 break
end
14 x ( 1 ) = b ( 1 ) / L ( 1 , 1 ) ;f o r i = 2: n ,
16 x ( i ) = ( b ( i )−L ( i , 1 : i −1)∗( x ( 1 : i − 1)) ’)/L ( i , i ) ;end
18 x=x ’ ;return
Page 11
-
8/19/2019 Syl Matlab
12/60
-
8/19/2019 Syl Matlab
13/60
Syllabus Matlab 23 INVPOWER.M
V1( ind )=V1( ind )+1;16
f o r ( i=l : −1 : 1 )18 i f (V1( i)>=b )
V1( i )=0 ;20 V1( i −1)=V1( i −1)+1;
end22 end
22 interp ls.m
Contenu du fichier interp ls.m :
2 c l os e a l l
c le ar a l l
4 hold o f f x = 1 0 : 5 0 ;
6
X p = 1 0 : 3 : 5 0 ;8 Yp=2∗(Xp.ˆ2)+200∗randn ( s i z e ( X p ) ) ;
10 plot ( x , 2 ∗ ( x . ˆ 2 ) )hold
12 pause
plot (Xp,Yp, ’* ’ )14 a=a xis ;
pause
16
y i n = p o l l a g ( Xp , Yp , x )
18 plot (x , y in , ’r ’ )a xis ( a ) ;
23 invpower.m
Contenu du fichier invpower.m :
function [ sigma , x , ni te r , er r ]=invpo wer (A, z0 ,mu, to l l , nmax)2 n=max( s i z e ( A ) ) ;
M=A−mu∗eye ( n ) ;4 [L,U,P]= lu (M) ;
q=z0/norm( z 0 ) ;6 q2=q ’ ;
e r r = [ ] ;8 s i gm a = [ ] ;
r e s = t o l l + 1;10 n i t e r = 0;
while ( r e s >= t o l l & n i te r
-
8/19/2019 Syl Matlab
14/60
Syllabus Matlab 27 LS POL.M
c o s t h e t a =abs ( q2 ∗q ) ;24 i f ( c o s t h e t a >= 5e −2) ,
r e s =norm( z−lam∗q ) / c o s t h e t a ;26 e r r = [ e r r ; r e s ] ;
sigma=[sigma ; lam ] ;28 e ls e ,
disp ( ’ M u lt i pl e e i ge n va l ue ’ ) ; break ;30 end
x=q ;32 end
return
24 kern2.m
Contenu du fichier kern2.m :
function K=kern (x , ce nt er , base )2 % X[ N, n ]
% c e n t e r [ m, n ] 4
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 , ce nt er , base )2 % X[ N, n ]
% c e n t e r [ m, n ]
4
K=exp(−(( x−c e n t e r ) / b a s e ) . ˆ 2 ) ;
26 loga.m
Contenu du fichier loga.m :
function l=log a (a , x)2
4 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
4 f o r ( i =1: length (Xp))f o r ( j =1:m+1)
6 X( i , j )=Xp( i )ˆ ( j −1);end
8 Y( i ,1 )=Yp( i ) ;end10
12 a=pinv (X) ∗Y;
Page 14
-
8/19/2019 Syl Matlab
15/60
Syllabus Matlab 31 MODEL.2M.M
14 y=zeros ( s i z e ( x ) ) ;f o r ( j =1 :m+1)
16 y=y+a ( j ) . ∗ x . ˆ ( j −1);end
28 lu kji.m
Contenu du fichier lu kji.m :
function [ A] = l u k j i (A)2 [ n , n ] = s i z e (A);
f o r k=1:n−14 A(k+1:n , k)=A(k+1:n , k)/A(k , k ) ;
f o r j=k+1:n,6 f o r i=k+1:n
A( i , j )=A( i , j )−A( i , k) ∗A(k , j ) ;8 end
end
10 end
return
29 mat square.m
Contenu du fichier mat square.m :
function [ n]=mat squa re (A)2 [ n , m] = s i z e (A);
i f n ˜ = m
4 disp ( ’ E rr or : t he m at ri x s ho ul d b e s qu ar ed ’ ) ;s t o p6 end
return
30 model2.m
Contenu du fichier model2.m :
function z=model (x , y)2
4
6 z=0.1+(1.0+ s in ( 2 .∗ x+3.∗y ) ) . / ( 3 . 5 + s in ( x−y ) ) ;
31 model.2m.m
Contenu du fichier model.2m.m :
function y=model(x)2
4 y=0.1+(1+ s in (3∗x + 2 ) ) . / ( 3 . 5 + si n (2−x ) ) ;y=0.5+0.4∗ si n (2∗ pi ∗x ) ;
Page 15
-
8/19/2019 Syl Matlab
16/60
Syllabus Matlab 35 PLOT FUN.M
32 model.m
Contenu du fichier model.m :
function y=model(x)2
4 y=0.1+(1+ s in (3∗x + 2 ) ) . / ( 3 . 5 + si n (2−x ) ) ;y=0.5+0.4∗ si n (2∗ pi ∗x ) ;
33 phi.m
Contenu du fichier phi.m :
function p=phi (Y,A,b)2 % Y[ n , N]
4 N=s i z e ( Y , 2 ) ;
f o r i =1:N6 p( i ,1)=1/ 2∗Y( : , i ) ’∗A∗Y ( : , 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 ( x 0 , y0 , R , c )2
% ( x −x0)ˆ2+(y −y0)ˆ2=Rˆ2 4 i f n ar gi n
-
8/19/2019 Syl Matlab
17/60
Syllabus Matlab 38 POL LAG.M
36 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=x−abs ( x )−5;4 x max=x+abs ( x ) + 5 ;
6
de lt a=(x max−x m i n ) / 5 0 ;8
I x=x min : del ta : x max ;10
plot ( I x , y+m.∗ ( I x−x) , c)
37 plot line.m
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)4 x min=min(x1 , x2 ) ;
x max=max(x1 , x2 ) ;6
8 de lt a=(x max−x m i n ) / 5 0 ;
10 I x=x min : de lt a : x max ;
12 plot ( I x , ( y 2−y 1 ) / ( x 2−x1 ) . ∗ ( I x−x1)+y1 , c )e l s e i f ( y1==y2 )
14 plot ( x1 , y1 , ’*’ ) ;e l s e
16 y min=min(y1 , y2 ) ;y max=max(y1 , y2 ) ;
18
de lt a=(y max−y m i n ) / 5 0 ;20
I y=y min : del ta : y max ;22
plot ( x1 ∗ones ( 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=ones ( s i z e ( x ) ) ;4
6 f o r ( i =1: length (X))
8 p( i ,: )=Y( i )∗on ;f o r ( j =1: length (X))
10
i f ( j˜=i )
Page 17
-
8/19/2019 Syl Matlab
18/60
Syllabus Matlab 40 REAL2F.M
12 p( i ,: )= p( i , : ) . ∗ ( x−X( j )) . / (X( i)−X( j ) ) ;end
14 end
end
16 y=sum( p ) ;
39 powerm.m
Contenu du fichier powerm.m :
function [ nu1 , x1 , ni te r , er r ]=powerm(A, z0 , t o l l , nmax)2 q=z0/norm( z 0 ) ;
q2=q ;4 e r r = [ ] ;
nu1 =[ ];6 r e s = t o l l + 1;
n i t e r = 0;8 z=A∗q ;
while ( r e s >= t o l l & n i te r = 5e −2) ,20 n i t e r = n i t e r + 1;
r e s =norm( z−lam∗q ) / c o s t h e t a ;22 e r r = [ e r r ; r e s ] ;
nu1=[nu1 ; lam ] ;24 e l s e
disp ( ’ M u lt i pl e e i ge n va l ue ’ ) ; break ;26 end
end
28 return
40 real2f.m
Contenu du fichier real2f.m :function [ s ,m, e]= re al 2f (r , b , t , l , u)
2
% r e a l 2 f : r oun ds t o t he n e ar e st f l o a t i n g p o in t . I t r e tu r ns e r ro r i n c as e o f o v er f l ow / u n de4 %
% REAL2F(R, B, T, L ,U)6 % R − [ 1 , 1 ] r e a l i np ut
% B − [ 1 , 1 ] b a s e 8 % 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 b ou nd e x p on e n t 10 % U − [ 1 , 1 ] u p pe r b ou nd e x p on e n t
%
12 % Returns :% M − [1 ,T ] m an ti ss a v e ct o r o f d i g i t s
14 % E − [ 1 , 1 ] e x p on e n t % S − [ 1 , 1 ] signum : 0 f o r p o s i t i v e , 1 f o r n e ga t i ve
16
Page 18
-
8/19/2019 Syl Matlab
19/60
Syllabus Matlab 41 ROOT ITER.M
% Co ur s d e C a l c u l énumrique MATH 03118 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 20
i f ( r==0)22 s=0;
m=0;24 e=0;
return ;26 end
28 s=rbˆ u∗(1−bˆ(− t ) ) )36 error ( ’overflow’ ) ;
end
38
40 e=c e i l ( l o g a ( b , r ) ) ;
42 f=r ∗( b ˆ ( t−e ) ) ;f o r ( i =1:t −1)
44 m( i )=f l o o r ( f /(bˆ( t−i ) ) ) ;f=f −b ˆ ( t−i ) ∗m( i ) ;
46
end
48 i f ( round ( f )
-
8/19/2019 Syl Matlab
20/60
Syllabus Matlab 41 ROOT ITER.M
end
16
i f ( nargin x(k+1)50 ni t=ni t +1;
52 x k=xvect ( ni t ) ;f x k =f e v a l ( f u n , x k ) ;
54
i f ( meth==2) %s e c a n t m et ho d 56 m=(fx k−fx ( ni t −1 ) )/ ( x k−xve ct ( nit − 1 ) ) ;
e l s e i f ( meth==3) % R e gu la F a l s i 58 I k=find ( f x k ∗ fx 0)60 m=(fx k−fx ( Ik ( end ) ) ) / ( x k−xv ec t ( Ik (end ) ) ) ;
end
62 e l s e i f ( meth==4) % Newton [d,m]= f e v a l ( fun , x k ) ;
64 end
66
x k1=x k−f x k / m ;68 i f ( t es t==1)
stop=abs ( x k 1−x k ) ;70 e l s e
stop=abs ( f x k ) ;72 end
74 x d i f =[ x d i f ; s t o p ] ;
Page 20
-
8/19/2019 Syl Matlab
21/60
Syllabus Matlab 42 ROUND2FLO.M
76 i f ( grap hic )a xis ( [ min( xvect)−abs ( d e l t a 2 ∗min( xvect ) ) max( xvect )+abs ( d e l t a 2 ∗max( xve ct ) ) . . .
78 min( fx )−abs ( d e l t a 2 ∗min( fx ) ) max( fx)+abs ( d e l t a 2 ∗max( fx ) ) ] )
80 p l o t l i n e ( x k , 0 , x , f e v a l ( fun , x k ) , ’: ’ ) % p l ot f u nc t io n v a lu e plot ( x k , f x k , ’* ’ )
82 p l o t l i n e 2 ( x k , f x k , m, ’r’ ) ; % p l o t c or de plot ( x k 1 , 0 , ’* ’ ) ; % p l o t i n
84 i f ( meth==1)t i t l e ( [ ’Corde method: Iteration=’ num2str ( nit − 2 ) ] )
86 e l s e i f ( meth==2)t i t l e ( [ ’Secant method: Iteration=’ num2str ( ni t −2 ) ] )
88 e l s e i f ( meth==3)t i t l e ( [ ’Regula falsi method: Iteration=’ num2str ( nit −2 ) ] )
90 e l s e i f ( meth==4)t i t l e ( [ ’Newton method: Iteration=’ num2str ( ni t −2 ) ] )
92 end
94 pause
end
96
x=x k1 ;98 xvect =[xvec t ; x ] ;
f x = [ f x ; f e v a l ( fun , x ) ] ;100 end
return
42 round2flo.m
Contenu du fichier round2flo.m :
function Mx ti ld e=rou nd2 flo (Mx,b , t )2 % r o un d 2f l o : r e tu r ns t he r e a l c oo re sp on di ng t o t h e n e ar e st f l o a t i n g p o in t
% 4 % ROUND2FLO(Mx,B,T)
% Mx − [ 1 , 1 ] r e a l num be r 6 % B − [ 1 , 1 ] b a s e
% T − [ 1 , 1 ] number o f s i g n i f i c a n t d i g i t s 8 % Returns M x t i ld e [ 1 , 1 ] r e a l nummber in d ec im al f or mat
% 10
12 % Co ur s d e C a l c u l énumrique MATH 031% Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
14 % J a n u a r y 2 0 0 2 i f ( t
-
8/19/2019 Syl Matlab
22/60
Syllabus Matlab 44 S ARRET.M
43 s ab stababs.m
Contenu du fichier s ab stababs.m :
2 c l e ar a l l
4 f o r ( h = 0 . 1 : . 1 : 1 )c le a r y
6 c le a r ytc lo se a l l
8
hold o f f 10 T=15;
t = 0 : . 1 : T ;12 lambda=−1;
plot ( t , exp (lambda∗ t ) ) ;14
16 cont=1;y( cont )=1;
18 y t ( c o n t ) = 0 ;
20 y ( co nt +1)=y( co nt )+h∗ lambda∗y( cont ) ;yt ( con t+1)=h;
22 cont=2;
24 r=roots ([ 2 −3∗h∗ lambda−2 lambda∗h ] )
26
f o r ( th=(2∗h ) : h :T)28 yt ( con t+1)=th ;
y( con t+1)=y( cont )+0.5∗h∗(3∗ lambda∗y( cont)−lambda∗y ( c o n t − 1 ) ) ;30 cont=cont+1;
end
32
34 hold
36 plot (yt , y , ’r ’ )t i t l e ( [ ’h=’ num2str ( h ) ’ , r_0 =’ num2str ( r ( 1 ) ) ’ r _1 = ’ num2str ( r ( 2 ) ) ] )
38 xlabel ( ’time’ )pause
40
end
44 s arret.m
Contenu du fichier s arret.m :
% SCRIPT s i t e r .m 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% March 2002 6
c lo se a l l
8 fun=’f_arret’ ;a=0;
10 b=30;nmax=120000;
Page 22
-
8/19/2019 Syl Matlab
23/60
Syllabus Matlab 46 S COMPR.M
12 t o l l = 1 e−3;−log ( 1 e−9)
14
meth=4; % meth : 1 f o r co rd e , 2 f o r s ec an t , 3 f o r r e gu l a f a l s i , 4 Newton 16 stop =2; % 1 f o r i ncr em en t , 2 f o r r e s i d u a l
18 [ xvect , xdi f , fx , ni t ]= ro o t i te r (a , b , meth ,nmax, to ll , fun ,1 , stop )
45 s assoc.m
Contenu du fichier s assoc.m :
% SCRIPT s as so c 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
b=10;8 t=2;
d 1 = 0 . 1 6 5 6 ;10 d 2 = 7 . 4 4 0 9 ;
d 3 = 2 . 9 1 6 8 ;12
d s t a r 1 =r o u n d 2 f l o ( d 1 , b , t ) ;14 d s t a r 2 =r o u n d 2 f l o ( d 2 , b , t ) ;
d s t a r 3 =r o u n d 2 f l o ( d 3 , b , t ) ;16
xtemp=round 2fl o ( dst ar2+dstar3 , b , t ) ;18 xs ta r1=rou nd 2f lo ( ds ta r1+xtemp , b , t )
20 xtemp=round 2fl o ( dst ar1+dstar2 , b , t ) ;xs ta r2=rou nd 2f lo ( ds ta r3+xtemp , b , t )
46 s compr.m
Contenu du fichier s compr.m :
% SCRIPT s com pr 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% March 2002 6
c le ar a l l
8 c l os e a l l
c m = [ 0 : 1 / 2 5 6 : 1 ; 0 : 1 / 2 5 6 : 1 ; 0 : 1 / 2 5 6 : 1 ] ;10 colormap (cm’)
a=double ( imread ( ’alf.jpg’ ) ) ;12 [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 ) ;14 image ( a ) ;
t i t l e ( [ ’Original uncompressed image. Size=’ num2str ( c o m p l o r i g ) ’ Normalised=1’ ] )16 pause
[U,S,V]=svd ( double (a )) ;18 s i z e ( S )
f o r ( h = 1 : 5 : 1 0 0 )20 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
-
8/19/2019 Syl Matlab
24/60
Syllabus Matlab 48 S CONIUG.M
22
image (A)24 t i t l e ( [ ’ C o mp r es s ed i ma g e h = ’ num2str ( h ) ’ S iz e = ’ num2str ( compl ) ’ Normalised=’ num2str
PSNR = − 5 ∗ log10 ( ( 1 / m∗n )∗ sum(sum( (A−a ) . ˆ 2 ) ) )26 pause
end
47 s coniug2.m
Contenu du fichier s coniug2.m :
% SCRIPT s co ni ug 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% F e b r u a r y 2 0 0 2 6
8
c lo se a l l
10 c l e ar a l l
n=3;12 A = [ 2 3 5 ; . . .
3 2 6 ; . . .14 5 6 2 ] ;
b = [ 1 ; 3 ; 4 ] ;16
18
x ha t = [ 0 ; 0 ; 0 ] ;
20 p=zeros ( 3 , 1 1 ) ;r=zeros ( 3 , 1 1 ) ;
22 r ( : ,1) =b−A∗xhat ;p ( : , 1 ) = r ( : , 1 ) ;
24
f o r (k=1:n−1)26 plot ( xhat (1) , xhat (2 ) , ’k*’ )
28 r ( : , k)=b−A∗xhat ;alph a=p ( : , k) ’∗ r ( : , k ) / ( p ( : , k ) ’∗A∗p ( : , k ) ) ;
30 xnew=xhat+alpha∗p ( : , k ) ;r ( : , k+1)=r ( : , k)−a l p h a∗A∗p ( : , k ) ;
32 beta=(A∗p ( : , k ) ) ’∗ r ( : , k+1)/(p ( : , k) ’∗A∗p ( : , k ) )p ( : , k+1)=r ( : , k+1)−beta∗p ( : , k ) ;
34 pause
36 p l o t l i n e ( x h a t ( 1 ) , x h a t ( 2 ) , x new ( 1 ) , x new ( 2 ) , ’r:’ ) ;xhat=xnew
38 end
48 s coniug.m
Contenu du fichier s coniug.m :
% SCRIPT s co ni ug 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% F e b r u a r y 2 0 0 2
Page 24
-
8/19/2019 Syl Matlab
25/60
Syllabus Matlab 49 S COST.M
6
8
c lo se a l l
10 c l e ar a l l
12 A= [ 2 3 ; 3 2 ] ;b = [ 1 ; 3 ] ;
14
[ y1 , y2 ] = meshgrid ( − 2 : . 5 : 2 , − 2 : . 5 : 2 ) ;16
18 z=1/2∗( y 1 . ˆ 2∗A ( 1 , 1 ) + y 2 .∗ y1 ∗A ( 2 , 1 ) + y 1 .∗ y2 ∗A ( 1 , 2 ) + y 2 . ˆ 2∗A(2,2)) −b ( 1 )∗y1−b ( 2 )∗ y2 ;
20
22 hold o f f x=A\b
24 contour (y1 , y2 , z ,3 0 )hold on
26 plot ( x ( 1 ) , x ( 2 ) , ’r*’ )
28
x h a t = [ −1 . 5 ; −1 . 5 ] ;30 p=zeros ( 2 , 1 1 ) ;
r=zeros ( 2 , 1 1 ) ;32 r ( : ,1) =b−A∗xhat ;
p ( : , 1 ) = r ( : , 1 ) ;34
f o r ( k = 1 : 3 )36 plot ( xhat (1) , xhat (2 ) , ’k*’ )
38 r ( : , k)=b−A∗xhat ;alph a=p ( : , k) ’∗ r ( : , k ) / ( p ( : , k ) ’∗A∗p ( : , k ) ) ;
40 xnew=xhat+alpha∗p ( : , k ) ;r ( : , k+1)=r ( : , k)−a l p h a∗A∗p ( : , k ) ;
42 beta=(A∗p ( : , k ) ) ’∗ r ( : , k+1)/(p ( : , k) ’∗A∗p ( : , k ) )p ( : , k+1)=r ( : , k+1)−beta∗p ( : , k ) ;
44 pause
46 p l o t l i n e ( x h a t ( 1 ) , x h a t ( 2 ) , x new ( 1 ) , x new ( 2 ) , ’r:’ ) ;xhat=xnew
48 end
49 s cost.m
Contenu du fichier s cost.m :
% SCRIPT s co st 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
c le ar a l l
8 addpath( ’c:\bontempi\prof\calcul\quarteroni\Programs’ )I =2:20;
10 f o r ( n=I )f l ( n , 1 ) = c o u t ( n ) ;
12 b o n ( n , 1 ) = 2∗(nˆ3/3−n/3)+n∗( n−1)/2;
Page 25
-
8/19/2019 Syl Matlab
26/60
Syllabus Matlab 50 S DIST FLO.M
14 end
16 plot ( I , [ f l ( I ) b on ( I ) ] )t i t l e ( ’ A l go r it h mi c c os t L U ’)
18 xlabel ( ’ o rd e r o f t he s q ua r e m a tr i x ’)
ylabel ( ’flops’ )20 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
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
8 c l e ar a l l
c lo se 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 noz ero=F(F˜=0);
18 % −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
20 f p r i n t f ( 1 , ’ C he ck t ha t t he m in im um o f F i s b ^( L -1 ). .. ’ ) ;b ˆ ( L−1)
22 min abs F=min( abs ( F nozero ))f p r i n t f ( 1 , ’ P us h a b u tt o n t o c o nt i nu e ’ ) ;
24 pause
26 % −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−f p r i n t f ( 1 , ’ C he c k t ha t t he m a xi u m o f F i s b ^ U *( 1 - b ^( - t ) ). . . ’ ) ;
28 bˆU∗(1−bˆ(− t ) )max abs F=max( abs ( F nozero ))
30 f p r i n t f ( 1 , ’ P us h a b u tt o n t o c o nt i nu e ’ ) ;pause
32
% −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−34
36 F=F(F>0);
38 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
r e l d i s t =[NaN; d i f f ( F ) . / F ( 1 : end−1 ) ] ;42 hold o f f
plot ( F ( 1 : end ) , abs ( r e l d i s t ) , ’*-’ )44 hold
plot (F, i n f r e l d i s t ∗ ones ( length (F) ,1) , ’--’ )46 plot (F , s u p r e l d i s t ∗ ones ( length (F) ,1) , ’--’ )
Page 26
-
8/19/2019 Syl Matlab
27/60
Syllabus Matlab 52 S ERRGEN.M
48 xlabel ( ’Floating point numbers’ )ylabel ( ’Relative Distance’ )
50
a xis ( [ min(F ) max( F ) 0 1 . 2 ∗ s u p r e l d i s t ] )52 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 er r ar r .m 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
8 c l e ar a l l
c lo se a l l
10 % Random number gen era ti on r=10∗randn ;
12
f o r ( b = 2 : 1 6 )14 I T = 1 : 1 0 ;
f o r ( t=I T)16 r t i l d e =r o u n d 2 f l o ( r , b , t ) ;
% r ou nd in g t o t he n e ar e st f l o a t i n g p o in t 18
e r e l ( t , 1 )= abs ( ( r−r t i l d e ) / r ) ;
20 % r o u nd i n g e r r o r c o m pu t a ti o n
22 bound( t ,1)= 0.5 ∗bˆ(1− t ) ;% t h e o r e t i c a l b ou nd c om p ut a ti o n
24 i f ( e r e l ( t ) >0.5∗bˆ(1− t ) & 0 . 5∗bˆ(1− t )>1e−14)error ( ’what?’ ) ;
26
end
28 end
30 % v i s u a l i s a t i o n plot ( I T , [ e r e l b ou nd ] )
32 xlabel ( ’ N u mb e r o f s i gn i fi c an t d i gi t s ’ ) ;legend ( ’Rounding error’ , ’bound’ ) ;
34 t i t l e ( [ ’base=’ num2str ( b ) ] )pause
36 end
52 s errgen.m
Contenu du fichier s errgen.m :
% SCRIPT s er rg en 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
Page 27
-
8/19/2019 Syl Matlab
28/60
Syllabus Matlab 54 S EULER.M
8 t=3b=10;
10
d = 0 . 1 0 7 ∗1 e1 ;12 x=dˆ2−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 ) ;
18 x 2 s t a r =r o u n d 2 f l o ( x 1 s t a r −1,b , t ) ;
20 e r r 1 =abs ( ( x−x 2 s t a r ) / x )pause
22
f p r i n t f ( 1 , ’Second algorithm’ ) ;24 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 ) ;26 e r r 2 =abs ( ( x−x 2 s t a r ) / x )
53 s euler arr.m
Contenu du fichier s euler arr.m :
c le ar a l l
2 c l os e a l l
addpath . . \ deuxieme4 hold o f f
t = 0 : . 0 1 : 0 . 5 ;6 b=10;
tt =2;8 y=f e v a l ( ’sol_an’ , t ) ;
10 plot ( t , y )hold
12
f o r ( h = [ 0 . 1 : − 0 . 0 0 5 : 0 . 0 0 5 ] )14 c le a r y h a t ;
s t e p = 1 ;16 y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t = 0: h : 0 . 5 )18 y hat ( ste p+1)=ro und 2fl o ( y hat ( ste p)+rou nd2 flo (h∗d e r a n ( t , y h a t ( s t e p ) ) , b , t t ) , b , t t ) ;
step=step+1;20 end
22 plot ( 0 : h : 0 . 5 , y h a t ( 1 : end−1) , ’k-’ )h
24 pause
26 end
xlabel ( ’t ’ )28 ylabel ( ’y(t)’ )
%le g e n d ( ’ An a l y t i c a l ’ , ’ E u le r h =0 . 0 5 ’ , ’ E u le r h =0 . 1 ’ )
54 s euler.m
Contenu du fichier s euler.m :
c le ar a l l
Page 28
-
8/19/2019 Syl Matlab
29/60
Syllabus Matlab 55 S EULER PC.M
2 c l os e a l l
hold o f f 4 t = 0 : . 0 1 : 0 . 5 ;
6 y=f e v a l ( ’sol_an’ , t ) ;
8 plot ( t , y )hold
10
f o r ( h = [ 0 . 0 5 0 . 1 ] )12 c le a r y h a t ;
s t e p = 1 ;14 y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t = 0: h : 0 . 5 )16 y h at ( st ep +1)=y hat ( st ep )+h∗d e r a n ( t , y h a t ( s t e p ) ) ;
step=step+1;18 end
20 i f (h==0.05)plot ( 0 : h : 0 . 5 , y h a t ( 1 : end−1) , ’r-*’ )
22 e l s e
plot ( 0 : h : 0 . 5 , y h a t ( 1 : end−1) , ’k-*’ )24 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 :
c le ar a l l
2 c l os e a l l
hold o f f 4 t = 0 : . 0 1 : 0 . 5 ;
6 y=f e v a l ( ’sol_an’ , t ) ;
8 plot ( t , y )hold
10
h = 0 . 1 ;12 c l e a r y h a t ;
s t e p = 1 ;14 y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t = 0: h : 0 . 5 )16 y h at ( st ep +1)=y hat ( st ep )+h∗d e r a n ( t , y h a t ( s t e p ) ) ;
step=step+1;18 end
20 plot ( 0 : h : 0 . 5 , y h a t ( 1 : end−1) , ’r-*’)
22
c l e a r y h a t ;24 s t e p = 1 ;
y h a t ( s t e p )= s o l a n ( 0 ) ;26 f o r ( t = 0: h : 0 . 5 )
y hat ( st ep +1)=y hat ( st ep )+h∗d e r a n ( t , y h a t ( s t e p ) ) ;
Page 29
-
8/19/2019 Syl Matlab
30/60
Syllabus Matlab 57 S EXEMPLE.M
28 y h at ( st ep +1)=y hat ( st ep )+h∗der an ( t , y hat ( ste p +1));
30 step=step+1;end
32
plot ( 0 : h : 0 . 5 , y h a t ( 1 : end−1) , ’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 :
2 c l e ar a l l
c lo se a l l
4 hold o f f
T=5;6 t = 0 : . 1 : T ;
lambda=−5;8 plot ( t , exp (lambda∗ t ) ) ;
10 cont=1;y( cont )=1;
12 yt ( cont +1)=0;
14 h = 0 . 4 1 ;
16 h∗ lambda
pause18
f o r ( th=h : h :T)20 yt ( con t+1)=th ;
y( cont+1)=y( cont)+h∗ lambda∗y( cont ) ;22 cont=cont+1;
end
24
26 hold
28 plot (yt , y , ’r ’ )t i t l e ( [ ’h=’ num2str ( h ) ’ , h * La mb da = ’ num2str( h∗ lambda ) ] )
30 xlabel ( ’time’ )
57 s exemple.m
Contenu du fichier s exemple.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 X= [ − 3 : . 0 5 : 3 ] ’ ;
Y=s in (X);
6
x = −3 : . 0 5 : 3 ;8 y=s in ( x ) ;
10 yp=po l la g (X,Y, x ) ;
Page 30
-
8/19/2019 Syl Matlab
31/60
Syllabus Matlab 61 S GAUSEI2.M
12 plot ( x , y )
14 hold
plot (x , yp )16 t i t l e ( ’Interpolation polynomiale’ )
58 s fillin.m
Contenu du fichier s fillin.m :
% SCRIPT s f i l l i n 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6 c l e ar a l l
rand ( ’state’ , 0 )8
n=100;10 A=s p ra n d ( 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 :
2 [ 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
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
c le ar a l l8 c l os e a l l
10 b = 1 0 ; t = 1 ; L=−1;U=1;[ f , c ] = f l o s e t ( b , t , L , U ) ;
12
plot ( f , zeros ( s i z e ( f )) , ’* ’ )14 t i t l e ( [ ’b=’ num2str ( b ) ’ t = ’ num2str ( t ) ’ L= ’ num2str (L ) ’ U= ’ num2str ( U ) ] )
16 card=2∗(b−1)∗b ˆ ( t −1)∗(U−L+1)+1
61 s gausei2.m
Contenu du fichier s gausei2.m :
Page 31
-
8/19/2019 Syl Matlab
32/60
Syllabus Matlab 62 S GAUSEI.M
% SCRIPT s ga us ei 2 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
c le ar a l l8 n=3;
A = [ 3 1 − 1 ; 2 6 2 ; 1 2 4 ] ;10 b = [ 3 ; 1 0 ; 7 ] ;
12 D=diag ( diag ( A ) ) ;E=−t r i l (A, −1 ) ;
14
P=D−E ;16 F=−t r i u ( A , 1 ) ;
18 N=F;
20 x ( : , 1 ) = zeros ( n , 1 ) ;
22
f o r i =2:2024 x ( : , i )=in v (P )∗ (N∗x ( : , i −1)+b ) ;
26 end
f ig u r e ( 2 )28 plot ( x ’ )
62 s gausei.m
Contenu du fichier s gausei.m :
% SCRIPT s ga us ei 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
c le ar a l l
8 n=4;A = [ 5 7 6 5 ; 7 1 0 8 7 ; 6 8 1 0 9 ; 5 7 9 1 0 ] ;
10 b = [ 2 3 ; 3 2 ; 3 3 ; 3 1 ] ;
12 D=diag ( diag ( A ) ) ;E=−t r i l (A, −1 ) ;
14
P=D−E ;16 F=−t r i u ( A , 1 ) ;
18 N=F;
20 x=10∗o n e s ( n , 1 ) ;
22
f o r i =1:300024 x=in v (P )∗ (N∗x+b )
26 end
Page 32
-
8/19/2019 Syl Matlab
33/60
Syllabus Matlab 64 S GERS.M
63 s gers2.m
Contenu du fichier s gers2.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 n=4;
A=randn ( n , n )6
[V,L]= e i g (A);8
Lambda=diag ( L ) ;10
plot ( r e a l (Lambda) , imag (Lambda) , ’rx’ )12 hold on
p l o t c i r c l e ( 0 , 0 ,norm(A))14
16 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--’ )
18 end
20
f o r ( j =1:n)22 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 ( ’ N oi r : n o rm e . R o ug e : d i sq u es l i gn e s . V e rt : d i sq u es c o lo n ne s ’ )
64 s gers.mContenu du fichier s gers.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 n=3;
A = [ 1 0 2 3 ; −1 2 −1 ; 0 1 3 ] ;6
[V,L]= e i g (A);8
Lambda=diag ( L ) ;
10
plot ( r e a l (Lambda) , imag (Lambda) , ’rx’ )12 hold on
p l o t c i r c l e ( 0 , 0 ,norm(A))14
16 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--’ )
18 end
20
f o r ( j =1:n)22 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 xlabel ( ’Re(z)’ )ylabel ( ’Im(z)’ )
26
t i t l e ( ’ N oi r : n o rm e . R o ug e : d i sq u es l i gn e s . V er t e : d i sq u es c o lo n ne s ’ )
Page 33
-
8/19/2019 Syl Matlab
34/60
Syllabus Matlab 66 S GRADIENT.M
65 s grad al.m
Contenu du fichier s grad al.m :
% SCRIPT s g r a d a l 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% F e b r u a r y 2 0 0 2 6
c lo se a l l
8 c l e ar a l l
10 A= [ 2 3 ; 3 2 ] ;b = [ 1 ; 3 ] ;
12
[ y1 , y2 ] = meshgrid ( − 2 : . 5 : 2 , − 2 : . 5 : 2 ) ;14 x h a t = [ −1 . 5 ; −1 . 5 ] ;
16
z=1/2∗( y 1 . ˆ 2∗A ( 1 , 1 ) + y 2 .∗ y1 ∗A ( 2 , 1 ) + y 1 .∗ y2 ∗A ( 1 , 2 ) + y 2 . ˆ 2∗A(2,2)) −b ( 1 )∗y1−b ( 2 )∗ y2 ;18
20
hold o f f 22 x=A\b
contour (y1 , y2 , z ,3 0 )24 hold on
plot ( x ( 1 ) , x ( 2 ) , ’r*’ )26
28 f o r ( k = 1 : 1 0 )plot ( xhat (1) , xhat (2 ) , ’k*’ )
30
r=b−A∗xhat ;32 alp ha=r ’∗ r /( r ’∗A∗ r ) ;
xnew=xhat+alpha∗ r ;34 pause
36 p l o t l i n e ( x h a t ( 1 ) , x h a t ( 2 ) , x new ( 1 ) , x new ( 2 ) , ’r:’ ) ;xhat=xnew
38 end
66 s gradient.m
Contenu du fichier s gradient.m :
% SCRIPT s g r a d i e n t 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% F e b r u a r y 2 0 0 2 6
8
A = [ 5 0 . 1 ; 0 . 1 5 ] ;10 b = [ 1 ; 3 ] ;
12 [ y1 , y2 ] = meshgrid ( − 2 : . 0 1 : 2 , −2 : . 0 1 : 2 ) ;
14
Page 34
-
8/19/2019 Syl Matlab
35/60
Syllabus Matlab 68 S ITER.M
z=1/2∗( y 1 . ˆ 2∗A ( 1 , 1 ) + y 2 .∗ y1 ∗A ( 2 , 1 ) + y 1 .∗ y2 ∗A ( 1 , 2 ) + y 2 . ˆ 2∗A(2,2)) −b ( 1 )∗y1−b ( 2 )∗ y2 ;16
f ig u r e ( 1 )18 h=mesh( y1 , y2 , z )
xlabel ( ’x1’ ) ;20 ylabel ( ’x2’ ) ;
z la b e l ( ’PHI’ ) ;22
f ig u r e ( 2 )24 hold o f f
x=A\b26 contour (y1 , y2 , z )
hold on28 plot ( x ( 1 ) , x ( 2 ) , ’r*’ )
67 s half.m
Contenu du fichier s half.m :% SCRIPT s h a l f
2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
8 b = 1 0 , t =4d1=0.8717
10 d2=0.8719
12
d s t a r 1 =r o u n d 2 f l o ( d 1 , b , t ) ;14 d s t a r 2 =r o u n d 2 f l o ( d 2 , b , t ) ;
16 f p r i n t f ( 1 , ’First algorithm’ ) ;xst ar1=round 2fl o ( rou nd2 flo ( dst ar1+dstar2 , b , t )/2 ,b , t )
18
pause
20
f p r i n t f ( 1 , ’Second algorithm’ ) ;22 xtemp=rou nd 2f lo ( ro un d2 fl o ( dst ar2 −dstar1 , b , t )/2 ,b , t )
xs ta r2=rou nd 2f lo ( ds ta r1+xtemp , b , t )
68 s iter.m
Contenu du fichier s iter.m :
% SCRIPT s i t e r .m 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% March 2002 6
c lo se a l l
8 fun=’f_test’ ;a=1;
10 b = 3 . 9 ;x 0 = 1 . 1 ;
12 nmax=120000;
Page 35
-
8/19/2019 Syl Matlab
36/60
Syllabus Matlab 70 S JACOBI.M
t o l l = 1 e−6;14
% meth : 1 f o r c or de , 2 f o r s ec an t , 3 f o r r e gu l a f a l s i , 4 Newton 16 meth=4;
18 [ x v e c t , x d i f , f x , n i t ] = r o o t i t e r ( a , b , meth , n max , t o l l , f u n , 1 )
69 s jacobi2.m
Contenu du fichier s jacobi2.m :
% SCRIPT s j a c o b i 2 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
c le ar a l l
8 n=3;A = [ 3 1 − 1 ; 2 6 2 ; 1 2 4 ] ;
10 b = [ 3 ; 1 0 ; 7 ] ;
12 D=diag ( diag ( A ) ) ;P=D;
14 N=D−A;
16 x ( : , 1 ) = zeros ( n , 1 ) ;
18
f o r i =2:20
20 x ( : , i )=in v (P )∗ (N∗x ( : , i −1)+b ) ;
22 end
f ig u r e ( 1 )24 plot ( x ’ )
70 s jacobi.m
Contenu du fichier s jacobi.m :
% SCRIPT s j a c o b i 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
c le ar a l l
8 n=4;A = [ 5 7 6 5 ; 7 1 0 8 7 ; 6 8 1 0 9 ; 5 7 9 1 0 ] ;
10 b = [ 2 3 ; 3 2 ; 3 3 ; 3 1 ] ;
12 D=diag ( diag ( A ) ) ;P=D;
14 N=D−A;
16 x=zeros ( n , 1 ) ;
18
f o r i =1:10
Page 36
-
8/19/2019 Syl Matlab
37/60
Syllabus Matlab 72 S LEAST2.M
20 x=in v (P )∗ (N∗x+b )pause
22 end
71 s lag.m
Contenu du fichier s lag.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 X= [ − 3 : 1 : 3 ] ’ ;
Y=X. ˆ 3 ;6
Y=log (X.ˆ2+1)8 x = −3 : . 0 1 : 3 ;
y=log ( x . ˆ 2 + 1 )10
yp=po l la g (X,Y, x ) ;12
plot ( x , y )14
hold
16 plot (X,Y, ’k*’ )plot (x , yp , ’g ’ )
18 legend ( ’funct’ , ’points’ , ’interp’ )
72 s least2.m
Contenu du fichier s least2.m :c le ar a l l
2 c l os e a l l
hold o f f 4 Xp = [ 1 3 4 6 7 ] ’ ;
Yp=[ −2.1 −0.9 −0.6 0 . 6 0 . 9 ] ;6 x = 1 : 7 ;
m=1;8
f o r ( i =1: length (Xp))10 f o r ( j =1:m+1)
X( i , j )=Xp( i )ˆ ( j −1);
12 endY( i ,1 )=Yp( i ) ;
14 end
16
a=pinv (X) ∗Y;18
y=zeros ( s i z e ( x ) ) ;20 f o r ( j =1 :m+1)
y=y+a ( j ) . ∗ x . ˆ ( j −1);22 end
24 plot (Xp,Yp, ’* ’ )hold
26 plot ( x , y )xlabel ( ’x ’ )
28 ylabel ( ’y ’ )t i t l e ( [ ’ P o ly n om i al o rd e r m = ’ , num2str(m) ] ) ;
Page 37
-
8/19/2019 Syl Matlab
38/60
Syllabus Matlab 75 S LU.M
73 s least.m
Contenu du fichier s least.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 Xp = [ 0 : . 5 : 1 . 5 ] ’ ;
Yp=si n (Xp);6 x = 0 : . 0 1 : 1 . 5 ;
m=2;8
f o r ( i =1: length (Xp))10 f o r ( j =1:m+1)
X( i , j )=Xp( i )ˆ ( j −1);12 end
Y( i ,1 )=Yp( i ) ;14 end
16
a=pinv (X) ∗Y;18
y=zeros ( s i z e ( x ) ) ;20 f o r ( j =1 :m+1)
y=y+a ( j ) . ∗ x . ˆ ( j −1);22 end
24 plot (Xp,Yp, ’* ’ )hold
26 plot ( x , y )xlabel ( ’x ’ )
28 ylabel ( ’y ’ )t i t l e ( [ ’ P o ly n om i al o rd e r m = ’ , num2str(m) ] ) ;
74 s LotkaVolterra.m
Contenu du fichier s LotkaVolterra.m :
c le ar a l l
2 c l os e a l l
simtime=input ( ’enter runtime=’ ) ;4
i n i t x = [ 5 0 0 0 1 0 0 ] ’ ;
6 [ t , x]=ode23 ( ’fLV’ , [ 0 , s i m t im e ] , i n i t x ) ;plot ( t , x ) ;
8 xlabel ( ’time’ ) ;ylabel ( ’population’ ) ;
10
12 legend ( ’Rabbits’ , ’Foxes’ )
75 s lu.m
Contenu du fichier s lu.m :
% SCRIPT s l u 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2
Page 38
-
8/19/2019 Syl Matlab
39/60
Syllabus Matlab 76 S MIL STABABS.M
6
A= [ 4 , 8 , 1 2 ; 3 8 1 3 ; 2 9 1 8 ] ;8
LU=l u k j i (A);10
L=t r i l (LU, −1 ) ;
12 f o r ( i =1: s i z e ( A , 1 ) )L( i , i )=1;
14 end
16 U=t r i u (LU);
18 b = [ 4 ; 5 ; 1 1 ] ;y=f o r w a r d c o l ( L , b )
20
x=backwa rd col (U, y)
76 s mil stababs.mContenu du fichier s mil stababs.m :
2 c l e ar a l l
c lo se a l l
4 hold o f f T=5;
6 t = 0 : . 1 : T ;lambda=−5;
8 plot ( t , exp (lambda∗ t ) ) ;h = 0 . 0 5 ;
10
cont=1;12 y( cont )=1;
y t ( c o n t ) = 0 ;14
y( cont+1)=y( cont)+h∗ lambda∗y( cont ) ;16 yt ( con t+1)=h;
cont=2;18
roots ([ 1 −2∗h∗ lambda −1 ] )20
h∗ lambda22 pause
24 f o r ( th=(2∗h ) : h :T)yt ( cont +1)=th ;
26 y ( con t+1)=y( cont −1)+2∗h∗ lambda∗y( cont ) ;cont=cont+1;
28 end
30
hold
32
plot (yt , y , ’r ’ )34 t i t l e ( [ ’h=’ num2str ( h ) ’ , h * La mb da = ’ num2str( h∗ lambda ) ] )
xlabel ( ’time’ )
Page 39
-
8/19/2019 Syl Matlab
40/60
Syllabus Matlab 80 S PCA.M
77 s nodal.m
Contenu du fichier s nodal.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 X= [ − 3 : 1 : 3 ] ’ ;
x = − 3 . 4 : . 0 1 : 3 . 4 ;6
plot ( x , zeros ( s i z e ( x ) ) , ’k ’ )8 hold
plot (x , ( x−X ( 1 ) ) . ∗ ( x−X ( 2 ) ) . ∗ ( x−X ( 3 ) ) . ∗ ( x−X ( 4 ) ) . ∗ ( x−X ( 5 ) ) . ∗ ( x−X ( 6 ) ) . ∗ ( x−X ( 7 ) ) , ’b ’ )
78 sol an2.m
Contenu du fichier sol an2.m :
function y=so l an 2 ( t )2 global lambda
y=exp (lambda∗ t ) ;
79 sol an.m
Contenu du fichier sol an.m :
function y=so l a n ( t )2
y=−3∗exp(−t )−2∗ t+2;
80 s pca.m
Contenu du fichier s pca.m :
% SCRIPT s pc a 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% March 2002 6
c le ar a l l
8 c l os e a l lc m = [ 0 : 1 / 2 5 6 : 1 ; 0 : 1 / 2 5 6 : 1 ; 0 : 1 / 2 5 6 : 1 ] ;
10 colormap (cm’)a=double ( imread ( ’alf.jpg’ ) ) ;
12 [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 ) ;
14 image ( a ) ;t i t l e ( [ ’Original uncompressed image. Size=’ num2str ( c o m p l o r i g ) ’ Normalised=1’ ] )
16 pause
18 me=mean( a ) ;C=a−ones (m, 1) ∗me;
20 [ v e c t e u r s , v a l e u r s ] =e ig (C ’∗C ) ;
22 DD=diag ( v a l e u r s ) ;[ o , ord]= sort (−DD);
24
va le ur s or d=DD( ord ) ;
Page 40
-
8/19/2019 Syl Matlab
41/60
Syllabus Matlab 82 S POL LAG.M
26 v e c t e u r s o r d =v e c t e u r s ( : , o r d ) ;
28
f o r h = 1 : 1 0 : n30
a compr=C∗ v e c t e u r s o r d ( : , 1 : h ) ;
32 [ m compr , n compr]= s i z e ( a c o mp r ) ;compl=m compr∗n compr ;
34
a decompr=(a compr∗ v e c t e u r s o r d ( : , 1 : h ) ’ ) ;36
image ( a decompr+ones (m,1 )∗me)38 t i t l e ( [ ’Compressed image. Size=’ num2str ( compl ) ’ Normalised=’ num2str ( c om pl / c o m p l o r i g ) ]
40 pause
end
81 spcub.mContenu du fichier spcub.m :
function [ s ,A,b]=spcub (X,Y, x)2
n=length (X) −1;4
f o r ( i =1:n)6 h ( i )=X( i +1)−X( i ) ;
end
8
A=zeros (n+1,n+1);
10 b=zeros (n+1 ,1);f o r ( i =1:n−1)
12 A( i , i )=h( i )/ 6;A( i , i +1)=(h( i )+h( i +1)) /3;
14 A( i , i +2)=h( i +1 )/6 ;
16 b ( i )=(Y( i +2)−Y( i +1))/h ( i +1)−(Y( i +1)−Y( i ))/ h( i ) ;end
18 A ( n , 1 ) = 1 ;A(n+1,n+1)=1;
20
M=A\b ;22 s = [ ] ;
f o r ( i =1:n)24 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 )) ;26 i f ( i=X( i ) & x=X( i ) & x
-
8/19/2019 Syl Matlab
42/60
Syllabus Matlab 83 S POL NEW2.M
hold o f f 2 X= [ − 3 : 1 : 3 ] ’ ;
4 x = −3 : . 0 1 : 3 ;
6 on=on es ( s i z e ( x ) ) ;
f o r ( i =1: length (X))8
p=on ;10 f o r ( j =1: length (X))
12 i f ( j˜=i )p=p . ∗ ( x−X( j ) ) . / ( X ( i )−X( j ) ) ;
14 end
end
16 plot ( x , p )i f ( i==1)
18 hold onend
20 plot ( x , zeros ( s i z e ( x ) ) ) ;plot (X( i ) ,1 , ’k*’ ) ;
22 pause
end
83 s pol new2.m
Contenu du fichier s pol new2.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 X = [ 0 : . 5 : 2 ] ’ ;
Y=s in (X);6 x = 0 : . 0 1 : 2 ;
8 d=d i v i d i f (X’ ,Y’ ) ;n=length (X);
10 y=d (1 ,1 )f o r ( i =2:n)
12 p=(x−X ( 1 ) ) ;f o r ( j =2: i −1)
14 p=p . ∗ ( x−X( j ) ) ;end
16
18 y=y+p∗d( i , i ) ;end
20 % p l o t ( x , y )
22 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
24 plot (X,Y, ’k*’ )
26 t i t l e ( ’Divided differences: interpolating polynomial’)xlabel ( ’x ’ )
28 ylabel ( ’f(x)’ )
Page 42
-
8/19/2019 Syl Matlab
43/60
Syllabus Matlab 85 S POWER.M
84 s pol new.m
Contenu du fichier s pol new.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 X= [ − 3 : 1 : 3 ] ’ ;
Y=X. ˆ 2 ;6 x = −3 : . 0 1 : 3 ;
8 d=d i v i d i f (X’ ,Y’ ) ;n=length (X);
10 y=d (1 ,1 )f o r ( i =2:n)
12 p=(x−X ( 1 ) ) ;f o r ( j =2: i −1)
14 p=p . ∗ ( x−X( j ) ) ;end
16
18 y=y+p∗d( i , i ) ;end
20 plot ( x , y )hold
22 plot (X,Y, ’k*’ )
85 s power.m
Contenu du fichier s power.m :
% SCRIPT s pow er 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% March 2002 6
c le ar a l l
8 c l os e a l l
hold o f f 10 A = [ 2 1 7 −1 ; 5 7 7 ; 4 −4 2 0 ] ;
z0 =[ 1 ,1 ,1] ’ ;12 nmax=10000;
t o l l = 1 e−4;14
[ nu1 , x1 , ni te r , er r ]=powerm(A, z0 , to l l , nmax)16
18
20 [ s igma , x , ni ter , err ]=invpower (A, z0 ,0 , to ll ,nmax ) ;plot (nu1)
22 hold
plot ( sigma , ’r ’ )24 xlabel ( ’Iterations’ )
ylabel(
’Extremes eigenvalues’)26 a xis ( [ 1 n i t er 0 2 5 ] )
Page 43
-
8/19/2019 Syl Matlab
44/60
Syllabus Matlab 87 S RBF2.M
86 spy lu kji.m
Contenu du fichier spy lu kji.m :
function [ A] = s p y l u k j i (A)2 [ n , n ] = s i z e (A);
f o r k=1:n−14 A(k+1:n , k)=A(k+1:n , k)/A(k , k ) ;
f o r j=k+1:n,6 f o r i=k+1:n
A( i , j )=A( i , j )−A( i , k) ∗A(k , j ) ;8
end
10 end
spy (A);12 t i t l e ( [ ’ I t er a ti o n n o . ’ num2str ( k ) ] )
pause
14 end
return
87 s rbf2.m
Contenu du fichier s rbf2.m :
c le ar a l l
2 c lo se a l l
n=400;4 Xp=4∗rand ( n , 2 ) −2 ;
Yp=model2(Xp( : , 1) , Xp( : , 2 ) ) ;6 [mx,my] = meshgrid ( Xp , Yp ) ;
8 mz=model2 (mx, my) ;
10 mesh(mx,my,mz)
12 f o r mx=2:8hold o f f
14
qq=lin s p a c e ( −2 ,2 ,32) ;16 [Q1,Q2]=meshgrid (qq ) ;
x=[Q1 ( : ) Q2 ( : ) ] ;18
z=model2( x ( : , 1) , x ( : , 2 ) ) ;
20 Z=reshape ( z , 3 2 , 3 2 ) ;f ig u r e ( 2 )
22 mesh(Q1,Q2,Z)D=x ( end)−x ( 1 ) ;
24 de l ta=D/(mx) ;
26 c c=lin s p a c e ( −2 , 2 , m x ) ;[C1,C2]= meshgrid ( cc ) ;
28 C= [C1 ( : ) C2 ( : ) ] ;B=de lt a ∗ones ( s i z e (C , 1 ) , 1 ) ;
30 m=s i z e ( C , 1 )f o r ( i =1: length (Xp))
32 f o r ( j =1:m+1)i f ( j==1)
34 X( i , j )=1 ;e l s e
36 X( i , j )=kern 2 (Xp( i , : ) ,C( j − 1 ,: ) ,B( j − 1 ) ) ;end
Page 44
-
8/19/2019 Syl Matlab
45/60
Syllabus Matlab 88 S RBF.M
38
end
40 Y( i ,1) =Yp( i ) ;end
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 ) ;
50 f o r ( j =1:m)z r b f ( i ) = z r b f ( i ) +a ( j + 1)∗k e r n 2 ( x ( i , : ) , C ( j , : ) , B ( j ) ) ;
52
end
54 end
56
58
RMSE rbf=sqrt (sum( ( z−z r b f ) . ˆ 2 ) / length ( z ) )60 f ig u r e ( 1 )
62 Z r b f=reshape ( z r b f , 3 2 , 3 2 ) ;mesh(Q1, Q2, Z rb f )
64
66 pause
end
88 s rbf.m
Contenu du fichier s rbf.m :
c le ar a l l
2 c lo se a l l
X p = 0 : . 1 : 1 ;4 Yp=model (Xp)+0 .0∗randn ( s i z e ( X p ) ) ;
6 f o r m=2: length (Xp)hold o f f
8
%pl ot (x ,0.1+(1+ sin (2 ∗ x+6) )./(3.5+ sin (x −2)))10 %Yp=s i n (Xp) ;
%y=x ;12 %pl ot (y ,0.1+(1+ sin (3 ∗y+2)). /(3.5+ sin (2 −y ) ) )
14
x = 0 : . 0 1 : 1 . 0 ;16 y=model (x ) ;
18 D=x ( end)−x ( 1 ) ;de l ta=D/(m−1);
20 B=de lt a ∗o n e s (m , 1 ) ;
22
C=x (1 ): d el ta : x( end)24
Page 45
-
8/19/2019 Syl Matlab
46/60
Syllabus Matlab 89 S REFINE.M
26 f o r ( i =1: length (Xp))f o r ( j =1:m+1)
28 i f ( j==1)X( i , j )=1;
30 e l s e
X( i , j )=ke rn (Xp( i ) ,C( j −1) ,B( j − 1 ) ) ;
32 end
34 end
Y( i ,1) =Yp( i ) ;36 end
38
a=pinv (X) ∗Y;40
y r b f =zeros ( s i z e ( x ) ) ;42
f o r ( i =1: length ( x ) )44 y r b f ( i ) =a ( 1 ) ;
f o r ( j =1:m)46 y r b f ( i ) = y r b f ( i ) +a ( j + 1)∗kern (x ( i ) ,C( j ) ,B( j ) ) ;
48 end
end
50
52 plot (Xp,Yp, ’*’ )hold
54 plot ( x , y r b f )xlabel ( ’x ’ )
56 ylabel ( ’y ’ )
58
[ y pol , a]= l s p o l (Xp,Yp,m, x ) ;60 RMSE rbf=sqrt (sum( ( y−y r b f ) . ˆ 2 ) / length ( y ) ) ;
RMSE pol=sqrt (sum( ( y−y p o l ) . ˆ 2 ) / length ( y ) ) ;62
t i t l e ( [ ’ N um b er o f b a si s f u nc t io n s m = ’num2str(m) ] ) ;64
legend ( ’Points’ , ’RBF’ ) ;66 pause
end
89 s refine.m
Contenu du fichier s refine.m :
% SCRIPT s r e f i n e 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6 c l e ar a l l
global base8 global t
base=2;10 t=15;
12 n=4;rand ( ’state’ , 0 )
Page 46
-
8/19/2019 Syl Matlab
47/60
Syllabus Matlab 90 S RK2.M
14 A = [ 5 7 6 5 ; 7 1 0 8 7 ; 6 8 1 0 9 ; 5 7 9 1 0 ] ;b = [ 2 3 ; 3 2 ; 3 3 ; 3 1 ] ;
16
x=f LUs olv e (A, b)18 r=b−A∗x ;
20
dx=f LU so lv e (A, r );22 r=b−A∗( x+dx ) ;
x+dx24 pause
90 s rk2.m
Contenu du fichier s rk2.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 t = 0 : . 0 1 : 0 . 5 ;
6 y=f e v a l ( ’sol_an’ , t ) ;
8 plot ( t , y )hold
10
f o r ( h = 0 . 1 )12 c le a r y h a t ;
s t e p = 1 ;14 y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t = 0: h : 0 . 5 )16 y h at ( st ep +1)=y hat ( st ep )+h∗d e r a n ( t , y h a t ( s t e p ) ) ;
step=step+1;18 end
20 plot ( 0 : h : 0 . 5 , y h a t ( 1 : end−1) , ’r-*’ )
22
c l e a r y h a t ;24 s t e p = 1 ;
y h a t ( s t e p )= s o l a n ( 0 ) ;26 f o r ( t = 0: h : 0 . 5 )
c2=1;28 b1=1/2;
b2=1/2;30 K1=der an ( t , y hat ( st ep ) ) ;
K2=d e r a n ( t+h∗c2 , y hat ( st ep )+h∗c2∗K1);32
y hat ( st ep +1)=y hat ( st ep )+h∗( b1∗K1+b2∗K2);34 step=step+1;
end
36
plot ( 0 : h : 0 . 5 , y h a t ( 1 : end−1) , ’k-*’ )38
40
end
42 xlabel ( ’t ’ )ylabel ( ’y(t)’ )
44
Page 47
-
8/19/2019 Syl Matlab
48/60
Syllabus Matlab 92 S RK4.M
legend ( ’Analytical’ , ’Euler h=0.1’ , ’ R K2 h = 0. 1 ’ )
91 s rk2 stab.m
Contenu du fichier s rk2 stab.m :
c le ar a l l
2 c l os e a l l
global lambda4
T=15;6 f o r ( h = 0 . 1 : 0 . 1 : T )
t = 0 : . 0 1 : T ;8 hold o f f
10 lambda=−1;R=abs (1+h∗ lambda+0.5∗( h∗ lambda )ˆ2 );
12 i f ( R>=1)
f p r i n t f ( 1 , ’Unstable’ ) ;14 e l s e
f p r i n t f ( 1 , ’ S ta b le \ n ’ ) ;16
end
18
20
y=f e v a l ( ’sol_an2’ , t ) ;22
plot ( t , y )24 hold
26 c l e a r y h a t ;s t e p = 1 ;
28 y h a t ( s t e p )= s o l a n 2 ( 0 ) ;
30 f o r ( t=0:h :T)c2=1;
32 b1=1/2;b2=1/2;
34 K1=der an2 ( t , y hat ( st ep ) ) ;K2=de r an 2 ( t+h∗c2 , y hat ( ste p)+h∗c2∗K1);
36
y hat ( st ep +1)=y hat ( st ep )+h∗( b1∗K1+b2∗K2);38 step=step+1;
end
40
plot ( 0 : h : T , y h a t ( 1 : end−1) , ’k-*’ )42
44
xlabel ( ’t ’ )46 ylabel ( ’y(t)’ )
t i t l e ( [ ’ R un ge - K u t ta 2 n d o r de r : h = ’ num2str ( h ) ’ | R( h \ l am bd a )|= ’ num2str (R ) ] ) ;48 pause
end
92 s rk4.m
Contenu du fichier s rk4.m :
Page 48
-
8/19/2019 Syl Matlab
49/60
Syllabus Matlab 93 S RUNGE.M
c le ar a l l
2 c l os e a l l
hold o f f 4 t = 0 : . 0 1 : 0 . 6 ;
6 y=f e v a l ( ’sol_an’ , t ) ;
8 plot ( t , y )hold
10
f o r ( h = 0 . 1 5 )12 c l e a r y h a t ;
s t e p = 1 ;14 y h a t ( s t e p )= s o l a n ( 0 ) ;
f o r ( t = 0: h : 0 . 6 )16 c2=1;
b1=1/2;18 b2=1/2;
K1=der an ( t , y hat ( st ep ) ) ;20 K2=d e r an ( t+h∗c2 , y hat ( st ep )+h∗c2∗K1);
22 y h at ( st ep +1)=y hat ( st ep )+h∗( b1∗K1+b2∗K2);step=step+1;
24 end
26 plot ( 0 : h : 0 . 6 , y h a t ( 1 : end−1) , ’r-*’ )
28 c l e a r y h a t ;s t e p = 1 ;
30 y h a t ( s t e p )= s o l a n ( 0 ) ;f o r ( t = 0: h : 0 . 6 )
32 c2=1;b1=1/2;
34 b2=1/2;K1=der an ( t , y hat ( st ep ) ) ;
36 K2=der an ( t+h/ 2 , y ha t ( st ep )+h/2∗K1);K3=der an ( t+h/ 2 , y ha t ( ste p)+h/2∗K2);
38 K4=der an ( t+h/ 2 , y ha t ( st ep )+h∗K3);
40 y h at ( st ep +1)=y hat ( st ep )+h/6∗(K1+2∗K2+2∗K3+K4 ) ;step=step+1;
42 end
44 plot ( 0 : h : 0 . 6 , y h a t ( 1 : end−1) , ’k-*’ )
46
end
48 xlabel ( ’t ’ )ylabel ( ’y(t)’ )
50
legend ( ’Analytical’ , ’ R K2 h = 0. 2 ’ , ’ R K4 h = 0. 2 ’ )
93 s runge.m
Contenu du fichier s runge.m :
c lo se a l l
2 c l e ar a l l
hold o f f 4 X= [ − 3 : 0 . 5 : 3 ] ’ ;
Page 49
-
8/19/2019 Syl Matlab
50/60
Syllabus Matlab 95 S SPLINE2.M
Y=1./(1+X.ˆ2);6 x = −3 : . 0 1 : 3 ;
y=1./(1+x . ˆ 2 );8
yp=po l la g (X,Y, x ) ;10
plot ( x , y )12
hold
14 plot (X,Y, ’k*’ )plot (x , yp , ’g ’ )
16 legend ( ’funct’ , ’points’ , ’interp’ )
94 s ru spl.m
Contenu du fichier s ru spl.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 X= [ − 3 : . 1 : 3 ] ’ ;
Y=X. ˆ 3 ;6 Y=1./(1+X.ˆ2);
8 x = −3 : . 0 1 : 3 ;y=1./(1+x . ˆ 2 );
10
ys=spcub(X,Y,x)12
plot ( x , y )
14
hold
16 plot (X,Y, ’k*’ )plot (x , ys , ’g ’ )
18 legend ( ’funct’ , ’points’ , ’spline’ )
95 s spline2.m
Contenu du fichier s spline2.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 X = [ 0 : . 5 : 2 ] ’ ;
Y=s in (X);6
x = 0 : . 0 1 : 2 ;8 y=s in ( x ) ;
[ ys ,A,b]=spcub(X,Y,x )10
plot ( x , y )12
hold
14 plot (X,Y, ’k*’ )
plot (x , ys , ’g ’ )16 legend ( ’funct’ , ’points’ , ’spline’ )
Page 50
-
8/19/2019 Syl Matlab
51/60
Syllabus Matlab 98 S TAYLOR.M
96 s spline.m
Contenu du fichier s spline.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 X= [ − 3 : . 3 : 3 ] ’ ;
Y=X. ˆ 3 ;6
x = −3 : . 0 1 : 3 ;8 y=x . ˆ 3 ;
10 ys=spcub(X,Y,x)
12 plot ( x , y )
14 hold
plot (X,Y, ’k*’ )
16 plot (x , ys , ’g ’ )legend ( ’funct’ , ’points’ , ’spline’ )
97 s stable.m
Contenu du fichier s stable.m :
% SCRIPT s s t a b le 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6 c l e ar a l l
c lo se a l l
8 hold o f f N=10;
10 E(N)=0.0;f o r ( i=N: −1 : 2 )
12 E( i −1)=(1−E( i ))/ i ;end
14
16 plot (E )
18 E(N)=0.0+1e−3;f o r ( i=N: −1 : 2 )
20 E( i −1)=(1−E( i ))/ i ;end
22
hold
24
plot (E , ’r’ )26 xlabel ( ’No. iterations’)
ylabel ( ’E ’ )
98 s taylor.mContenu du fichier s taylor.m :
c le ar a l l
2 c l os e a l l
Page 51
-
8/19/2019 Syl Matlab
52/60
Syllabus Matlab 100 S UNSTABLE2.M
hold o f f 4 t = 0 : . 0 1 : 0 . 5 ;
6 y=f e v a l ( ’sol_an’ , t ) ;
8 plot ( t , y )
hold10
plot ( t ,−1+1∗t −1. 5∗ t .ˆ2+0. 5∗ t .ˆ 3 , ’r--’ )12
legend ( ’Analytical’ , ’Taylor’ )
99 s triang.m
Contenu du fichier s triang.m :
% SCRIPT s tr ia ng 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6
8 A= [ 2 0 0 ; 4 2 0 ; 4 , 8 , 1 2 ];b = [ 4 ; 2 ; − 4 ] ;
10 f o r w a r d c o l (A, b ) % Qu a r t er o n i ’ s b o o k
12 A= [ 2 0 0 ; 4 2 0 ; 4 , 8 , 1 2 ];b = [ 4 ; 2 ; − 4 ] ;
14 f o r w a r d r o w ( A, b ) % Qu a r t er o n i ’ s b o o k
16
A= [ 4 , 8 , 1 2 ; 0 2 4 ; 1 0 2 ] ;18 b = [ 4 ; 2 ; 4 ] ;
b a c k w a r d c o l ( A, b ) % Qu a r t er o n i ’ s b o o k
100 s unstable2.m
Contenu du fichier s unstable2.m :
c le ar a l l
2 c l os e a l l
hold o f f 4 X= [ − 1 : . 1 : 0 . 5 ] ’ ;
randn ( ’state’ , 0 ) ;6 Y=s in (2∗ pi ∗X)+0.1∗randn ( s i z e ( X ) ) ;
x = −1 : . 0 1 : 0 . 5 ;8 y=s in (2∗ pi ∗x ) ;
yp=po l la g (X,Y, x ) ;10
f o r m=1: length (X)−112
[ yls , a]= l s p o l (X,Y,m, x ) ;14
plot ( x , y )16
hold
18 plot (X,Y, ’k*’ )plot (x , yp , ’g ’ )
Page 52
-
8/19/2019 Syl Matlab
53/60
Syllabus Matlab 102 EX5.M
20 plot (x , yls , ’k’ )
22 legend ( ’funct’ , ’points’ , ’interp’ , ’least squares’ , 4 )t i t l e ( [ ’ L ea st - s q u a re s o r de r = ’ num2str ( m ) ] )
24 a xis ( [ − 1 0 . 5 −3 2 . 5 ] )pause
26
end
101 s unstable.m
Contenu du fichier s unstable.m :
% SCRIPT s u n s t a b le 2
% Co ur s d e C a l c u l énumrique MATH 0314 % Co p y r i g h t ( c ) G i a n luc a B on te mp i , DI −ULB
% J a n u a r y 2 0 0 2 6 c l e ar a l l
c lo se a l l
8 hold o f f N=10;
10 E ( 1 ) = 0 . 3 6 7 8 7 9 ;f o r ( i =1:N)
12 E( i +1)=1−( i +1)∗E( i ) ;end
14
16 plot (E )
18 E(1)=E(1)+1e−6;f o r ( i =1:N)
20 E( i +1)=1−( i +1)∗E( i ) ;end
22
hold
24
plot (E , ’r’ )26 xlabel ( ’No. iterations’)
ylabel ( ’E ’ )
TP
102 ex5.m
Contenu du fichier ex5.m :
function ex5(A,B)2 %ex5 : é r a l i s e l a s o l ut i o n de l ’ e x er c ic e
%A f f i c h e l a somme , p u i s l a é d i f f r e n c e e t e nf in l e p ro du it de A et B 4 disp ( ’ S om me d es d eu x m a tr i ce s ’ )
A∗B6 disp ( ’éD i ff r en c e : A - B ’)
A−B8 disp ( ’Produit A*B’ )
A∗B
Page 53
-
8/19/2019 Syl Matlab
54/60
Syllabus Matlab 105 EX8.M
103 ex6.m
Contenu du fichier ex6.m :
% S c ri p t a f f i c h a n t s ur l e meme g ra ph e l e s deux f o n ct i o n s x . ∗ x . ∗ c os ( x )2 % e t x . ∗ x . ∗ s i n ( x )
x = −2 : 0 . 1 : 2 ;4 y1=x .∗ x .∗ co s ( x ) ;
y2=x .∗ x .∗ s in ( x ) ;6 plot (x , y1 )
hold ;8 plot (x , y2 )
hold ;
104 ex7.m
Contenu du fichier ex7.m :
function r=ex7(n)2 % F o n c t i o n é r s ol v a nt l ́e ’ q u a t i on x . ∗ x −x −1
% n e s t l e Nombre d ’ ́e i t r a t i o n édemands .4 x=2;
xprec=2;6 f o r ( i =1:n)
xprec=x;8 x=1+1./x ;
end
10 i f abs ( x−x p r e c )
-
8/19/2019 Syl Matlab
55/60
Syllabus Matlab 109 EX12.M
106 ex9.m
Contenu du fichier ex9.m :
function r=ex9(X,k)2 % S c r i p t de l ’ e x e r ci c e 9
4 [ l , c ]= s i z e (X);i f k>c | k1
8 disp ( ’ C et t e f o nc t io n n e f o nc t io n ne q u a v ec d es v ec t eu rs , p as d es m a tr i ce s ’ )e l s e
10 p r o d u i t = 1 ;f o r ( i =1:k−1)
12 p r o d u i t=p r o d u i t .∗X(1 , i ) ;end
14 f o r ( i=k+1:c)p r o d u i t=p r o d u i t .∗X(1 , i ) ;
16 endr=produi t ;
18 end
107 ex10.m
Contenu du fichier ex10.m :
function r=ex10 (c ,d , t )2 % b l ah , b l a h
D=on es ( 1 , t ). ∗ d ;4 C=one s (1 ,t −1).∗ c ;
A=diag (D);6 A=A+diag (C, −1 );
A=A+diag ( C , 1 ) ;8 r=A;
108 ex11.m
Contenu du fichier ex11.m :
function [ x1 , x2]=ex11(a , b , c)2 % C e tt e f o n c t i o n é r s o u t un ́e q u t a t i o n du èdeuxime é d e g r
% ax .∗ x+bx+c=0 en pre nan t comme édonne s e s c o e f f i c i e n t s a , b e t c 4 % Les s o l u t i o n s s on t é r e nv oy e s d an s x 1 e t x2
6 d=b .∗b−4∗a∗c ;x1=(−b+sqrt ( d ) ) / ( 2∗ a ) ;
8 x2=(−b−sqrt ( d ) ) / ( 2∗ a ) ;
109 ex12.m
Contenu du fichier ex12.m :
function r=ex12(A,n)2 % 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 nc ti o n e x er c ic e 1 0 pour éé g nr er l a m at r ic e 4 B=on es ( s i z e ( A ) ) ;
Aexposan t=ones ( s i z e ( A ) ) ;6 f o r ( i =1:n)
Page 55
-
8/19/2019 Syl Matlab
56/60
Syllabus Matlab 113 GSEID.M
Aexposant= Aexposant∗A;8 B=B+Ae xp os an t ;
end ;10 r=B;
Autres
110 gacol.m
Contenu du fichier gacol.m :
function [M]= ga co l (M, c , s , j1 , j2 , i , k)2 f o r j=j1 : j2
t 1=M( j , i ) ;4 t 2=M( j , k ) ;
M( j , i)=c∗ t1−s∗ t2 ;
6 M( j , k)=s∗ t1+c∗ t2 ;end
8 return
111 garow.m
Contenu du fichier garow.m :
function [M]=gar ow (M, c , s , i , k , j1 , j 2 )2 f o r j=j1 : j2
t 1=M( i , j ) ;4 t 2=M( k , j ) ;
M( i , j )=c∗ t1−s∗ t2 ;6 M(k , j )=s ∗ t1+c∗ t2 ;
end
8 return
112 givcos.m
Contenu du fichier givcos.m :
function [ c , s ] = g i v c o s ( x i , x k )2 i f ( xk==0), c =1; s =0; e ls e ,
i f abs ( x k ) > abs ( xi )4 t=−xi /xk ;
s=1/sqrt (1+t ˆ2 );6 c=s∗ t ;
e l s e
8 t=−xk/x i ;c=1/sqrt (1+t ˆ2 );
10 s=c∗ t ;end
12 end
return
113 gseid.m
Contenu du fichier gseid.m :
Page 56
-
8/19/2019 Syl Matlab
57/60
Syllabus Matlab 115 HOUSHESS.M
function X=gse id (A,B, P, delt a , max1)2 % I n p u t −A i s an N x N n o n s in g ul a r m at ri x
% −B i s an N x 1 m at ri x 4 % −P i s an N x 1 m at ri x ; t he i n i t i a l g ue ss
% −d e lt a i s t he t ol er an ce f or P 6 % −max1 i s t h e maximum n umb er o f i t e r a t i o n s
% Output −X i s an N x 1 m at ri x : t h e j a c o b i a pp ro xi ma ti on t o8 % 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:max1f o r j =1:N
14 i f j==1X(1)=(B(1)−A(1 ,2 :N)∗P ( 2 : N ) ) /A ( 1 , 1 ) ;
16 e l s e i f j==NX(N)=(B(N)−A ( N , 1 : N−1)∗(X(1:N−1)) ’)/A(N,N) ;
18 e l s e
% X c o n t a in s t h k t h a pp r ox i ma t io n s and P t h ( k −1 ) s t 20 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
22 end
e r r =abs (norm(X’−P ) ) ;24 r e l e r r =e r r / (norm(X)+eps ) ;
P=X ’ ;26 i f ( err
-
8/19/2019 Syl Matlab
58/60
-
8/19/2019 Syl Matlab
59/60
Syllabus Matlab 120 VHOUSE.M
f o r k=1:n−14 [ c (k ) , s (k)]= gi vc os (H(k , k ) ,H(k+1,k ) ) ;
H=garow (H, c (k ) , s (k ) ,k , k+1,k , n ) ;6 end
R=H; Q=pro dg iv (c , s ,n );8 return
119 secant.m
Contenu du fichier secant.m :
function [ xvect , xd if , fx , nit ]=s ec an t (xm1, x0 , nmax, t o l l , fun )2 function [ xvect , xd if , fx , nit ]=s ec an t (xm1, x0 , nmax, t o l l , fun )
%x0 , xm1 : édonnes i n i t i a l e s 4 %nmax : nombre d ’ ́e i t r a t i o n s maximum d e l a émthode
% t o l é t o l r an c e du t e s t d ’ ̂e a r r t 6 %f un : l a f o n c t i o n do nt on r e ch e rc h e une r ac i ne
8 x=xm1 ;fxm1=eval ( fun ) ;
10 xve ct =[x ] ;fx =[fxm1 ] ;
12 x=x0 ;fx0=eval ( fun ) ;
14 xvect =[xvect ; x ] ;fx =[fx ; fx0 ] ;
16 e r r = t o l l + 1;ni t =0;
18 x d i f = [ ] ;while ( n i t < nmax & er r > t o l l ) ,
20 ni t=ni t +1;x=x0−f x 0 ∗( x0−xm1) /( fx0−fxm1 ) ;
22 xvect =[xvect ; x ] ;fnew=eval ( fun ) ;
24 fx =[f x ; fnew ] ;e r r =abs ( x0−x ) ;
26 x d i f = [ x d i f ; e r r ] ;xm1=x0 ;
28 fxm1=fx 0 ;x0=x ;
30 fx 0=fnew ;end
32 return
120 vhouse.m
Contenu du fichier vhouse.m :
function [ v , beta ] = v h o u s e ( x )2 n=length ( x ) ;
x=x/norm( x ) ;4 s=x ( 2 : n ) ’∗x ( 2 : n ) ;
v = [ 1 ; x ( 2 : n ) ] ;6 i f ( s ==0), beta =0 ;
e l s e8 mu=sqrt ( x ( 1 ) ˆ2 + s ) ;
i f ( x ( 1 )
-
8/19/2019 Syl Matlab
60/60
Syllabus Matlab 120 VHOUSE.M
12 v(1)=− s /( x(1)+mu) ;end
14 beta=2∗v ( 1 ) ˆ 2 / ( s+v ( 1 ) ˆ 2 ) ;v=v/v (1 ) ;
16 end
return