1.0 Bisection Method
1.0 Bisection Method
0 BISECTION METHOD
2.0 NEWTON RAPHSON
1.0 GAUSS ELIMINATION
2.0 GAUS JORDAN
3.0 LU COMPOSITION
1.0 CURVE FITTING
2.0 LINEARIZATION
3.0 MAXIMUM
4.0 MINIMUM
1.0 EULER
2.0 RK 2
3.0 RK 4
1.0 TRAPEZOIDAL RULE SINGLE
2.0 TRAPEZOIDAL RULE MULTIPLE
3.0 SIMPSON 1/3 SINGLE
4.0 SIMPSON 1/3 MULTIPLE
5.0 SIMPSON 3/8 SINGLE
6.0 SIMPSON 3/8 MULTIPLE
BISECTION METHOD
EXAMPLE 1
% Group : PEC2216A2
% ID No. : 2019564227
% This code is to find the void fraction of bed using Bisection Method.
clear all
clc
y = @(x)(10*(x^3))-(0.15*(x^2))+(2.05*x)-1.9;
xl = -2;
xu = 2;
es = 0.1;
sign1 = y(xl)*y(xu);
if sign1 < 0
disp(' ')
else
disp(' ')
exit
end
disp(' ')
disp('iter xl xu xm ea ')
disp('_________________________________________________ ')
disp(' ')
iter = 0;
xm = 0.5*(xl + xu);
while ea > es
iter = iter+1;
sign2 = y(xl)*y(xm);
if sign2 < 0
xmold = xm;
xu = xm;
xl = xl;
if iter == 1
ea = ea;
else
ea = abs((xmnew-xmold)/xmold)*100;
end
% Display ( iter xl xu xm ea )
xmold = xm;
xl = xm;
xu = xu;
if iter == 1
ea = ea;
else
ea = abs((xmnew-xmold)/xmold)*100;
end
end
xm = xmnew;
% Display ( iter xl xu xm ea )
fprintf(' %i %3f %3f %3f %3f \n', iter, xl, xu,xm, ea)
endwhile
disp(' ')
% Display (xm)
EXAMPLE 2
clc
clear all
format short g
xl = 0;
xu = 0.11;
fxl = f(xl);
fxu = f(xu);
if signcheck < 0;
disp('theres only one root')
xm = (xu+xl)/2;
else signcheck> 0;
disp('theres no root')
end
i=0;
while(i<4)
xmold=xm;
xm = (xu+xl)/2;
if signcheck2 < 0;
xl = xm;
xu = xm;
end
epsilonA = abs((xmnew-xmold)/xmnew)*100;
i= i+1;
end
EXAMPLE 3
clear all
clc
f=@(x)120*x-(5/6)*x^3-220;
ea=100;
es=0.01;
xL=0;
xU=2;
sign1=f(xL)*f(xU);
if sign1<0
disp('')
else
disp('')
end
disp ('')
disp('iter xL xU xm ea')
disp('---------------------------------------------')
disp('')
iter = 0;
xm=0.5*(xL+xU);
while ea > es
iter = iter + 1;
sign2 = f(xL)*f(xm);
if sign2 < 0
xmold = xm;
xU=xm;
xL=xL;
xmnew = 0.5*(xL+xU);
if iter==1
ea=ea;
else
ea = abs((xmnew-xmold)/xmold)*100;
end
% disp([iter xL xU xm ea])
elseif sign2>0
xmold =xm;
xL=xm;
xU= xU;
xmnew = 0.5*(xL+xU);
if iter ==1
ea = ea;
else
ea = abs ((xmnew-xmold)/xmold)*100;
end
%disp {(iter xL xU xm ea)}
end
xm = xmnew;
fprintf('%i %3f %3f %3f %3f \n', iter, xL, xU, xm, ea)
end
disp('')
%disp (xm)
% Group : PEC2216A2
% ID No. : 2019564227
% This code is to find the void fraction of bed using Newton Raphson Method.
clear all
clc
x0 = 1;
es = 0.1;
x = x0;
y = @(x)(10*(x^3))-(0.15.*(x^2))+(2.05*x)-1.9;
iter = 0;
disp('iter x ea ')
disp('_________________________ ')
disp('')
while ea > es
iter = iter+1;
xold = x;
x = x-(y(x)/dy(x));
xnew = x;
if iter == 1
ea = ea;
else
ea = abs((xnew-xold)/xnew)*100;
endif
endwhile
disp(' ')
fprintf('The root of this equation is %5f by iteration of %i with error of %3f
\n',x,iter,ea)
EXAMPLE 2
x = 4.2;
epsilonS = 0.0001;
epsilonA = 100;
i=0;
disp('i x x0 epsilonA');
x0=x;
i=i+1;
f = (-2)+(6*x)-(4*(x^2))+(0.5*(x^3));
df= 6-(8*x)+(1.5*(x^2));
x = (x0)-(f/df);
disp([i x x0 epsilonA]);
end
EXAMPLE 3
format short g
clear all
clc
x = 0.05;
disp('i x x0 epsilonA');
i=0;
while(i<4)
x0=x;
i = i+1;
df =3*(x^2)-(0.33*x);
x = x0 - (f/df);
display([i x x0 epsilonA])
end
EXAMPLE 4
clear all
clc
x0=3;
es=1;
ea=100;
x=x0;
iter=0;
disp ('-------------------------')
disp ('')
while ea > es
iter = iter + 1;
xold = x;
x=x-(f(x)/df(x));
xnew=x;
if iter ==1
ea=ea;
else
ea =abs ((xnew-xold)/xnew)*100;
endif
disp ('')
fprintf ('the root of this equation is %5f by iteration of %i with error of %3f\n',x,iter,ea)
GAUS ELIMINATION METHOD
EXAMPLE 1
format short g
A = [ 1 7 -4 ; 4 -4 9 ; 12 -1 3]
pause;
B = [-51 ; 62 ; 8]
pause;
C = [A B]
pause;
pause;
pause;
%Backward subtitution
c = C(3,4)/C(3,3)
pause;
pause;
answer = [a;b;c]
EXAMPLE 2
clear all
clc
format short g
%% left side
%% right side
B = [106.8;177.2;279.2]
%% augemented form
C = [A B]
%% Backward elimination
c = C(3,4)/C(3,3)
b = (C(2,4)-C(2,3)*c)/C(2,2)
solution = [a;b;c]
EXAMPLE 3
% Gauss Elimination
clear all
clc
A = [ 1 1 -1 ; 1 -2 3 ; 2 3 1 ];
B = [ 4; -6; 7];
AB = [ A B ]
AB (2, :) = AB (2,:)-AB(1, :)
x2=A\B
EXAMPLE 4
clc; clear;
A = [1 -3 1;
2 -8 8;
-6 3 -15];
Ab = [ A b]
alpha = -Ab(2,1)/Ab(1,1)
Ab(2,:)= Ab(2,:)+alpha*Ab(1,:)
alpha = -Ab(3,1)/Ab(1,1)
Ab(3,:)= Ab(3,:)+alpha*Ab(1,:)
alpha = -Ab(3,2)/Ab(2,2)
Ab(3,:)= Ab(3,:)+alpha*Ab(2,:)
% Perform Back substituion to compute unknown
% Exact solution
solution=A\b
EXAMPLE 5
A = [1 -3 1;
2 -8 8;
-6 3 -15];
Ab = [ A b];
[m,n] = size(Ab);
disp('---------------------------------')
disp(Ab)
disp(' ')
% Performing pivoting
for j =1:m-1
for z =2:m
if Ab(j,j) == 0
t = Ab(j,:);
Ab(j,:) = Ab(z,:);
Ab(z,:) = t;
end
end
for i = j+1:m
disp('------------------------------------------------------')
disp(' ')
disp('*************************')
disp('*************************')
disp( Ab(i,:))
disp('*************************')
disp('*************************')
disp( Ab(i,:))
disp(' ')
end
end
disp(Ab)
% Initialize x vectors
x = zeros(1,m);
disp(' ')
disp('-----------------------------------------------')
for s =m:-1:1
c = 0;
for k =2:m
c = c + Ab(s,k)*x(k);
end
disp(['x(',num2str(s),')=',num2str(x(s))])
end
% Ab
% x'
disp(' ')
disp('------------------------------')
x2 = A\b
GAUSS JORDAN METHOD
EXAMPLE 1
format short g
A = [ 1 7 -4 ; 4 -4 9 ; 12 -1 3]
pause
B = [-51 ; 62 ; 8]
pause
C = [A B]
pause
pause
pause
%make C(2,2) = 1
C(2,:) = C(2,:)/C(2,2)
pause
%make C(3,3) = 1
C(3,:) = C(3,:)/C(3,3)
pause
%zero column 3
pause
pause
%zero column 2
clear all
clc
format short g
%% left side
%% right side
B = [106.8;177.2;279.2]
%% augemented form
C = [A B]
C(1,:) = C(1,:)/C(1,1)
C(2,:) = C(2,:)/C(2,2)
C(3,:) = C(3,:)/C(3,3)
pause
EXAMPLE 3
A = [1 -3 1;
2 -8 8;
-6 3 -15];
Ab = [ A b];
[m,n] = size(Ab);
% Performing pivoting
for j =1:m-1
for z =2:m
if Ab(j,j) == 0
t = Ab(1,:); %
Ab(1,:) = Ab(z,:); %
Ab(z,:) = t;
end
end
for i = j+1:m
Ab
end
end
for j = m:-1:2
for i = j-1:-1:1
end
end
for s =1:m
Ab(s,:) = Ab(s,:)/Ab(s,s);
x(s) = Ab(s,n);
end
Ab
x'
disp(' ')
x2 = A\b
disp(' ')
solution =rref(Ab)
LU DECOMPOSITION METHOD
EXAMPLE 1
format short g
A = [ 1 7 -4 ; 4 -4 9 ; 12 -1 3]
U = [A]
pause
clc
pause
pause
% Calculate L32
pause
%Zero at second Column
%the l values
[A]
L21 = A(2,1)/A(1,1)
L31 = A(3,1)/A(1,1)
LU = [L*U]
[A]
pause
clc
disp('[L][Z]=[C]')
[L]
%right side of the matrix
C = [-51 ; 62 ; 8]
LC = [L C]
B = [A C]
z1 = C(1,1)
z2 = C(2,1) - (L(2,1)*z1)
pause
clc
disp('[U][X]=[Z]')
[U]
Z = [z1 ; z2 ; z3]
ZU = [Z U]
X=[x1 ; x2 ; x3]
EXAMPLE 2
clear all
clc
format short g
%% left side
U = [A]
l32 = U(3,2)/U(2,2)
l21 = A(2,1)/A(1,1)
l31 = A(3,1)/A(1,1)
U = [U(1,:);U(2,:);U(3,:)]
%% LU must be same with A
LU = [L*U]
if LU == A
else
end
%% right side
C = [106.8;177.2;279.2]
%% to find Z
%%[L][Z] = [C]
LC = [L C]
Z1 = LC(1,4)
Z2 = LC(2,4) - (LC(2,1)*Z1)
Z = [Z1;Z2;Z3]
%% to find X
%%[U][X] = [Z]
UZ = [U Z]
X3 = UZ(3,4)/UZ(3,3)
X2 = (UZ(2,4)- UZ(2,3)*X3)/UZ(2,2)
X = [X1;X2;X3]
EXAMPLE 3
% LU Decomposition
clear all
clc
A = [ 1 1 -1; 1 -2 3; 2 3 1];
B = [ 4; -6; 7];
AB = [ A B]
[L, U, P] = lu (A)
disp ('--------------------------------------------')
y = L\(P*B);
x = U\y;
disp ('----------------------------------------------')
x2 = A\B
Gauss jordan
A = [1 -3 1;
2 -8 8;
-6 3 -15];
Ab = [ A b];
[m,n] = size(Ab);
% Performing pivoting
for j =1:m-1
for z =2:m
if Ab(j,j) == 0
t = Ab(1,:); %
Ab(1,:) = Ab(z,:); %
Ab(z,:) = t;
end
end
for i = j+1:m
Ab
end
end
for j = m:-1:2
for i = j-1:-1:1
end
end
for s =1:m
Ab(s,:) = Ab(s,:)/Ab(s,s);
x(s) = Ab(s,n);
end
disp('Gauss Jordan Method')
Ab
x'
disp(' ')
x2 = A\b
disp(' ')
solution =rref(Ab)
EXAMPLE 4
A = [1 -3 1;
2 -8 8;
-6 3 -15];
[ L, U ,P] = lu(A)
y = L\(P*b);
x = U\y
disp(' ')
disp('------------------------------')
x2 = A\b
CURVE FITTING
EXAMPLE 1
% Group : PEC2216A2
% ID NO. : 2019564227
% This code is to find the equation of the line that best fits the data and the r square
value
clear all
clc
% The following data shows the relationship between the viscosity of SAE 70 oil and
temperature
% After taking log of the data, define the temperature log(x) and the viscosity of SAE
70 oil log(y)
figure(1)
plot(x,y,'r--')
xlabel('Temperature (^oC)')
ylabel('Viscosity (N.s/m^2)')
grid on
disp('============================================')
disp('============================================')
disp('')
order = 1;
coefficient_linear= polyfit(x,y,order);
disp('')
disp(' a0 a1 ')
disp(coefficient_linear)
disp('')
Viscosity_Prediction_linear = polyval(coefficient_linear,x)
disp('')
disp('')
disp(' (y)')
disp('')
disp(compare_linear)
disp('')
% Compute r square
figure(2)
plot(x,y,'r',x,Viscosity_Prediction_linear,'b--')
xlabel('Temperature (^oC)')
ylabel('Viscosity (N.s/m^2)')
%
=================================================================
=============
disp('')
disp('============================================')
disp('============================================')
disp('')
order = 2;
coefficient_polynomial2= polyfit(x,y,order);
disp('')
disp(' a0 a1 a2')
disp(coefficient_polynomial2)
disp('')
Viscosity_Prediction_polynomial2 = polyval(coefficient_polynomial2,x)
disp('')
disp('The comparison between the actual viscosity and predicted viscosity')
disp('')
disp(' (y)')
disp('')
disp(compare_polynomial2)
disp('')
% Compute r square
%
=================================================================
=============
disp('')
disp('============================================')
disp('============================================')
disp('')
order = 3;
coefficient_polynomial3= polyfit(x,y,order);
disp('')
disp(' a0 a1 a2 a3')
disp(coefficient_polynomial3)
disp('')
Viscosity_Prediction_polynomial3 = polyval(coefficient_polynomial3,x)
disp('')
disp('')
disp(' (y)')
disp('')
disp(compare_polynomial3)
disp('')
% Compute r square
figure(3)
plot(x,y,'r',x,Viscosity_Prediction_linear,'k--',x,Viscosity_Prediction_polynomial2,'g',
x,Viscosity_Prediction_polynomial3,'bo')
xlabel('Temperature (^oC)')
ylabel('Viscosity (N.s/m^2)')
grid on
EXAMPLE 2
clc;clear;close;
%assign the independent var (t) and the dependent variable (v) to matrix
t= [1:1:15];
figure (1)
plot(t,v,'ro-')
grid on
order =3;
%plot the measured data (v) and the predicted data (predicted v)
figure (2)
hold on
grid on
disp ('')
sr = sum ((v-predicted_v).^2);
r_square = (st-sr)/st
disp ('')
T= [0 5 10 20 30 40];
figure (1)
plot (T,n,'r*-')
xlabel ('Temperature(T)')
ylabel ('Viscosity(n)')
grid on
xln=T;
yln=log(n);
coef= polyfit(xln,yln,order)
predicted_yln= polyval(coef,xln)
disp(' T ln n');
disp([T' predicted_yln'])
disp(' ');
figure(2)
hold on
plot (xln,yln,'g*-')
xlabel ('Temperatur(T)')
grid on
predictednifT = polyval(coef,15.8)
disp(' ');
beta1_valueB= coef(1)
alpha1_valueD= exp(coef(2))
disp(' ');
r_square= (St-Sr)/St
EXAMPLE 4
%Curve fitting
clc ;
clear all;
%Store the independent variables values (x) and the dependent in OCTAVE
x=[1:1:5];
y=[0.5,2,2.9,3.5,4];
figure(1)
plot(x,y,'bo-')
xlabel('x')
ylabel('y')
title('y vs x')
lnx=log(x)%linearization
figure(2)
plot(lnx,y)
xlabel('lnx')
ylabel ('y')
title('y vs lnx')
coef= polyfit(lnx,y,order)
y_predicted= polyval(coef,lnx)
%Plot the measured dependent variable data (y) and the predicted y
figure(3)
plot(lnx,y,'r*-',lnx,y_predicted,'go-')
xlabel('lnx')
ylabel ('y')
title('y vs lnx')
%Perform r square
St=sum((y-mean(y)).^2);
Sr=sum((y-y_predicted).^2);
r_square=(St-Sr)/St
answer=polyval(coef,log(3.3))
EXAMPLE 5
clear;
clc;
figure(1);
plot(c,k,'go-');
title('c vs k') ;
grid on;
coef=polyfit(c, k,1)
predicted_k=polyval(coef, c)
disp([k' predicted_k'])
figure(2);
plot(c, k, 'go-') ;
hold on;
kmax=1/coef(2)
cs=kmax*coef(1)
st=sum((k-mean(k)).^2);
sr=sum((k-predicted_k).^2);
rsq=(st-sr)/st
predict_k=polyval(coef, 2.8)
cnew=1./c
knew=1./k
figure(3)
plot(c,k,'go-');
hold on;
grid on;
coef=polyfit(cnew, knew, 2)
predicted_knew=polyval(coef, cnew)
st=sum((knew-mean(knew)).^2);
sr=sum((knew-predicted_knew).^2);
rsq=(st-sr)/st
EULER’S METHOD
EXAMPLE 1
clear all
clc
t0 = 0;
y0 = 1;
tEnd = 5;
h = 0.01;
N = (tEnd-t0)/h;
EXAMPLE 2
%%Euler
clear all
clc
t0 = 0;
y0 = 2;
tend = 4;
h = 1;
N = (tend -t0)/h;
T = [t0:h:tend]';
Y = zeros(N+1,1);
Y(1) = y0;
for i = 1:N
fi = 4*exp(0.8*T(i))-0.5*Y(i);
end
disp([YTrue Y])
plot (T,Y,'m',T,YTrue,'b')
ylabel('solution of Y')
grid on
True_error = abs((YTrue-Y));
Max_error = max(True_error)
EXAMPLE 3
for i = 1:N
fi = -2*T(i)*Y(i);
end
plot(T,Y,'g',T,YTrue,'b')
title (strcat('Comparison between the neumerical values solution h=', num2str(h),' &
exact solution'))
xlabel('Time')
grid on
True_error= abs((YTrue-Y));
%%heuns
clear all
clc
t0 = 0;
y0 = 2;
tend = 4;
h = 1;
N = (tend -t0)/h;
T = [t0:h:tend]';
Y = zeros(N+1,1);
Y(1) = y0;
for i = 1:N
k1 = ODEfunction(T(i),Y(i));
end
%% Compute the exact solution
disp([YTrue Y])
plot (T,Y,'m',T,YTrue,'b')
xlabel('Time')
ylabel('solution of Y')
grid on
True_error = abs((YTrue-Y));
Max_error = max(True_error)
EXAMPLE 2
t0 = 0;
y0 = 1;
tEnd = 5;
h = 0.5;
N = (tEnd-t0)/h;
T = (t0:h:tEnd)';
Y = zeros(N+1,1);
Y(1) = y0;
for i = 1:N
K1 = myODEfunc(T(i),Y(i));
end
disp([YTrue Y])
plot(T,Y,'g',T,YTrue,'b')
title (strcat('Comparison between the neumerical values solution h=', num2str(h),' &
exact solution'))
xlabel('Time')
grid on
True_error= abs((YTrue-Y));
function [ u ]= myODEfunc(T,Y)
End
RUNGEE KUTTA
EXAMPLE 1
t0 = 0;
y0 = 1;
tEnd = 5;
h = 0.05;
N = (tEnd-t0)/h;
T = (t0:h:tEnd)';
Y = zeros(N+1,1);
Y(1) = y0;
for i = 1:N
K1 = myODEfunc(T(i),Y(i));
K2 = myODEfunc(T(i) +h, Y(i)+h*K1);
K3 = myODEfunc (T(i)+h/2,Y(i)+h*K2/2);
K4 = myODEfunc(T(i)+h,Y(i)+h*K3);
end
function [ u ]= myODEfunc(t,y)
u= -2*t*y;
end
disp([YTrue Y])
plot(T,Y,'g',T,YTrue,'b')
title (strcat('Comparison between the neumerical values solution h=', num2str(h),' &
exact solution'))
xlabel('Time')
grid on
True_error= abs((YTrue-Y));
a= 8;
b= 30;
n= 1;
M = @(x)2000*log(140000./(140000-2100.*x))-9.8.*x;
Exact= 11061.34
%----------------------------------------------------
x=a:h:b;
%Display results
disp(' ')
disp([' For h=',num2str (h), ' % True Error =', num2str(err), 'n = ', num2str(n), ' I =',
num2str(ITrapSing), ' Trapezoidal Single'])
disp(' ')
%-------------------------------------------------------
%Trapezoidal Rule (Multiple)
n=40;
h= (b-a)/n;
x= a:h:b;
ITrapeMultiple= h/2*(M(x(1))+2*sum(M(x(2:n)))+M(x(n+1)))
%Display results
disp(' ')
disp([' for h =', num2str(h), '%True error=', num2str(err),' n=', num2str(n), ' I=',
num2str(ITrapeMultiple)' ' Trapezoidal Multiple'])
disp(' ')
%------------------------------------------------------------
n=4;
h=(b-a)/n;
x=a:h:b;
ISimps13Single = h/3*(M(x(1))+4*(M(x(2))+(M(x(n+1)))))
err=abs((Exact-ISimps13Single)/Exact)*100
%Display results
disp(' ')
disp([' for h =', num2str(h), '%True error=', num2str(err),' n=', num2str(n), ' I=',
num2str(ISimps13single)' ' Simpson 1/3 single'])
disp(' ')
%----------------------------------------------------------------
n=4;
h=(b-a)/n;
x= a:h:b;
ISimps13Multiple = h/3*(M(x(1))+4*sum(M(x(2:2:n)))+2*sum(M(x(3:2:n-
1)))+M(x(n+1)))
err= abs((Exact-ISimps13Multiple)/Exact)*100
%Display results
disp(' ')
disp([' for h =', num2str(h), '%True error=', num2str(err),' n=', num2str(n), ' I=',
num2str(ISimps13Multiple)' ' Simpson 1/3 Multiple'])
disp(' ')
%------------------------------------------------------------------
n=3;
h=(b-a)/n;
x=a:h:b;
ISimps38Single=3*h/8*(M(x(1))+3*M(x(2))+3*M(x(3))+M(x(n+1)))
err=abs((Exact-ISimps38Single)/Exact)*100
%Display results
disp(' ')
disp([' for h =', num2str(h), '%True error=', num2str(err),' n=', num2str(n), ' I=',
num2str(ISimps38Single)' ' Simpson 3/8 Single'])
disp(' ')
%-----------------------------------------------------------------
%simpson3/8(multiple)
n= 9
h=(b-a)/n;
x=a:h:b;
ISamps38Multiple=3*h/8*(M(x(1))+3*sum(M(x(2:3:n-
1)))+3*sum(M(x(3:3:n)))+2*sum(M(x(4:3:n-2)))+M(x(n+1)))
err=abs((Exact-ISamps38Multiple)/Exact)*100
%Display results
disp(' ')
disp([' for h =', num2str(h), '%True error=', num2str(err),' n=', num2str(n), ' I=',
num2str(ISimps38Multiple)' ' Simpson 3/8 Multiple'])
disp(' ')