Ass 3 Doc
Ass 3 Doc
1) Jacobi
clear;
clc;
a = 0.7071;
b= 0.9806;
c = 0.1961;
d = 0.669;
F= zeros(21,21);
B = zeros(21,1);
Y = zeros(21,1);
A = zeros(21,1);
F(1,1) = -1;F(1,3) = -a;
F(2,2) = -1;F(2,6) = 1;F(2,3) = -a;
F(3,5) = 1;F(3,3) = a;
F(4,4) = -1;F(4,8) = 1;F(4,7) = a;
F(5,5) =-1; F(5,7) = -a;
F(6,6) = -1; F(6,10) = 1;F(6,7) = -a;
F(7,9) = 1; F(7,7) = a;
F(10,10) = -1;F(10,11) = -a;
F(11,12) = 1; F(11,11) = a;
F(12,12) = -1; F(12,16) = 1;
F(13,13) = 1;
F(9,14) = b; F(9,15) = a;F(9,11) = -a, F(9,9) = -1;
F(8,14) =c; F(8,15) = a; F(8,11) =a;F(8,13) = 1; F(8,8) = -1;
F(14,18)= b;F(14,14)= -b; F(14,19) = 0.7433;
F(17,18) = c; F(17,14)= -c; F(17,17) = 1; F(17,19) = d;B(17,1)= 8000;
F(16,20) =1; F(16,16) = -1; F(16,15) = -a;
F(15,17) = -1; F(15,15) = -a;
F(20,20) = -1 ; F(20,19) = -0.7433; B(20,1) = -5000;
F(19,21) = -1; F(19,19) = -d;
F(18,18) = b;B(18,1) = 10000*cos(60);
F(21,21) = 1; F(21,18) = -c; B(21,1) = 10000*sin(60);
Figure (1)
for x = 1:100
for i = 1:21
temp = B(i,1);
for j = 1:21
if i~=j
temp = temp -(F(i,j)*A(j,1));
end
end
Y(i,1) = temp/F(i,i);
end
A = Y;
end
Y
M = Y-z
Output:
x_Jacobimethod =
1.0e+04 *
1.0000
5.1247
-1.4142
-5.7907
1.0000
4.1247
-1.4142
-4.7907
1.0000
3.1247
-4.4190
3.1247
0
-0.5846
-2.1940
3.1247
1.5514
0.5099
-1.4440
1.5733
0.9660
iteration =14
%-------------
x_guess=zeros(21,1)+1;
[x_GaussSeidelMethod,iteration]=GaussSeidelMethod(A,b_vector,x_guess) % Gauss Seidel
method
[x_SOR_w1,iteration]=SuccessiveOverRelaxation(A,b_vector,x_guess,0.5) % Gauss Seidel
method with w<1
[x_SOR_w2,iteration]=SuccessiveOverRelaxation(A,b_vector,x_guess,1.5) % Gauss Seidel
method with w>1
x_slash= A\b_vector
error_GaussSeidelMethod= x_slash-x_GaussSeidelMethod
error_SOR_w1= x_slash-x_SOR_w1
error_SOR_w2= x_slash-x_SOR_w2
% Part B - Gauss Seidel Method
Output:
x_GaussSeidelMethod =1.0e+04 *
1.0000
5.1247
-1.4142
-5.7907
1.0000
4.1247
-1.4142
-4.7907
1.0000
3.1247
-4.4190
3.1247
0
-0.5846
-2.1940
3.1247
1.5514
0.5099
-1.4440
1.5733
0.9660
iteration =11
iteration =74
x_SOR_w2 =1.0e+04 *
1.0000
5.1247
-1.4142
-5.7907
1.0000
4.1247
-1.4142
-4.7907
1.0000
3.1247
-4.4190
3.1247
0
-0.5846
-2.1940
3.1247
1.5514
0.5099
-1.4440
1.5733
0.9660
iteration =102
x_slash = 1.0e+04 *
1.0000
5.1247
-1.4142
-5.7907
1.0000
4.1247
-1.4142
-4.7907
1.0000
3.1247
-4.4190
3.1247
0
-0.5846
-2.1940
3.1247
1.5514
0.5099
-1.4440
1.5733
0.9660
error_GaussSeidelMethod =1.0e-10 *
-0.0728
-0.2910
0.1091
0.2910
-0.0728
-0.2183
0.1091
0.1455
-0.0728
-0.1091
0.1455
-0.0728
0
0
0.0364
-0.0728
-0.0182
0
0
0
0
error_SOR_w1 =1.0e-07 *
0.0150
-0.3631
0.4070
0.1090
-0.0899
-0.1088
0.0266
0.0032
-0.0031
-0.0031
0.0007
-0.0001
0
-0.0000
0.0000
-0.0001
-0.0000
0
0
0.0000
0
error_SOR_w2 =1.0e-07 *
0.4342
0.4061
0.0485
-0.0045
0.0063
0.0044
0.0028
-0.0006
0.0006
0.0006
0.0003
0.0001
0
0
0.0000
-0.0000
-0.0000
0
0
0
0
Comments:
Structure of the coefficient matrix:
Original structure of matrix of coefficient:
Cost of computing:
Gauss Seidel method converges more faster than Jacobi and SOR method
and takes less time for computing.
As the number of iterations for Gauss Seidel is less than that compared to
Jacobi and SOR method, from the point of accuracy, Gauss Seidel is
compared to be more accurate
2) Multivariate Newton
%NEWTONS MUTIVARIATIVE METHOD:
tolerance=0.5e-6;
max_iteration= 70;
tosolve=@rodheat;
syms T1 T2 T3 T4
L=10; %Length of the rod in m
h_p= 0.05; % m^-2
sigma= 2.7e-9; % sigma K^-3 m^-2
T_inf= 200; % K
T_A= 300 ;% K
T_B= 400 ;% K
delta_x= 2 ;% m
MatrixY= [-T_A+(2+h_p*delta_x^2)*T1+sigma*delta_x^2*T1^4-T2-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;
-T1+(2+h_p*delta_x^2)*T2+sigma*delta_x^2*T2^4-T3-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;
-T2+(2+h_p*delta_x^2)*T3+sigma*delta_x^2*T3^4-T4-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;
-T3+(2+h_p*delta_x^2)*T4+sigma*delta_x^2*T4^4-T_B-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;];
T_guess= zeros(4,1)+1;
tosolveT= [T1; T2; T3; T4] ;
% multivariate Newton's Method
[x_multivariateNewtonsMethod,iteration]=
multivariateNewtonsMethod(tosolve,MatrixY,tosolveT,T_guess,tolerance,max_iteration);
function EquationY=rodheat(T)
% input
L=10; % m
h_p= 0.05; % m^-2
sigma= 2.7e-9; % sigma K^-3 m^-2
T_inf= 200; % K
T_A= 300 ;% K
T_B= 400 ;% K
delta_x= 2 ;% m
EquationY=zeros(4,1);
EquationY(1)=-T_A +(2+h_p*delta_x^2)*T(1)+sigma*delta_x^2*T(1)^4-T(2)-
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
EquationY(2)=-T(1)+(2+h_p*delta_x^2)*T(2)+sigma*delta_x^2*T(2)^4-T(3)-
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
EquationY(3)=-T(2)+(2+h_p*delta_x^2)*T(3)+sigma*delta_x^2*T(3)^4-T(4)-
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
EquationY(4)=-T(3)+(2+h_p*delta_x^2)*T(4)+sigma*delta_x^2*T(4)^4-T_B -
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
end
function [x_n,iteration]=
multivariateNewtonsMethod(EquationY,MatrixY,VectorX,GuessX,tolerance,max_iteration)
% input EquationY is a function; MatrixY is a syms equation matrix of EquationY
error1=inf;x_n=GuessX; iteration=0;
while error1>tolerance && iteration<max_iteration
J=jacobian(MatrixY,VectorX);J=subs(J,VectorX,x_n);J=double(J);
dx = -J\EquationY(x_n);
x_n1=x_n+dx;
error1=max( abs(x_n1-x_n) );
x_n=x_n1;
iteration=iteration+1;
end
end
Output:
x_multivariateNewtonsMethod =
250.4827
236.2962
245.7596
286.4921
iteration = 6
b) Broydens Method:
tolerance=0.5e-6;
max_iteration= 80;
tosolve=@rodheat;
syms T1 T2 T3 T4
L=10; % m
h_p= 0.05; % m^-2
sigma= 2.7e-9; % sigma K^-3 m^-2
T_inf= 200; % K
T_A= 300 ;% K
T_B= 400 ;% K
delta_x= 2 ;% m
MatrixY= [-T_A+(2+h_p*delta_x^2)*T1+sigma*delta_x^2*T1^4-T2-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;
-T1+(2+h_p*delta_x^2)*T2+sigma*delta_x^2*T2^4-T3-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;
-T2+(2+h_p*delta_x^2)*T3+sigma*delta_x^2*T3^4-T4-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;
-T3+(2+h_p*delta_x^2)*T4+sigma*delta_x^2*T4^4-T_B-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;];
T_guess=zeros(4,1)+1;
tosolveT=[T1; T2; T3; T4] ;
[x_BroydensMethod,iteration]= BroydensMethod(tosolve,T_guess,tolerance,max_iteration)
function EquationY=rodheat(T)
% input
L=10; % m
h_p= 0.05; % m^-2
sigma= 2.7e-9; % sigma K^-3 m^-2
T_inf= 200; % K
T_A= 300 ;% K
T_B= 400 ;% K
delta_x= 2 ;% m
EquationY=zeros(4,1);
EquationY(1)=-T_A +(2+h_p*delta_x^2)*T(1)+sigma*delta_x^2*T(1)^4-T(2)-
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
EquationY(2)=-T(1)+(2+h_p*delta_x^2)*T(2)+sigma*delta_x^2*T(2)^4-T(3)-
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
EquationY(3)=-T(2)+(2+h_p*delta_x^2)*T(3)+sigma*delta_x^2*T(3)^4-T(4)-
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
EquationY(4)=-T(3)+(2+h_p*delta_x^2)*T(4)+sigma*delta_x^2*T(4)^4-T_B -
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
end
function [x1,iteration]= BroydensMethod(EquationY,GuessX,tolerance,max_iteration)
error1=inf; iteration=0; x1=GuessX; x0=GuessX-0.1; % x0 is assumed to be GuessX-0.1
[n,~]=size(GuessX); B=eye(n,n); % initial matrix is Identity Matrix
while error1>tolerance && iteration<max_iteration
dx=x1-x0; dF=EquationY(x1)-EquationY(x0);
B=B+ (dx-B*dF)*dx'*B / (dx'*B*dF) ; % new matrix for next solution
d_x= B* EquationY(x1);
x0=x1; x1=x1-d_x;
error1= max( abs(x1-x0) ); iteration=iteration+1;
end
end
Output: x_BroydensMethod
250.4827
236.2962
245.7596
286.4921
iteration = 16
C) fsolve
tosolve=@rodheat;
syms T1 T2 T3 T4
L=10; % m
h_p= 0.05; % m^-2
sigma= 2.7e-9; % sigma K^-3 m^-2
T_inf= 200; % K
T_A= 300 ;% K
T_B= 400 ;% K
delta_x= 2 ;% m
MatrixY= [-T_A+(2+h_p*delta_x^2)*T1+sigma*delta_x^2*T1^4-T2-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;
-T1+(2+h_p*delta_x^2)*T2+sigma*delta_x^2*T2^4-T3-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;
-T2+(2+h_p*delta_x^2)*T3+sigma*delta_x^2*T3^4-T4-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;
-T3+(2+h_p*delta_x^2)*T4+sigma*delta_x^2*T4^4-T_B-h_p*delta_x^2*T_inf-
sigma*delta_x^2*T_inf^4;];
T_guess=zeros(4,1)+1;
tosolveT=[T1; T2; T3; T4] ;
% fsolve function
T=fsolve(tosolve,T_guess)
x= 0:delta_x:10;
Temperature= [T_A; T; T_B];
optimset('display','iter','tolX',0.5e-6);
T=fsolve(tosolve,T_initial,options)
plot(x,Temperature)
function EquationY=rodheat(T)
% input
L=10; % m
h_p= 0.05; % m^-2
sigma= 2.7e-9; % sigma K^-3 m^-2
T_inf= 200; % K
T_A= 300 ;% K
T_B= 400 ;% K
delta_x= 2 ;% m
EquationY=zeros(4,1);
EquationY(1)=-T_A +(2+h_p*delta_x^2)*T(1)+sigma*delta_x^2*T(1)^4-T(2)-
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
EquationY(2)=-T(1)+(2+h_p*delta_x^2)*T(2)+sigma*delta_x^2*T(2)^4-T(3)-
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
EquationY(3)=-T(2)+(2+h_p*delta_x^2)*T(3)+sigma*delta_x^2*T(3)^4-T(4)-
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
EquationY(4)=-T(3)+(2+h_p*delta_x^2)*T(4)+sigma*delta_x^2*T(4)^4-T_B -
h_p*delta_x^2*T_inf-sigma*delta_x^2*T_inf^4;
end
Output:
Fsolve completed because the vector of function values is near zero as measured by the default
value of the function tolerance, and the problem appears regular as measured by the gradient.
1 10 338752 1 941 1
2 15 332406 2.5 927 2.5
T=
250.4827
236.2962
245.7596
286.4921
Comments:
The number of iterations for Newtons Method is less than that of the Broydens method. From the
point of accuracy, Broydens method is more accurate than fsolve and Newtons method as it
computes the whole Jacobian only at the first iteration and to do rank-one updates at other
iterations.
3) Q-R factorization
clc;
clear;
m1 = 100; % weight acting at the front region in kg
m2 = 200; % weight acting at the back region in kg
M = 1000; % mass of the car in kg
r = 0.9; % radius of gyration in m
jg = 810; % jg= M*r^2 substituting and getting the value
% suspension system springs
k1 = 18*10^3; %N/m
k2 = 20*10^3; %N/m
k3 = 20e3; %N/m
% lenght between the front, back and centre of gravity
l1 = 1; % in m
l2 = 1.5; % in m
F=[(k1+k2)/m1 0 -k2/m1 (k2*l1)/m1;...
0 (k1+k3)/m2 -k3/m2 (-k3*l2)/m2;...
-k2/M -k3/M (k2+k3)/M (k3*l2-k2*l1)/M;...
(k2*l1)/jg (-k3*l2)/jg (k3*l2-k2*l1)/jg (k2*l1^2+k3*l2^2)/jg];
% (A - lamda*I)X = O, where lamda = omega^2
x=[0;0;0;0];
tol = 5e-100;
max_iter=100;
tic
[eigval,eigvec,totaliter]=QRmethod(F,x,max_iter,tol);
t2=toc
natfreq = sqrt(eigval)
function [eigval,eigvect,iter_num]=QRmethod(F,x,max_iter,tol)
lamda_old=diag(F);
[n m]=size(F);
Qbar=eye(n);
for i=1:max_iter
[Q,R]=qr(F); % 'qr' MATLAB command gives Orthogonal and Upper trianggular matrix
F=R*Q;
Qbar=Qbar*Q;
errornorm=norm(lamda_old-diag(F),inf);
if errornorm<tol
break;
end
lamda_old=diag(F);
end
eigval=diag(F);
iter_num=i;
eigvect=Qbar;
end
Output:
eigenvectors_QR =
w=
20.1410
15.3794
5.6152
4.0653
t2 =
3.2403e-03
eigenvectors_QR =
Output:
natfreq =
mode_shapes =
natural_frequencies =
4.0653e+00
5.6152e+00
1.5379e+01
2.0141e+01
t1 =
1.6948e-04
Comments:
Computing time difference of MATLAB 'eig' and QR factorization method:
computing_time_difference = 3.1478e-03
This time difference shows that MATLAB 'eig' function is more efficient and quicker compared
to the QR factorization method.