Professional Documents
Culture Documents
16 mai 2004
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
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 :
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
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 :
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
Page 8
Syllabus Matlab
FLOSET.M
26
28
end
30
32
34
36
12
end
P=P ;
11
flo2real.m
Contenu du fichier flo2real.m :
10
12
14
16
18
20
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
18
20
22
24
26
28
30
32
34
36
38
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
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 ) ;
10
12
14
16
18
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
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 :
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
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
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 ]
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 :
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 :
30
model2.m
Contenu du fichier model2.m :
function z=model ( x , y )
2
31
model.2m.m
Contenu du fichier model.2m.m :
function y=model ( x )
2
Page 15
MODEL.2M.M
Syllabus Matlab
32
35
model.m
Contenu du fichier model.m :
function y=model ( x )
2
33
phi.m
Contenu du fichier phi.m :
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
14
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 ) ;
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;
plot ( I x , y+m. ( I x x ) , c )
plot line.m
37
i f ( x1 = x2 )
x min=min( x1 , x2 ) ;
x max=max( x1 , x2 ) ;
10
12
14
16
18
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 :
on=o n e s ( s i z e ( x ) ) ;
4
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
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
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 :
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
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 :
10
12
14
16
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
24
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
40
end
44
s arret.m
Contenu du fichier s arret.m :
% SCRIPT s i t e r .m
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
18
45
s assoc.m
Contenu du fichier s assoc.m :
% SCRIPT s a s s o c
2
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
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
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
36
38
end
48
s coniug.m
Contenu du fichier s coniug.m :
% SCRIPT s c o n i u g
2
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
46
48
end
49
s cost.m
Contenu du fichier s cost.m :
% SCRIPT s c o s t
2
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
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
50
52
51
s err arr.m
Contenu du fichier s err arr.m :
% SCRIPT s e r r a r r .m
2
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
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
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
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
36
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
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
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 :
60
s floset.m
Contenu du fichier s floset.m :
% SCRIPT s f l o s e t
2
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
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
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
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
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
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
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
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
36
38
end
66
s gradient.m
Contenu du fichier s gradient.m :
% SCRIPT s g r a d i e n t
2
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
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
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
69
s jacobi2.m
Contenu du fichier s jacobi2.m :
% SCRIPT s j a c o b i 2
2
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
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
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
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
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 = ) ;
10
12
75
s lu.m
Contenu du fichier s lu.m :
% SCRIPT s l u
2
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
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 )
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
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
81
spcub.m
Contenu du fichier spcub.m :
n=length (X) 1;
4
f o r ( i =1:n )
h ( i )=X( i +1)X( i ) ;
end
10
12
14
16
18
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 )
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
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
18
20
22
24
26
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
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
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
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
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
60
62
66
89
s refine.m
Contenu du fichier s refine.m :
% SCRIPT s r e f i n e
2
10
12
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
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
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
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
38
end
40
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
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
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
10
12
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
s triang.m
99
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
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
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
24
26
end
101
s unstable.m
10
12
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
Page 53
EX5.M
Syllabus Matlab
103
105
EX8.M
ex6.m
% 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
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
10
12
14
16
18
Page 54
Syllabus Matlab
106
109
EX12.M
ex9.m
10
12
14
16
18
107
ex10.m
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
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
Page 55
Syllabus Matlab
Aexposant = Aexposant A;
B=B+Aexposant ;
10
113
end ;
r=B ;
Autres
110
gacol.m
111
garow.m
112
givcos.m
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
Page 56
GSEID.M
Syllabus Matlab
115
HOUSHESS.M
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
10
115
houshess.m
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
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
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
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
10
12
14
16
18
20
22
24
26
28
30
32
120
vhouse.m
10
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