You are on page 1of 31

Programs

REM: Grammer method---in MatLab Program


Dr.Shazad Shawki , M.Shilan , M.Bayan Omer

cl c
f or mat l ong g
A=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ A] r ow by r ow = ' )
B=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ B] = ' )
% f or exampl e t ake
%A=[ 4 - 1 1 ; 4 - 8 1 ; - 2 1 5 ] ; B=[ 7 - 21 15] ;
%t he answr er i s {2 4 3};
t i c
n=l engt h( B) ;
X=zer os( n, 1) ; DD=zer os( n, 1) ;
D=det ( A) ;
f or i =1: n
AA=A;
AA( 1: n, i ) =B;
DD( i , 1) =det ( AA) ;
end
X=DD/ D;
t oc
X


REM: LU- Fact or i zat i on met hod- - - i n Mat Lab Pr ogr am
Dr . Shazad Shawki , M. Shi l an , M. Bayan Omer

cl c
f or mat l ong g
A=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ A] r ow by r ow = ' )
B=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ B] = ' )
% f or exampl e t ake
%A=[ 4 - 1 1 ; 4 - 8 1 ; - 2 1 5 ] ; B=[ 7 - 21 15] ;
%t he answr er i s {2 4 3};
t i c
%The sol ut i on t o i s obt ai ned wi t h mat r i x di vi si on
n=l engt h( B) ; x=zer os( n, 1) ;
x = A\ B' ;
t oc
x
t i c
n=l engt h( B) ; X=zer os( n, 1) ;
%The sol ut i on i s act ual l y comput ed by sol vi ng t wo t r i angul ar
syst ems
[ L, U] = l u( A)
Y = L\ B' ;
X = U\ Y;
t oc
X


REM: Gauss- El i mi nat i on met hod- - - i n Mat Lab Pr ogr am
Dr . Shazad Shawki , M. Shi l an , M. Bayan Omer

cl c
f or mat l ong g
AB=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ A: B] r ow by r ow = ' )
%f or exampl e t ake AB=[ 4 - 9 2 5 ; 2 - 4 6 3 ; 1 - 1 3 4] ;
%t he answr er i s {6. 95 2. 5 - 0. 15};
t i c
[ n, m] =si ze( AB) ;
f or k=1: n- 1
f or i =k+1: n
b=AB( i , k) / AB( k, k) ;
f or j =1: m
AB( i , j ) =AB( i , j ) - b*AB( k, j ) ;
end
end
end
X=zer os( n, 1) ;
X( n) =AB( n, m) / AB( n, n) ;
f or i =n- 1: - 1: 1
S=0;
f or j =n: - 1: i +1
S=S+AB( i , j ) *X( j ) ;
end
X( i ) =( AB( i , m) - S) / AB( i , i ) ;
end
t oc
X


REM: Gauss- J or dan met hod- - - i n Mat Lab Pr ogr am
Dr . Shazad Shawki , M. Shi l an , M. Bayan Omer

cl c
f or mat l ong g
AB=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ A: B] r ow by r ow = ' )
%f or exampl e t ake AB=[ 4 - 9 2 5 ; 2 - 4 6 3 ; 1 - 1 3 4] ;
%t he answr er i s {6. 95 2. 5 - 0. 15};
t i c
[ n, m] =si ze( AB) ;
f or k=1: n- 1
f or i =k+1: n
b1=AB( i , k) / AB( k, k) ;
f or j =1: m
AB( i , j ) =AB( i , j ) - b1*AB( k, j ) ;
end
end
end
f or k=n: - 1: n- 1
f or i =k- 1: - 1: 1
b2=AB( i , k) / AB( k, k) ;
f or j =m: - 1: 1
AB( i , j ) =AB( i , j ) - b2*AB( k, j ) ;
end
end
end
X=zer os( n, 1) ;
f or i =1: n
X( i ) =AB( i , m) / AB( i , i ) ;
end
t oc
X


REM: J acobi - I t er at i ve met hod- - - i n Mat Lab Pr ogr am
Dr . Shazad Shawki , M. Shi l an , M. Bayan Omer

