0% found this document useful (0 votes)
25 views21 pages

Ass 3 Doc

ass3doc

Uploaded by

Kh Md Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views21 pages

Ass 3 Doc

ass3doc

Uploaded by

Kh Md Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Assignment -3

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

(b) Gauss Siedal


% input
a=0.7071;
b=0.9806;
c =0.1961;
d=0.669;

b_vector=[0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 8000; 0; 0; -5000; 0; 10000*cosd(60);


10000*sind(60)];
Spy(F)
z = F\B
% partial pivoting, exchange rows------------
[n,n]=size(A);
for i=1:n-1
[~, row]= max(abs(A(i:n,i))); % row is row position number relatively
row= row+i-1; % now row is row position number absolutely
if row~= i
A([i row],:)=A([row i],:);b_vector([i row],:)=b_vector([row i],:);
end
end
A([16 15],:)=A([15 16],:);b_vector([16 15],:)=b_vector([15 16],:);
A([17 15],:)=A([15 17],:);b_vector([17 15],:)=b_vector([15 17],:);
A([20 19],:)=A([19 20],:);b_vector([20 19],:)=b_vector([19 20],:);
Figure(2)
Spy(F)
z = F\B

%-------------
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

function [x,iteration]= GaussSeidelMethod(A,b,x_guess)


[n,n]=size(A);x=x_guess;iteration_max=500;
tolerance= 0.5e-6;error1=1;iteration=0;
while error1>tolerance && iteration<iteration_max
for i=1:n % i -- row number
tosubtract=0;
for j=1:n % j -- the element in this row
if j~=i
tosubtract=tosubtract+A(i,j)*x(j);
end
end
x(i)= ( b(i)- tosubtract) /A(i,i);
end
iteration=iteration+1;
error2=abs(A*x-b);
error1=max(error2);
end
end
% Part C- Sor w>1 and w<1
function [x,iteration]= SuccessiveOverRelaxation(A,b,x_guess,w)
[n,n]=size(A);x1=x_guess;iteration_max=500;
tolerance= 0.5e-6;error1=1;iteration=0;x=zeros(n,1);
while error1>tolerance && iteration<iteration_max
for i=1:n % i -- row number
tosubtract=0;
for j=1:n % j -- the element in this row
if j~=i
tosubtract=tosubtract+A(i,j)*x1(j);
end
end
x(i)= ( b(i)- tosubtract) /A(i,i);
end
x1=x*w+x1*(1-w);iteration=iteration+1;
error2=abs(A*x1-b);
error1=max(error2);
end
end
% Part e- Matlab \
x_slash= A\b_vector

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

SOR Method w>1


x_SOR_w1 =
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 =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:

Condition of convergence is that the matrix is diagonally dominant, which


states that the coefficient of matrix is rearranged so that it is diagonally
dominant.
Structure of matrix of coefficient after partial pivoting:

Cost of computing:

For Jacobi Method, cost of computing is 2*(n^2) = (2*(21^2)) = 882.

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

tolerance=0.5e-6; max_iteration= 50;

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.

Norm of First-order Trust-region

Iteration Func-count f(x) step optimality radius


0 5 341318 946 1

1 10 338752 1 941 1
2 15 332406 2.5 927 2.5

3 20 316964 6.25 894 6.25

4 25 281005 15.625 810 15.6

5 30 207468 39.0625 604 39.1

6 35 121829 97.6563 162 97.7

7 40 58503.9 244.141 438 244

8 45 6751.47 280.998 115 610

9 50 57.0708 52.9899 9.96 610

10 55 0.00603793 5.80332 0.102 610

11 60 6.96953e-11 0.0605473 1.08e-05 610


12 65 3.79663e-26 6.49108e-06 4.72e-13 610

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 =

0.9953 0.0405 -0.0569 -0.0672


-0.0301 0.9665 0.2451 -0.0697
-0.0507 -0.1179 0.1812 -0.9750
0.0770 -0.2243 0.9507 0.1998

w=
20.1410
15.3794
5.6152
4.0653

t2 =

3.2403e-03

(b) Matlab eig function

eigenvectors_QR =

% using eig function


% calculating computing time
tic
[eigenvector, lamda]=eig(F,M);
t1=toc
% display results for MATLAB 'eig'
mode_shapes=eigenvector
natfreq=sqrt(diag(lamda))

Output:
natfreq =
mode_shapes =

-2.0556e-02 -1.4618e-02 -8.5053e-03 9.6392e-02


-1.0664e-02 3.1934e-02 -6.2113e-02 -2.9118e-03
-2.9815e-02 4.9601e-03 7.8990e-03 -4.9089e-03
7.5436e-03 3.0430e-02 1.4000e-02 7.4591e-03

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.

You might also like