You are on page 1of 6

ME 408

Finite Element Approximation


Homework# 4
MATLAB implementation of the generalized Jacobi method:
Code Listing:
%Routine solves for eigen values and eigen vectors using the
JACOBI METHOD
%for a given pair of
%matrices, the matrices should be symmetric and positive
definite
%
close all;
clear all;
clc;
K1 = [ 10 4 5;4 6 4; 5 4 50];M1 = [5 1 0;1 10 5;0 5 7];
K2 = [3 1 8 5;1 6 2 9;8 2 2 3;5 9 3 7]; M2 = [5 0 8 5;0 6 2
9;8 2 4 0;5 9 ...
0 2];
%define constants
maxS = 15;% maximum number of cycles
K =K1; M =M1;%choose between the given stiffness and mass
matrices
rows =size(K,1);cols = size(K,2);
for s = 1:maxS
eps = 10^(-2*s);%Dynamic tolerance
for i=1:rows
for j=i+1:cols
fk = abs(K(i,j))/sqrt(K(i,i)*K(j,j));
fm = abs(M(i,j))/sqrt(M(i,i)*M(j,j));
if((fk >eps) | (fm >eps))
c1 = K(i,i)*M(i,j)-M(i,i)*K(i,j);
c2 = K(j,j)*M(i,j)-M(j,j)*K(i,j);
c3 = K(i,i)*M(j,j)-M(i,i)*K(j,j);
d = c3/2+sign(c3)*sqrt((c3/2)^2+c1*c2);
a = c2/d; b=-c1/d;
if(i==1 & j==2 & s==1)
X = eye(rows);
if(rem(rows,2)~=0)
temp = ceil(rows/2);
X(temp-1,temp+1)=a;
X(temp+1,temp-1)=b;

else
temp = rows/2;
X(temp,temp+1)=a;
X(temp+1,temp)=b;
end
end
%transform matrices K
K(:,i) = K(:,i) + b*K(:,j);
K(:,j) = a*K(:,i) + K(:,j);
K(i,:) = K(i,:) + b*K(j,:);
K(j,:) = a*K(i,:) + K(j,:);
%transform matrices M
M(:,i) = M(:,i) + b*M(:,j);
M(:,j) = a*M(:,i) + M(:,j);
M(i,:) = M(i,:) + b*M(j,:);
M(j,:) = a*M(i,:) + M(j,:);
%transform eigen vectors
X(:,i) =X(:,i) +b*X(:,j);
X(:,j) = a*X(:,i) + X(:,j);
end
end
end
%compute the eigen values
lambda = (diag(K)./diag(M))';
Flambda = max(abs((lambda.^s-lambda.^(s-1)))./
(abs(lambda.^(s-1))));
Fk =0;Fm =0;
for i=1:rows
for j=1:cols
if(abs(K(i,j)/sqrt(K(i,i)*K(j,j))) > Fk)
Fk =abs(K(i,j)/sqrt(K(i,i)*K(j,j)));
end
if(abs(M(i,j)/sqrt(M(i,i)*M(j,j))) > Fm)
Fm =abs(M(i,j)/sqrt(M(i,i)*M(j,j)));
end
end
end
if((Fk < eps & Fm < eps)|(Flambda < eps))
break;
end
end
disp('Stiffness Matrix and Mass Matrix');
K
M

disp('Eigenvalues');
lambda
disp('Eigenvectors');
X
%Eignevalues from the eig function
disp('Using MATLAB inbuilt function EIG');
[V,D] = eig(K,M);
diag(D)'
V
Sample output:
Problem 1]
K1 =
10
4
5

4
6
4

5
4
50

5
1
0

1
10
5

0
5
7

M1 =

Transformed Stiffness Matrix and Mass Matrix


K =
10.5386
-0.0000
0.0000

-0.0000
3.9828
-0.0000

0.0000
0
48.8408

-0.0000
9.1127
-0.0000

0.0000
-0.0000
4.4757

0.4371

10.9124

-0.4408
0.9428
-0.0485

-0.2654
-0.5023
1.0239

M =
5.3450
-0.0000
0.0000
Eigenvalues
lambda =
1.9717
Eigenvectors
X =
1.0214
0.1885
0.0266

Using MATLAB inbuilt function EIG


ans =
1.9717

10.9124

0.4371

-1.0000
0
0

0.0000
0.0000
-1.0000

-0.0000
-1.0000
0.0000

V =

Stiffness Matrix and Mass Matrix


K =
31.6902
-0.0000
-0.0521
2.8136

0.0000
7.9054
0.0687
-1.8915

-0.0521
0.0687
-1.1358
-0.0000

2.8136
-1.8915
0.0000
-6.4875

0
7.4685
0.0594
-1.7885

-0.0500
0.0594
-0.2841
0.0000

2.7958
-1.7885
-0.0000
-10.7569

1.0585

3.9977

0.6031

0.3660
0.8787
-0.2928
0.1067

-0.3022
0.3871
0.2936
-0.2406

-0.5584
-1.4177
0.4109
0.8685

M =
43.7151
0.0000
-0.0500
2.7958
Eigenvalues
lambda =
0.7249
Eigenvectors
X =
1.7189
0.0305
1.2568
-0.6850

Using MATLAB inbuilt function EIG


D =
3.9928

V =

0.7090

0.6469

1.0585

0.0010
-0.0074
1.0000
0.0013

1.0000
-0.2004
-0.0235
-0.8401

-0.2947
0.2388
0.0137
1.0000

0.0000
1.0000
0.0070
-0.0003

Problem 2]
K =
3
1
8
5

1
6
2
9

8
2
2
3

5
9
3
7

5
0
8
5

0
6
2
9

8
2
4
0

5
9
0
2

M =

Transformed Stiffness Matrix and Mass Matrix


K =
31.6902
-0.0000
-0.0521
2.8136

0.0000
7.9054
0.0687
-1.8915

-0.0521
0.0687
-1.1358
-0.0000

2.8136
-1.8915
0.0000
-6.4875

0
7.4685
0.0594
-1.7885

-0.0500
0.0594
-0.2841
0.0000

2.7958
-1.7885
-0.0000
-10.7569

1.0585

3.9977

0.6031

0.3660
0.8787
-0.2928
0.1067

-0.3022
0.3871
0.2936
-0.2406

-0.5584
-1.4177
0.4109
0.8685

M =
43.7151
0.0000
-0.0500
2.7958
Eigenvalues
lambda =
0.7249
Eigenvectors
X =
1.7189
0.0305
1.2568
-0.6850

Using MATLAB inbuilt function EIG


D =

3.9928

0.7090

0.6469

1.0585

0.0010
-0.0074
1.0000
0.0013

1.0000
-0.2004
-0.0235
-0.8401

-0.2947
0.2388
0.0137
1.0000

0.0000
1.0000
0.0070
-0.0003

V =

You might also like