cl c
f or mat l ong g
A=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ A] r ow by r ow = ' )
B=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ B] = ' )
Y=i nput ( ' i nput t he i ni t i al val ues [ Y] = ' )
E=i nput ( ' t he maxi mumer r or you need = ' )
% f or exampl e t ake
%A=[ 4 - 1 1 ; 4 - 8 1 ; - 2 1 5 ] ; B=[ 7 - 21 15] ; Y=[ 0 0 0] ;
E=0. 000001 ;
%t he exact sol ut i on i s {2 4 3};
t i c
n=l engt h( B) ;
X=zer os( n, 1) ;
D=E; I =1;
whi l e D>E | D==E
f or i =1: n
S=0; D=0;
f or j =1: n
i f i ~=j
S=S+A( i , j ) *Y( j ) ;
end
end
X( i , 1) =( B( i ) - S) / A( i , i ) ;
end
D=sum( abs( X( : , 1) - Y( : ) ) ) ;
[ I , X' ]
Y( : ) =X( : , 1) ;
I =I +1;
end
t oc
Tot al i t er at i on=I - 1
X


REM: Gauss- Sei del met hod- - - i n Mat Lab Pr ogr am
Dr . Shazad Shawki , M. Shi l an , M. Bayan Omer

cl c
f or mat l ong g
A=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ A] r ow by r ow = ' )
B=i nput ( ' i nput t he coef f i ci ent s of mat r i x [ B] = ' )
Y=i nput ( ' i nput t he i ni t i al val ues [ Y] = ' )
E=i nput ( ' t he maxi mumer r or you need = ' )
% f or exampl e t ake
%A=[ 4 - 1 1 ; 4 - 8 1 ; - 2 1 5 ] ; B=[ 7 - 21 15] ; Y=[ 0 0 0] ;
E=0. 000001 ;
%t he exact sol ut i on i s {2 4 3};
t i c
n=l engt h( B) ;
X=zer os( n, 1) ;
D=E; I =1;
whi l e D>E | D==E
f or i =1: n
S1=0; S2=0; D=0;
f or j =1: n
i f i ~=j
i f i <j S1=S1+A( i , j ) *Y( j ) ; end
i f i >j S2=S2+A( i , j ) *X( j , 1) ; end
end
end
SUM=S1+S2;
X( i , 1) =( B( i ) - SUM) / A( i , i ) ;
end
D=sum( abs( X( : , 1) - Y( : ) ) ) ;
[ I , X' ]
Y( : ) =X( : , 1) ;
I =I +1;
end
t oc
Tot al i t er at i on=I - 1
X


REM: For war d Di f f er ences By Funct i on- - - i n Mat Lab Pr ogr am
Dr . Shazad Shawki , M. Shi l an , M. Bayan Omer

cl c
f or mat l ong g
syms x
k=i nput ( ' number of Dat a k = ' ) ;
F=i nl i ne( ' 1+x+2. *x. ^2' , ' x' ) ;
X=[ ( 0: k) ] , YY=F( X) ; Y=YY , B=zer os( k+1, k) ;
f or i =1: k
f or j =1: k- i +1
YY( j ) =YY( j +1) - YY( j ) ;
B( j , i ) =YY( j ) ;
end
di sp( [ {i }, ' - For war d- : ' ] )
di sp( ' - - - - - - - - - - - - - - - - - - - - - ' )
di sp( [ B( : , i ) ' ] )
end
di sp( ' x y Dy( For war d) s: ' )
di sp( ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - ' )
di sp( [ X' Y' B] )



REM: Backwar d Di f f er ences By Tabl e- - - i n Mat Lab Pr ogr am
Dr . Shazad Shawki , M. Shi l an , M. Bayan Omer

cl c
f or mat l ong g
X=i nput ( ' I nput e t he X- Dat a as a vect or r ows = ' ) ;
%f or exampl e t ake X=[ 0 1 2 3 4 5 6 7]
Y=i nput ( ' I nput e t he Y- Dat a as a vect or r ows = ' ) ;
%f or exampl e t ake Y=[ 1 2 8 27 64 125 216 373]
X , Y
k=l engt h( X) - 1; B=zer os( k+1, k) ; YY=Y;
f or i =1: k
f or j =1: k- i +1
YY( j ) =YY( j +1) - YY( j ) ;
B( i +j , i ) =YY( j ) ;
end
di sp( [ {i }, ' - Backwar d- : ' ] )
di sp( ' - - - - - - - - - - - - - - - - - - - - - - ' )
di sp( [ B( : , i ) ' ] )
end
di sp( ' x y Dy( Backwar d) s: ' )
di sp( ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - ' )
di sp( [ X' Y' B ] )




REM: Di vi ded Di f f er ences By Tabl e- - - i n Mat Lab Pr ogr am
Dr . Shazad Shawki , M. Shi l an , M. Bayan Omer

cl c
f or mat shor t g
X=i nput ( ' I nput e t he X- Dat a as a vect or r ows = ' ) ;
%f or exampl e t ake X=[ 0 1 2 4 5 ]
Y=i nput ( ' I nput e t he Y- Dat a as a vect or r ows = ' ) ;
%f or exampl e t ake Y=[ - 1 3 2 2 1]
k=l engt h( X) - 1 ; B=zer os( k+1, k) ; YY=Y;
X , Y
f or i =1: k
f or j =1: k- i +1
YY( j ) =( YY( j +1) - YY( j ) ) / ( X( j +i ) - X( j ) ) ;
B( j , i ) =YY( j ) ;
end
di sp( [ {i }, ' - Di vi ded- Di f f . - : ' ] )
di sp( ' - - - - - - - - - - - - - - - - - - - - - - - - - - ' )
di sp( [ B( : , i ) ' ] )
end
di sp( ' x y Dy( Di vi ded-
Di f f er ence) s: ' )
di sp( ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - ' )
di sp( [ X' Y' B] )


REM: Lagrange Interpolation ---in MatLab Program
Dr.Shazad Shawki , M.Shilan , M.Bayan Omer

cl c
f or mat l ong g
syms x
X=i nput ( ' I nput t he X- Dat a as a vect or r ows = ' ) ;
Y=i nput ( ' I nput t he Y- Dat a as a vect or r ows = ' ) ;
xs=i nput ( ' I nput t he number xs we need t o f i nd t he val ue ys of i t
= ' ) ;
%f or exampl e you can t ake: X=[ 1/ 3 1/ 4 1] ; Y=[ 2 - 1 7] ; xs=1/ 2 ;
k=l engt h( X) ; F=0 ;
f or i =1: k
M=1;
f or j =1: k
i f i ~=j
M=M*( x- X( j ) ) / ( X( i ) - X( j ) ) ;
end
end
F=F+M*Y( i ) ;
end
F0=char ( F) ; Pn=i nl i ne( F0, ' x' ) ;
di sp( ' x y ' )
di sp( ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' )
di sp( [ X' Y' ] )
di sp( ' The Lagr ange I nt er pol at i on i s Pn( x) =' )
pr et t y( si mpl i f y( F) )
di sp( ' ' )
di sp( [ ' Lagr ange I nt er pol at i on Pn( x) at x =
' , {xs}, ' i s' , {Pn( xs) }] )

x y
----------------------------------------------------------
0.333333333333333 2
0.25 -1
1 7

The Lagrange Interpolation is Pn(x) =
2
-38 x + 349/6 x - 79/6

'Lagrange Interpolation Pn(x)at x ='[0.5]'is' [6.41666666666667]
REM:
Newton Interpolatory Divided Diff.---in MatLab Program
Dr. Shazad Shawki, M. Shilan, M. Bayan Omer
clc
format long g
syms x
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
xs=input('Input the number xs we need to find the value ys of it
= ');
% for example take: X=[-1 1 3 4] ; Y=[2 -4 6 8] ; xs=-0.5 ;
k=length(X)-1 ; B=zeros(k+1,k) ; YY=Y ; Z=sym(zeros(k,1));
for i=1:k
for j=1:k-i+1
YY(j)=(YY(j+1)-YY(j))/(X(j+i)-X(j));
B(j,i)=YY(j);
end
end
for i=1:k
a=1;
for j=1:i
a=a*(x-X(j));
end
Z(i,1)=a;
end
F=0;
for i=1:k
F=F+Z(i,1)*B(1,i);
end
F0=char(F);Pn=inline(F0,'x');
disp(' x y Dy(Divided-Difference)s:')
disp(' ----------------------------------------------------')
disp([X' Y' B])
disp('The NDD Interpolation is Pn(x) =')
pretty((F+Y(1)))
disp(' ')
disp(['Newton Interpolatory Divided-Differences Pn(x) at x
=',{xs},'is',{Pn(xs)+Y(1)}])
x y Dy(Divided-Difference)s:
----------------------------------------------------------------
-1 2 -3 2 -0.6
1 -4 5 -1 0
3 6 2 0 0
4 8 0 0 0
The NDD Interpolation is Pn(x) =

-3 x - 1 + 2 (x + 1) (x - 1) - 3/5 (x + 1) (x - 1) (x - 3)

'Newton Interpolatory Divided-Differences Pn(x) at x ='
[-0.5] 'is' [-2.575]
REM:
Newtons Forward Divided-Diff. Interp ---in MatLab Program
Dr.Shazad Shawki, M.Shilan, M.Bayan Omer
clc
format long g
syms s
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
xs=input('Input the number xs we need to find the value ys of it =
');
% for example take: X=[-1 1 3 5] ; Y=[2 -4 6 10] ; xs=1.5 ;
h=X(end)-X(end-1) ; x0=X(1) ; sh=(xs-x0)/h ;
k=length(X)-1 ; B=zeros(k+1,k) ; YY=Y ; Z=sym(zeros(k,1));
for i=1:k
for j=1:k-i+1
YY(j)=(YY(j+1)-YY(j))/(X(j+i)-X(j));
B(j,i)=YY(j);
end
end
for i=1:k
ss=1;
for j=0:i-1
ss=ss*(s-j);
end
Z(i,1)=ss;
end
F=0;
for i=1:k
F=F+(Z(i,1)*h^i)*B(1,i);
end
F0=char(F);Pn=inline(F0,'s');
disp(' x y Dy(Divided-Diff.)s:')
disp(' ----------------------------------------------------')
disp([X' Y' B])
disp('The NFDD Interpolation is Pn(xs=x0+sh) =')
pretty(F+Y(1))
disp(' ')
disp(['Newton Forward Divided Diff. Interpolation Pn(x) for x =
',{xs},'is',{Pn(sh)+Y(1)}])
x y Dy(Divided-Diff.)s:
-------------------------------------------------------------------
-1 2 -3 2 -0.458333333333333
1 -4 5 -0.75 0
3 6 2 0 0
5 10 0 0 0
The NFDD Interpolation is Pn(xs=x0+sh) =
-6 s + 8 s (s - 1) (11/3) s (s - 1) (s - 2) + 2

'Newton Forward Divided Diff. Interpolation Pn(x) for x = '
[1.5] 'is' [-2.140625]
REM:
Newtons Backward Divided-Diff. Interp ---in MatLab Program
Dr.Shazad Shawki, M.Shilan, M.Bayan Omer
clc
format long g
syms s
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
xs=input('Input the number Xs we need to find the value ys of it =
');
% for example take: X=[0 0.2 0.4 0.6 0.8];
Y=[1 1.2214 1.49182 1.82212 2.22554]; xs=0.65;
h=X(end)-X(end-1) ; x0=X(end) ; sh=(xs-x0)/h ;
k=length(X)-1 ; B=zeros(k+1,k) ; YY=Y ; Z=sym(zeros(k,1));
for i=1:k
for j=1:k-i+1
YY(j)=(YY(j+1)-YY(j))/(X(j+i)-X(j));
B(j,i)=YY(j);
end
end
for i=1:k
ss=1;
for j=0:i-1
ss=ss*(s+j);
end
Z(i,1)=ss;
end
F=0;
for i=1:k
F=F+(Z(i,1)*h^i)*B(k-i+1,i);
end
F0=char(F);Pn=inline(F0,'s');
disp(' x y Dy(Divided-Diff.)s:')
disp('-----------------------------------------------------------')
disp([X' Y' subs(vpa(B,10))])
disp('The NBDD Interpolation is Pn(xs=x0+sh) =')
pretty(F+Y(k+1))
disp(' ')
disp(['Newton Backward Divided Diff. Interpolation Pn(x) for x =
',{xs},'is',{Pn(sh)+Y(k+1)}])

x y Dy(Divided-Diff.)s:
-------------------------------------------------------------------
0 1 1.107 0.61275 0.22625 0.06197916667
0.2 1.2214 1.3521 0.7485 0.2758333333 0
0.4 1.49182 1.6515 0.914 0 0
0.6 1.82212 2.0171 0 0 0
0.8 2.22554 0 0 0 0

The NBDD Interpolation is Pn(xs=x0+sh) =

20171 1646516023766649
----- s + ----------------- s (s + 1)
50000 45035996273704960

4968971588865279
+ ------------------- s (s + 1) (s + 2)
2251799813685248000

1786427852189651
+ -------------------- s (s + 1) (s + 2) (s + 3)
1801439850948198400
5011470557349067
+ ----------------
2251799813685248


'Newton Backward Divided Diff. Interpolation Pn(x) for x = '
[0.65] 'is' [1.91555051757813]
REM:
Newton-Gregory Forward Diff. Interp ---in MatLab Program
Dr.Shazad Shawki, M.Shilan, M.Bayan Omer

clc
format long g
syms s
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
xs=input('Input the number xs we need to find the value ys of it =
');
%for example take: X=[0 1 2 3 4 5]; Y=[0 -1 8 135 704 2375];xs=0.5;
h=X(end)-X(end-1) ; x0=X(1); sh=(xs-x0)/h ;
k=length(X)-1 ; B=zeros(k+1,k) ; YY=Y ; Z=sym(zeros(k,1));
for i=1:k
for j=1:k-i+1
YY(j)=YY(j+1)-YY(j);
B(j,i)=YY(j);
end
end
for i=1:k
ss=1;
for j=0:i-1
ss=ss*(s-j);
end
Z(i,1)=ss;
end
F=0;
for i=1:k
F=F+(Z(i,1)/factorial(i))*B(1,i);
end
F0=char(F);Pn=inline(F0,'s');
disp(' x y Dy(Forword)s:')
disp(' -------------------------------------------------------')
disp([X' Y' B])
disp('The NGFD Interpolation is Pn(xs=x0+sh) =')
pretty(F+Y(1))
disp(' ')
disp(['Newton-Gregory Forward Interpolation Pn(x) for x =
',{xs},'is',{Pn(sh)+Y(1)}])

x y Dy(Forword)s:
-------------------------------------------------------------------
0 0 -1 10 108 216 120
1 -1 9 118 324 336 0
2 8 127 442 660 0 0
3 135 569 1102 0 0 0
4 704 1671 0 0 0 0
5 2375 0 0 0 0 0


The NGFD Interpolation is Pn(xs=x0+sh) =

-s + 5 s (s - 1) + 18 s (s - 1)(s - 2) + 9 s (s - 1)(s - 2)(s - 3)

+ s (s - 1) (s - 2) (s - 3) (s - 4)


'Newton-Gregory Forward Interpolation Pn(x) for x = '
[0.5] 'is' [-0.15625]
REM:
Newton-Gregory Backward Diff. Interp ---in MatLab Program
Dr.Shazad Shawki, M.Shilan, M.Bayan Omer
clc
format long g
syms s
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
xs=input('Input the number xs we need to find the value ys of it =
');
% take for example: X=[0 1 2 3 4 5];Y=[0 -1 8 135 704 2375];xs=4.5;
h=X(end)-X(end-1) ; x0=X(end) ; sh=(xs-x0)/h ;
k=length(X)-1 ; B=zeros(k+1,k) ; YY=Y ; Z=sym(zeros(k,1));
for i=1:k
for j=1:k-i+1
YY(j)=YY(j+1)-YY(j);
B(i+j,i)=YY(j);
end
end
for i=1:k
ss=1;
for j=0:i-1
ss=ss*(s+j);
end
Z(i,1)=ss;
end
F=0;
for i=1:k
F=F+(Z(i,1)/factorial(i))*B(k+1,i);
end
F0=char(F);Pn=inline(F0,'s');
disp(' x y Dy(Backword)s:')
disp(' ----------------------------------------------------------')
disp([X' Y' B])
disp('The NGB Interpolation is Pn(xs=x0+sh) =')
pretty(F+Y(k+1))
disp(' ')
disp(['Newton Backward Interpolation Pn(x) for x =
',{xs},'is',{Pn(sh)+Y(k+1)}])

x y Dy(Backword)s:
-------------------------------------------------------------------
0 0 0 0 0 0 0
1 -1 -1 0 0 0 0
2 8 9 10 0 0 0
3 135 127 118 108 0 0
4 704 569 442 324 216 0
5 2375 1671 1102 660 336 120


The NGB Interpolation is Pn(xs=x0+sh) =

1671 s + 551 s (s + 1) + 110 s (s + 1)(s + 2)

+14 s (s + 1)(s + 2)(s + 3) + s (s + 1)(s + 2)(s + 3)(s + 4)+2375

'Newton Backward Interpolation Pn(x) for x = '
[4.5] 'is' [1344.09375]
REM:
Linear least Square Approximation---in MatLab Program
Dr.Shazad Shawki , M.Shilan , M.Bayan Omer

clc
format long g
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
% for example you can take: X=[1 3 4 6 8 9 11 14];
Y=[1 2 4 4 5 7 8 9];
m=length(X) ;
SX=sum(X); SY=sum(Y); SXY=sum(X.*Y); SX2=sum(X.^2);
a=(m*SXY-SX*SY)/(m*SX2-SX^2) ;
b=(SX2*SY-SXY*SX)/(m*SX2-SX^2) ;
disp(['sum(X)=',{SX},'sum(Y)=',{SY},'sum(X.Y)=',{SXY},
'sum(X^2)=',{SX2}])
disp(' ------------------------------------------------------')
disp(['a=',{a},'b=',{b}])
disp(' ')
disp(['The linear least square approximation is
y= ',{a},' x + ',{b}])


'sum(X)=' [56] 'sum(Y)=' [40]
'sum(X.Y)=' [364] 'sum(X^2)=' [524]

----------------------------------------------------------------

'a=' [0.636363636363636] 'b=' [0.545454545454545]


The linear least square approximation is

y= [0.636363636363636] x + [0.545454545454545]

REM:
Exponential least Square Approximation---in MatLab Program
Dr.Shazad Shawki, M.Shilan, M.Bayan Omer

clc
format long g
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
% for example you can take: X=[1 1.25 1.5 1.75 2];
Y=[5.1 5.79 6.53 7.45 8.46];
m=length(X) ;
if Y(:)>0
SX=sum(X); SLY=sum(log(Y)); SXLY=sum(X.*log(Y));
SX2=sum(X.^2);
A=(m*SXLY-SX*SLY)/(m*SX2-SX^2);a=A;
B=(SX2*SLY-SX*SXLY)/(m*SX2-SX^2);b=exp(B);
disp(['sum(X)=',{SX},'sum(log(Y))=',{SLY},
'sum(X.log(Y))=',{SXLY},'sum(X^2)=',{SX2}])
disp(' ---------------------------------------------------')
disp(['a=',{a},'b=',{b}])
disp(' ')
disp(['The Exponential least square approximation is
y=',{b},' Exp ',{a}])
else
'input new data of Y and then run the program one time more '
end


'sum(X)=' [7.5] 'sum(log(Y))=' [9.40534298061312]
'sum(X.log(Y))=' [14.4240892230653] 'sum(X^2)=' [11.875]

------------------------------------------------------------------

'a=' [0.505719603432908] 'b=' [3.07249271362163]


'The Exponential least square approximation is

y= [3.07249271362163] Exp([0.505719603432908])
REM:
Geometric least Square Approximation---in MatLab Program
Dr.Shazad Shawki, M.Shilan, M.Bayan Omer

clc
format long g
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
% for example you can take: X=[1 1.25 1.5 1.75 2];
Y=[5.1 5.79 6.53 7.45 8.46];
if X(:) & Y(:) >0
m=length(X) ;
SLX=sum(log(X)); SLX2=sum(log(X).^2); SLY=sum(log(Y));
SLXLY=sum(log(X).*log(Y));
A=(m*SLXLY-SLX*SLY)/(m*SLX2-SLX.^2);a=A;
B=(SLX2*SLY-SLX*SLXLY)/(m*SLX2-SLX.^2);b=exp(B);
disp(['sum(log(X))=',{SLX},'sum(log(Y))=',{SLY},
'sum(log(X).log(Y))=',{SLXLY},'sum(log(X)^2)=',{SLX2}])
disp(' ----------------------------------------------------')
disp(['a=',{a},'b=',{b}])
disp(' ')
disp(['The Geometric least square approximation is
y= ',{b},' x ^ ',{a}])
else
' input new data of X and Y and then run the program one time
more '
end

'sum(log(X))=' [1.88137162791774]
'sum(log(Y))=' [9.40534298061312]
'sum(log(X).log(Y))=' [3.75662667752127]
'sum(log(X)^2)=' [1.00781784241107]

------------------------------------------------------------------

'a=' [0.725686012277717] 'b=' [4.99287342780078]


'The Geometric least square approximation is

y= [4.99287342780078] x ^([0.725686012277717])
REM:
Hyperbolic least Square Approximation---in MatLab Program
Dr.Shazad Shawki, M.Shilan, M.Bayan Omer

clc
format long g
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
% for example you can take: X=[-1 2 3 5]; Y=[5/4 4/3 5/12 -1/2];
m=length(X) ;
if Y(:)~=0
SX=sum(X); S1Y=sum(1./Y); SX1Y=sum(X./Y); SX2=sum(X.^2);
a=(S1Y*SX2-SX1Y*SX)/(m*SX2-SX^2);
b=(m*SX1Y-SX*S1Y)/(m*SX2-SX^2);
disp(['sum(X)=',{SX},'sum(1/Y)=',{S1Y},'sum(X/Y)=',{SX1Y},
'sum(X^2)=',{SX2}])
disp(' --------------------------------------------------')
disp(['a=',{a},'b=',{b}])
disp(' ')
disp(['The Hyperbolic least square approximation is
y= 1/(',{a},' + x ',{b},')'])
else
'input new data of Y and then run the program one time more '
end

'sum(X)=' [9] 'sum(1/Y)=' [1.95]
'sum(X/Y)=' [-2.1] 'sum(X^2)=' [39]

-----------------------------------------------------------------

'a=' [1.266] 'b=' [-0.346]


'The Hyperbolic least square approximation is

y= 1/([1.266] + x [-0.346] )
REM: Trapezoidal Rule By Table---in MatLab Program
Dr. Shazad Shawki, M. Shilan, M. Bayan Omer

clc
format long g
X=input('Input the X-Data as a vector rows = ');
Y=input('Input the Y-Data as a vector rows = ');
% for example take: X=[1 1.05 1.1 1.15 1.2 1.25 1.3] ;
% Y=[1 1.0247 1.04881 1.07238 1.09544 1.11803 1.14017] ;
n=length(X)-1 ; a=X(1) ; b=X(end) ; h=(b-a)/n ;
Trap=(h/2)*(Y(1)+2*sum(Y(2:end-1))+Y(end));
disp(['The solution by Trapezoidal rule = ',{Trap}])


'The solution by Trapezoidal rule = ' [0.32147225]


----------------------------------------------------------------


REM: Trapezoidal Rule By Function---in MatLab Program

clc
format long g
syms x
n=input('Input the number of sub-intervals = ');
ab=input('Input the integral interval [a,b] = ');
f=input('Input the function = ');
% for above example take: n=12 and 120 and [a,b]=[1,1.3] ;
% and f(x)=sqrt(x) ;
h=(ab(2)-ab(1))/n ;
Y=[subs(f,ab(1):h:ab(2))];
Trap=(h/2)*(Y(1)+2*sum(Y(2:end-1))+Y(end));
Exact=subs(int(f,ab(1),ab(2)));
Error=subs(abs(Trap-Exact));
disp(['The solution by Trapezoidal rule = ',{Trap}])
disp(['The exact solution of the integral = ',{Exact}])
disp(['The Error = ',{Error}])

n=12
'The solution by Trapezoidal rule = ' [0.32148216690302]
:

'The exact solution of the integral = ' [0.321485368419253]

'The Error = ' [3.20151623284248e-006]
REM: Simpsons (1/3)h Rule By Table---in MatLab Program
Dr. Shazad Shawki, M. Shilan, M. Bayan Omer

clc
format long g
syms x
n=input('Input the number of sub-intervals = ');
ab=input('Input the integral interval [a,b] = ');
f=input('Input the function = ');
% for above example take: n=12 and 120 [a,b]=[1,1.3] ;
% and f(x)=sqrt(x) ;
h=(ab(2)-ab(1))/n ;
Y=[subs(f,ab(1):h:ab(2))] ; Yeven=Y(2:2:n) ; Yodd=Y(3:2:n-1) ;
Simp1h3=(h/3)*(Y(1)+4*sum(Yeven)+2*sum(Yodd)+Y(end));
Exact=subs(int(f,ab(1),ab(2)));
Error=subs(abs(Simp1h3-Exact));
disp(['The solution by Simpson 1/3 rule = ',{Simp1h3}])
disp(['The exact solution of the integral = ',{Exact}])
disp(['The Error = ',{Error}])


'The solution by Simpson 1/3 rule = ' [0.321485368028156]

'The exact solution of the integral = ' [0.321485368419253]

'The Error = ' [3.91097376706284e-010]
REM: Simpsons (3/8)h Rule By Table---in MatLab Program
Dr. Shazad Shawki, M. Shilan, M. Bayan Omer

clc
format long g
syms x
n=input('Input the number of sub-intervals = ');
ab=input('Input the integral interval [a,b] = ');
f=input('Input the function = ');
% for above example take: n=12 and 120 ; [a,b]=[1,1.3] ;
% and f(x)=sqrt(x) ;
h=(ab(2)-ab(1))/n ;
Y=[subs(f,ab(1):h:ab(2))] ;
S=0;
for i=1:n/3
S=S+Y(3*i-2)+3*(Y(3*i-1)+Y(3*i))+Y(3*i+1);
end
Simp3h8=(3*h/8)*S;
Exact=subs(int(f,ab(1),ab(2)));
Error=subs(abs(Simp3h8-Exact));
disp(['The solution by Simpson 3/8 rule = ',{Simp3h8}])
disp(['The exact solution of the integral = ',{Exact}])
disp(['The Error = ',{Error}])


'The solution by Simpson 3/8 rule = ' [0.321485367540105]

'The exact solution of the integral = ' [0.321485368419253]

'The Error = ' [8.79148309707034e-010]
REM: Romberg Rule By Table---in MatLab Program
Dr. Shazad Shawki, M. Shilan, M. Bayan Omer

clc
format long g
syms x
n=input('Input the number of sub-intervals = ');
ab=input('Input the integral interval [a,b] = ');
f=input('Input the function = ');
% for above example take: n=4 and 6 and 10 ; [a,b]=[0,pi] ;
% and f(x)=sin(x) ;
h=ab(2)-ab(1) ; R=zeros(n,n) ;
S=subs(f,ab(1))+subs(f,ab(2));
R(1,1)=h*S/2;
for i=1:n
I=i+1; h=h/2; hx=ab(1)+h; k=2^i;
for j=2:2:k
S=S+2*subs(f,hx);
hx=hx+2*h;
end
R(1,I)=h*S/2;
for j=1:i
R(j+1,I)=R(j,I)+(R(j,I)-R(j,i))/(4^j-1);
end
end
Exact=subs(int(f,ab(1),ab(2)));
Error=subs(abs(R(n,n)-Exact));
disp(['The solution by Romberg rule = ',{R(n,n)}])
disp(['The exact solution of the integral = ',{Exact}])
disp(['The Error = ',{Error}])


'The solution by Romberg rule = ' [2.00000554997967]

'The exact solution of the integral = ' [2]

'The Error = ' [5.54997967050497e-006]
REM: Euler method to solve I.V.P. ---in MatLab Program
Dr. Shazad Shawki, M. Shilan, M. Bayan Omer

clc
format long g
syms x y
n=input('Input the number of iteratives n = ');
ab=input('Input the interval [a,b] = ');
y0=input('Input the initial values y(x0)= ');
fxy=input('Input the function f(x,y) = ');
% for above example take: n=10 and 20 ; [a,b]=[0,2] ;
% with y(x0)=0.5 ; and f(x,y)=y-x^2+1 ;
h=(ab(2)-ab(1))/n;
ff=char(fxy); f=inline(ff,'x','y');
X=zeros(n,1); Y=zeros(n,1) ;
X(1,1)=ab(1); Y(1,1)=y0 ;
for i=2:n+1
X(i,1)=X(1,1)+(i-1)*h;
Y(i,1)=Y(i-1)+h*f(X(i-1,1),Y(i-1,1));
end
disp(' Xn Yn ')
disp(' ----------------------------------------------')
disp([X Y])



Xn Yn
----------------------------------------------
0 0.5
0.2 0.8
0.4 1.152
0.6 1.5504
0.8 1.98848
1 2.458176
1.2 2.9498112
1.4 3.45177344
1.6 3.950128128
1.8 4.4281537536
2 4.86578450432
REM:
Euler Modification method to solve I.V.P.
in MatLab Program
Dr. Shazad Shawki, M. Shilan, M. Bayan Omer

clc
format long g
syms x y
n=input('Input the number of iteratives n = ');
ab=input('Input the interval [a,b] = ');
y0=input('Input the initial values y(x0)= ');
fxy=input('Input the function f(x,y) = ');
% for above example take: n=10 and 20 ; [a,b]=[0,2] ;
% with y(x0)=0.5 ; and f(x,y)=y-x^2+1 ;
h=(ab(2)-ab(1))/n;
ff=char(fxy); f=inline(ff,'x','y');
X=zeros(n,1); Y=zeros(n,1) ;
X(1,1)=ab(1); Y(1,1)=y0 ;
for i=2:n+1
X(i,1)=X(1,1)+(i-1)*h;
Y(i,1)=Y(i-1)+h*f(X(i-1,1),Y(i-1,1));
Yav=(f(X(i-1,1),Y(i-1,1))+f(X(i,1),Y(i,1)))/2;
Y(i,1)=Y(i-1,1)+h*Yav;
Yav=(f(X(i-1,1),Y(i-1,1))+f(X(i,1),Y(i,1)))/2;
Y(i,1)=Y(i-1,1)+h*Yav;
end
disp(' Xn Yn ')
disp(' ----------------------------------------------')
disp([X Y])


Xn Yn
-------------------------------------------
0 0.5
0.2 0.8286
0.4 1.2124692
0.6 1.6461173624
0.8 2.1228354168528
1 2.63442487939412
1.2 3.17086720261962
1.4 3.71991972160117
1.6 4.26662189979663
1.8 4.79269196155148
2 5.27578957701591
REM:
RK4 method to solve I.V.P. --- in MatLab Program
Dr. Shazad Shawki, M. Shilan, M. Bayan Omer

clc
format long g
syms x y
n=input('Input the number of iteratives n = ');
ab=input('Input the interval [a,b] = ');
y0=input('Input the initial values y(x0)= ');
fxy=input('Input the function f(x,y) = ');
% for above example take: n=10 and 20 ; [a,b]=[0,2] ;
% with y(x0)=0.5 ; and f(x,y)=y-x^2+1 ;
h=(ab(2)-ab(1))/n ; Y=zeros(n,1) ;
ff=char(fxy); f=inline(ff,'x','y');
X=[ab(1):h:ab(2)]' ; Y(1,1)=y0 ;
for i=2:n+1
K1=h*f(X(i-1,1),Y(i-1,1));
K2=h*f(X(i-1,1)+h/2,Y(i-1,1)+K1/2);
K3=h*f(X(i-1,1)+h/2,Y(i-1,1)+K2/2);
K4=h*f(X(i-1,1)+h,Y(i-1,1)+K3);
Y(i,1)=Y(i-1,1)+(K1+2*(K2+K3)+K4)/6;
end
disp(' Xn Yn ')
disp(' ----------------------------------------------')
disp([X Y])


Xn Yn
----------------------------------------------
0 0.5
0.2 0.82929333333333
0.4 1.21407621066667
0.6 1.64892201704160
0.8 2.12720268494794
1 2.64082269272875
1.2 3.17989417023223
1.4 3.73234007285498
1.6 4.28340949831841
1.8 4.81508569457943
2 5.30536300069265

REM: Adams Four t h- or der pr edi ct or - cor r ect or met hod t o
Sol ve I . V. P. - - - i n Mat Lab Pr ogr am
Dr . Shazad Shawki , M. Shi l an , M. Bayan Omer

cl c
cl ear
f or mat l ong g
syms x y
ab=i nput ( ' I nput t he i nt er val [ a, b] = ' ) ;
y0=i nput ( ' I nput t he i ni t i al val ues y( x0) = ' ) ;
N=i nput ( ' I nput t he number of i t er at i ons N = ' ) ;
f xy=i nput ( ' I nput t he f unct i on f ( x, y) = ' ) ;
f ex=i nput ( ' I nput t he exact sol ut i on y( x) = ' ) ;
%f or exampl e t ake
%ab=[ 0, 2] ; y0=0. 5; N=10; f xy=y- x^2+1; f ex=( x+1) ^2- 0. 5*exp( x) ;
f f =char ( f xy) ; f =i nl i ne( f f , ' x' , ' y' ) ; f e=char ( f ex) ;
f E=i nl i ne( f e, ' x' ) ;
t =zer os( N+1, 1) ; w=zer os( N+1, 1) ; yt =zer os( N+1, 1) ;
h=( ab( 2) - ab( 1) ) / N; t ( 1, 1) =ab( 1) ; w( 1, 1) =y0;
t ( : , 1) =[ ab( 1) : h: ab( 2) ] ;
f or i =1: 3
k1=h*f ( t ( i , 1) , w( i , 1) ) ;
k2=h*f ( t ( i , 1) +h/ 2, w( i , 1) +k1/ 2) ;
k3=h*f ( t ( i , 1) +h/ 2, w( i , 1) +k2/ 2) ;
k4=h*f ( t ( i , 1) +h, w( i , 1) +k3) ;
w( i +1, 1) =w( i , 1) +( k1+2*( k2+k3) +k4) / 6;
end
f or i =4: N
W=w( i , 1) +h*( 55*f ( t ( i , 1) , w( i , 1) ) - 59*f ( t ( i - 1, 1) , w( i -
1, 1) ) +37*f ( t ( i - 2, 1) , w( i - 2, 1) ) - 9*f ( t ( i - 3, 1) , w( i - 3, 1) ) ) / 24;
w( i +1, 1) =w( i , 1) +h*( 9*f ( t ( i +1, 1) , W) +19*f ( t ( i , 1) , w( i , 1) ) -
5*f ( t ( i - 1, 1) , w( i - 1, 1) ) +f ( t ( i - 2, 1) , w( i - 2, 1) ) ) / 24;
end
yt =subs( f E, t ( : , 1) ) ; Er r or =abs( yt - w) ;
di sp( ' t yt
w Er r or ' )
di sp( ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' )
di sp( [ t , yt , w, Er r or ] )

You might also like