You are on page 1of 25

QUESTION 1

Length x Breath x Height = 25500


(L-2) (W-2) () = 25500
[1270- 340 - 150 + 4 2 ] = 25500
4 3 - 490 2 +12750 -25500 =0

(I)

Fixed point method code

function[p]=fixptD(g,p0,tol,max1)
%Input - g is the iteration fuction input as a string 'g'
%
- p0 is the initial guess for the fixed point
%
- tol is the tolerance
%
- max1 is the maximum number of iterations
%Outputs - k is the number of iterations that were carried out
%
- p is the approximation to the fixed point
%
- err is the error in the approximation
%
- P contains the sequence {pn}
P(1)=p0;
for k=2:max1
P(k)=feval(g,P(k-1));
err=abs(P(k)-P(k-1));
relerr=err/(abs(P(k))+eps);
p=P(k);
if (err<tol) | (relerr<tol),break; end
end
if k==max1
disp('maximum of iterations exceeded')
end
Sequence=[P]
fprintf('iterations= %d\n', k);
P=P';
At the command prompt the following command is entered and the output shown
>> fixptD(inline('(-4*x^3+490*x^2+25500)/12750'),3,0.0001,100)

Sequence =
3.0000
2.1793

2.3374

2.2060

2.1836

2.1800

2.1794

iterations= 7
ans =
2.1793

(II)

Bisection method code

% method of bisection
function[c,H,yc]=bisect(f,a,b,delta)
ya=feval(f,a);
yb=feval(f,b);
if ya*yb>0
disp('ya and yb of the same sign')
return
end
max1=1+round((log(b-a)-log(delta))/log(2));
for k=1:max1
c=(a+b)/2;
yc=feval(f,c);
if yc==0
a=c;
b=c;
elseif yb*yc>0
b=c;
yb=yc;
else
a=c;
ya=yc;
end
if b-a<delta, break,end
end
c=(a+b)/2;
err=abs(b-a);
yc=feval(f,c);
H=[c,err,yc]

At the command prompt the following command is entered and the output shown

>> bisect(inline('4*x^3-490*x^2+12750*x-25500'),0,3,0.000001)
H=

2.1793

0.0000 -0.0004

ans =
2.1793

(III)

Newton-Raphson method code

%Newton Raphson Method Code


function [p0,err,k,y]=newton(f,df,p0,delta,epsilon,max1)
for k=1:max1
p1=p0-feval(f,p0)/feval(df,p0);
err=abs(p1-p0);
relerr=2*err/(abs(p1)+delta);
p0=p1;
y=feval(f,p0);
if (err<delta)|(relerr<delta)|abs(y)<epsilon, break, end
end
H=[p0,err,k,y]
At the command prompt the following command is entered and the output shown
>> newton(inline('4*x^3-490*x^2+12750*x25500'),inline('12*x^2-980*x+12750'),3,0.000001,0.000001,100)
H =
2.1793
ans =

0.0000

3.0000

-0.0000

2.1793
(IV)

Secant method code

%secant method code


function[p1,err,k,y]=secant(f,p0,p1,delta,epsilon,max1)
for k=1:max1
p2=p1-feval(f,p1)*(p1-p0)/(feval(f,p1)-feval(f,p0));
err=abs(p2-p1);
relerr=2*err/(abs(p2)+delta);
p0=p1;
p1=p2;
y=feval(f,p1);
if (err<delta)|(relerr<delta)|(abs(y)<epsilon), break, end
end
H=[p1,err,k,y]
At the command prompt the following command is entered and the output shown

>> secant(inline('4*x^3-490*x^2+12750*x25500'),1.5,2,0.000001,0.000000000001,1000000)
H =
2.1793
ans =
2.1793
(V)

0.0000

4.0000

-0.0000

Regula Falsi method code

function H=regulaD(f,a,b,delta,epsilon,max1)
ya=feval(f,a);
yb=feval(f,b);
if ya*yb>0
disp('ya and yb of the same sign')
return
end
max1=1+round((log(b-a)-log(delta))/log(2));
for k=1:max1
dx=yb*(b-a)/(yb-ya);
c=b-dx;
ac=c-a;
yc=feval(f,c);
if yc==0
a=c;
b=c;
elseif yb*yc>0
b=c;
yb=yc;
else
a=c;
ya=yc;
end
dx=min(abs(dx),ac);
if abs(dx)<delta,break, end
if abs(yc)<epsilon,break,end
if b-a<delta,break,end
end
c=b-dx;
err=abs(b-a);
yc=feval(f,c);
H=[c,err,yc]
At the command prompt the following command is entered and the output shown
>> regulaD(inline('4*x^3-490*x^2+12750*x-25500'),0,3,0.000001,0.000000000001,1000)
H=

2.1793

2.1793 -0.0035

ans =
2.1793

2.1793 -0.0035

The exact solution is found using


>> Eqt= [4 -490 12750 -25500];
>> roots(Eqt)
ans =
86.5039
33.8168
2.1793
All numerical approximations are same with exact solution up to four (4) decimal places.

QUESTION 11
Linear static deflection of a one dimensional bar using finite element method.
For a long linear elastic bar shown below, assume the following:

1. The bar has shear modulus and Poissons ratio v.


2. The bar has cross section h h and length L.
3. It is constrained on all its sides so that 2 = 3 = 0.
4. The bar is subjected to body force b = b (1 )1 .
5. The bar is either loaded or constrained at its ends, so that the boundary conditions are
Either 1 (0) = t*(0), 1 (L) = t* (L) or displacement 1 (0) = u*
(0) 1 (L) = u* (L) at x = 0 and
x = L.
Each element stiffness matrix is computed as

The contribution from each element to the global stiffness matrix can be computed from

Similarly, a nonzero body force is compute for each element as

and assemble the global force vector as

Add contributions to the force vector from prescribed traction boundary conditions
at x = 0 and x = L

Where the (L) superscript denotes the node that lies at x = L.


Modify the stiffness matrix to enforce the constraints

Solve the system of linear equations

for the unknown displacements 1

The above procedure can be done with the following Matlab code and the results
plotted

%
%
%

Define values for parameters below


Length and x-sect area of bar
Length = 5.;
A = 1.;

Material props
mu = 50.;
nu = 0.3;

%
const = 2*mu*A*(1-nu)/(1-2*nu);
Loading
bodyforce = 10.;
traction = 2.;
%
total no. elements, no. nodes on each element (2 for
linear, 3 for quadratic elemnts), total no. nodes;
L = 10;
Ne = 3;
nnodes = (Ne-1)*L+1;
%
%
Set up some data structures storing the mesh
%
coords = zeros(1,nnodes);
for i= 1 : nnodes
coords(i) = Length*(i-1)/(nnodes-1);
end;
%
%
Element connectivity (speficies node numbers on each
element)
%
connect = zeros(Ne,L);
for lmn=1:L
if (Ne==3)
connect(1,lmn) = 2*lmn-1;
connect(3,lmn) = 2*lmn;
connect(2,lmn) = 2*lmn+1;
elseif (Ne == 2)
connect(1,lmn) = lmn;
connect(2,lmn) = lmn+1;
end ;
end;
%
%
Integration points and weights for 2 point integration
%
npoints = Ne-1;
if (npoints == 2)
w = [1,1];
xi = [-0.5773502692,0.5773502692];
elseif (npoints == 1)
w = [2.,0.];
xi = [0.,0.];
end;
%
%
%
Assemble the global stiffness and force vector
%
K = zeros(nnodes,nnodes);
%

F = zeros(nnodes,1);
%
for lmn = 1 : L
%
%
%

Extract the coords of each node on the current element


lmncoords = zeros(Ne);
for a = 1 : Ne
lmncoords(a) = coords(connect(a,lmn));
end

%
%
For the current element, loop over integration points
and assemble element stiffness
%
kel = zeros(Ne,Ne);
fel = zeros(Ne,1);
%
for II = 1 : npoints
%
%
Compute N and dN/dxi at the current integration point
%
N = zeros(1,Ne);
dNdxi = zeros(1,Ne);
if (Ne == 3)
N(1) = -0.5*xi(II)*(1.-xi(II));
N(2) = 0.5*xi(II)*(1.+xi(II));
N(3) = (1.-xi(II)^2);
dNdxi(1) = -0.5+xi(II);
dNdxi(2) = 0.5+xi(II);
dNdxi(3) = -2.*xi(II);
elseif (Ne == 2)
N(1) = 0.5*(1.-xi(II));
N(2) = 0.5*(1.+xi(II));
dNdxi(1) = -0.5;
dNdxi(2) = 0.5;
end;
%
%
Compute dx/dxi, J and dN/dx
%
dxdxi = 0.;
for a = 1 : Ne
dxdxi = dxdxi + dNdxi(a)*lmncoords(a);
end
J = abs(dxdxi);
dNdx = zeros(1,Ne);
for a = 1 : Ne
dNdx(a) = dNdxi(a)/dxdxi;
end
%
%
Add contribution to element stiffness and force
vector from current integration pt

%
for a = 1 : Ne
fel(a) = fel(a) + w(II)*bodyforce*J*N(a);
for b = 1 : Ne
kel(a,b) = kel(a,b) +
const*w(II)*J*dNdx(a)*dNdx(b);
end
end
%
end
%
%
Add the stiffness and residual from the current
element into global matrices
%
for a = 1 : Ne
rw = connect(a,lmn);
F(rw) = F(rw) + fel(a);
for b = 1 : Ne
cl = connect(b,lmn);
K(rw,cl) = K(rw,cl) + kel(a,b);
end
end
end
%
Add the extra forcing term from the traction at x=L
%
F(nnodes) = F(nnodes) + traction;
%
Modify FEM equations to enforce displacement boundary
condition
%
To do this we simply replace the equation for the first
node with u=0
%
for a = 1 : nnodes
K(1,a) = 0.;
end
K(1,1) = 1.;
F(1) = 0.;
%
%
Solve the equations
%
u = K\F;
%
plot(coords,u,'r - s')
xlabel('Distance x')
ylabel('Displacement u')
title('Displacement of 1D bar','FontSize',12)

QUESTION 2
Gaussian Elimination with back substitution
function X=uptrbk(A,B)
%input Ais an NXN nonsigular matrix
%B is an NX1 solution matrix
%output X is an Nx1 solution matrix
%Initialize X and the temporary storage matrix C
[N N]=size(A);
X=zeros(N,1);
C=zeros(1, N+1);
%form the augmented matrix
Aug=[A B];
for p=1:N-1
%partial pivoting for column p
[Y,j]=max(abs(Aug(p:N,p)));
%Interchange row p and j
C=Aug(p,:);
Aug(p,:)=Aug(j+p-1,:)
Aug(j+p-1,:)=C
if Aug(p,p)==0
'A was singular.No unique solution'
break
end
%Estimation process for column p
for k=p+1:N

m=Aug(k,p)/Aug(p,p);
Aug(k,p:N+1)=Aug(k,p:N+1)-m*Aug(p,p:N+1)
end
end
%Back substitution on [U Y]
X=backsub(Aug(1:N,1:N),Aug(1:N,N+1))
The back substitution program
function X=backsub(A,B)
%Input - A is an nxn upper triangular nonsingular matrix
%
- B is an nx1 matrix
%
- X is the solution to the linear sytem AX=B
%Find th dimension of B and initialize X
n=length(B);
X=zeros(n,1);
X(n)=B(n)/A(n,n);
for k=n-1:-1:1
X(k)=(B(k)-A(k,k+1:n)*X(k+1:n))/A(k,k)
end
At the command prompt the following command is entered and the output shown
>> A=[8 -2 -1 0 0;-2 9 -4 -1 0;-1 -3 7 -1 -2;0 -4 -2 12 -5;0 -3 -7 0 15 ]
>> B=[5;2;1;1;5]
>> uptrbk(A,B)
Aug =

8.0000e+000

-2.0000e+000

8.5000e+000

0 -8.5000e+000

ans =

1.0821e+000
1.1749e+000
1.3067e+000
1.1836e+000

-1.0000e+000

0 5.0000e+000

-4.2500e+000

-1.0000e+000

0 3.2500e+000

-3.5294e-001

1.5000e+001

6.1471e+000

0 1.1696e+001

-1.2059e+001

-3.6332e-001

0 -2.2204e-016

5.6146e+000

6.6146e+000

1.1781e+000

QUESTION 3
Algorithm for transforming a matrix to an upper triangular matrix.
Step 1: For k = 1 to n-1 do step 2 to step 5
Step 2: For i = k+1 to n do step 3 to step 5
Step 3: For j = k+1 to n do step 4
Step 4: aij = aij aik.akj / akk
Step 5: ci = ci aik.ck / akk

(b) The Matlab Function


function X=uptrbk(A,B)
%input Ais an NXN nonsigular matrix
%B is an NX1 solution matrix
%output X is an Nx1 solution matrix
%Initialize X and the temporary storage matrix C
[N N]=size(A);
X=zeros(N,1);
C=zeros(1, N+1);
%form the augmented matrix
Aug=[A B];
for p=1:N-1
%partial pivoting for column p
[Y,j]=max(abs(Aug(p:N,p)));
%Interchange row p and j
C=Aug(p,:);
Aug(p,:)=Aug(j+p-1,:)
Aug(j+p-1,:)=C
if Aug(p,p)==0
'A was singular.No unique solution'
break
end
%Estimation process for column p
for k=p+1:N
m=Aug(k,p)/Aug(p,p);
Aug(k,p:N+1)=Aug(k,p:N+1)-m*Aug(p,p:N+1)
end
end

(c) At the command prompt the following command is entered and the output shown
>> A=[2 4 -6;1 5 3;1 3 2]
>> B=[-4;10;5]
>> uptrbk(A,B)
Aug =

4 -6

-4

12

(d)

The Matlab function is the same as that of program 3.2(page 136 Mathew and Finks)

(e) Algorithm for back substitution taking advantage of (a)


Step 6: xn = cn / ann
Step 7: For i = n-1 to 1 do step 8 to step 11
Step 8: s = 0
Step 9: For j = i+1 to n do step 10
Step 10: s = s + aijxj
Step 11: xi = (ci-s) / aii
Step 12: End.
The Matlab Function
function X=backsub(A,B)
%Input - A is an nxn upper triangular nonsingular matrix
%
- B is an nx1 matrix
%
- X is the solution to the linear sytem AX=B
%Find th dimension of B and initialize X
n=length(B);
X=zeros(n,1);
X(n)=B(n)/A(n,n);
for k=n-1:-1:1
X(k)=(B(k)-A(k,k+1:n)*X(k+1:n))/A(k,k)
end
(f) At the command prompt the following command is entered and the output shown
>> A=[2 4 -6;1 5 3;1 3 2]
>> B=[-4;10;5]

>> uptrbk(A,B)
ans =

-3
2
1

In comparison with program 3.2, both Matlab functions are same.

QUESTION 4
i.

Given the function


() =

Using the Newton Raphson iteration method


= 1

(1 )
(1 )

() = 1
Thus
= 1
= 1

(1
)
1
(1 )

1
(1 /1
)

1
( 1)1 + (/1
)
=

ii.

1
( 1)1 + (/1
)

To find 5,
= 5
5=0
2

Thus
() = 2 5 = 0

Where N=2 and A=5


Using the recursive formula in (i) with an initial approximation p0 2 , with a
tolerance of err=0.0001
(2 1)0 + (5/021 )
1 =
2
1 =

0 + (5/0 )
2

With p0 2
1 =
2 =
3 =

2 + 5/2
= 2.25000
2

2.25 + (5/2.25)
= 2.23611
2

2.23611 + (5/2.23611)
= 2.23607
2
|| = | 1 |
|| = |2.23607 2.23611|
|| = 0.00004 <

Thus the solution of = 5 converges to x=2.23607

iii.

Using Matlab for the Nth root recursive formula

function N= nroot(A,n,p0,delta,max1)
for k=1
p1=p0;
end
for k=2:max1
p1=((n-1)*p0+A/(p0^(n-1)))/n;
err=abs(p1-p0);
relerr=2*err/(abs(p1)+delta);
p0=p1;
if(err<delta)|(relerr<delta)
break
end
end
if k==max1
disp('maximum number of iterations exceeded: results
unreliable')
disp('consider increasin the maximum no of iterations')
end
disp(' [ p0
k
err]')

[p0 k err]
iv.

Verifying the answer in (ii) above using the Matlab program


>> nroot(5,2,2,1e-4,100)
[ p0
k err]
ans =
2.2361 4.0000 0.0000

QUESTION 6
(i)

Matlab program

function [Y]=LOOP(A,B)
[N,N]=size(A)
for I=1:N
L(I,1)=A(I,1);
end
for J=1:N
U(1,J)=A(1,J)/L(1,1);
end
SUM1=0.00;
SUM2=0.00;
for J=2:N
for I=J:N
SUM1=0.00;
for K=1:J-1
SUM1=SUM1+[L(I,K)*U(K,J)];
end
L(I,J)=[A(I,J)-SUM1];
end
U(J,J)=1;
for I=J+1:N
SUM2=0.00;
for K=1:J-1
SUM2=SUM2+[L(J,K)*U(K,I)];
end
U(J,I)=[A(J,I)-SUM2]/L(J,J);
end
end
L(I,J)=A(I,J)-SUM1
U(J,I)=[A(J,I)-SUM2]/L(J,J)

(ii)

LU triangular command

>> LOOP ([4 -1 0 0 0 ;-1 4 -1 0 0;0 -1 4 -1 0;0 0 -1 4 -1;0 0 0 -1 4],[100 200 200 200 100])
L=

4.0000

-1.0000

3.7500

-1.0000

3.7333

-1.0000

3.7321

-1.0000

3.7321

1.0000

-0.2500

1.0000

-0.2667

1.0000

-0.2679

1.0000

-0.2679

1.0000

U=

b. (i)

The Matlab script for the L U decomposition is;

function [X]=modlu(A,B)
[N,N]=size(A)
for I=1:N
L(I,1)=A(I,1);
end
for J=1:N
U(1,J)=A(1,J)/L(1,1);
end
SUM1=0.00;
SUM2=0.00;
for J=2:N
for I=J:N
SUM1=0.00;
for K=1:J-1
SUM1=SUM1+[L(I,K)*U(K,J)];
end
L(I,J)=[A(I,J)-SUM1];
end
U(J,J)=1;
for I=J+1:N
SUM2=0.00;
for K=1:J-1
SUM2=SUM2+[L(J,K)*U(K,I)];

end
U(J,I)=[A(J,I)-SUM2]/L(J,J);
end
end
L(I,J)=A(I,J)-SUM1
U(J,I)=[A(J,I)-SUM2]/L(J,J)
X=zeros(N,1);
C(1)=B(1)/L(1,1);
for I=2:N
SUM3=0.00;
for K=1:I-1
SUM3=SUM3+L(I,K)*C(K);
end
C(I)=B(I)-SUM3;
end
X(N)=C(N)
for J=N-1:-1:1
SUM4=0.00;
for K=J+1:N
SUM4= SUM4+U(J,K)*X(K);
end
X(J)= (C(J)-SUM4)/L(J,J)
end

At the command prompt the following commands are entered


>> modlu([4 -1 0 0 0 ;-1 4 -1 0 0;0 -1 4 -1 0;0 0 -1 4 -1;0 0
0 -1 4],[100 200 200 200 100])
ans =
46.1538
84.6154
92.3077
84.6154
46.1538

b.(ii)
>> modlu([4 -1 0 0 0 ;-1 4 -1 0 0;0 -1 4 -1 0;0 0 -1 4 -1;0
0 0 -1 4],[500 700 100 400 300])
ans=
188.5897
254.3590
128.8462

161.0256
115.2564
The following are entered at the command prompt
>> modlu([4 -1 0 0 0 ;-1 4 -1 0 0;0 -1 4 -1 0;0 0 -1 4 -1;0 0
0 -1 4],[80 120 150 100 90])
ans =
33.5641
54.2564
63.4615
49.5897
34.8974

QUESTION 7
LU decomposition code
function X=TFM(A,B)
[L,U,P]=lu(A)
%forward substitution code
%Y=frdsub(A,B)
%LY=B
Y=inv(L)*B
%Back substitution
%UX=Y
X=inv(U)
Commands
>> A= [4 -1 0 0 0; -1 4 -1 0 0; 0 -1 4 -1 0; 0 0 -1 4 -1; 0 0 0 -1 4]

A=

4 -1

4 -1

-1

0 -1

-1

0 -1

-1

0 -1

>> B=[ 100; 200; 200; 200 ; 100]

B=

100
200
200
200
100

>> TFM(A,B)
L=
1.0000
-0.2500
0
0
0

0
1.0000
-0.2667
0
0

0
0
1.0000
-0.2679
0

0
0
0
1.0000
-0.2679

0
0
0
0
1.0000

U=
4.0000 -1.0000
0 3.7500
0
0
0
0
0
0

0
-1.0000
3.7333
0
0

0
0
-1.0000
3.7321
0

0
0
0
-1.0000
3.7321

P=
1
0
0
0
0

0
1
0
0
0

Y=
100.0000

0
0
1
0
0

0
0
0
1
0

0
0
0
0
1

225.0000
260.0000
269.6429
172.2488

X=
0.2500 0.0667 0.0179 0.0048
0 0.2667 0.0714 0.0191
0
0 0.2679 0.0718
0
0
0 0.2679
0
0
0
0
ans =
46.1538
84.6154
92.3077
84.6154
46.1538

Question 10
>> phi=30*pi/180

phi =

5.2360e-001

>> theta=20*pi/180

theta =

3.4907e-001

0.0013
0.0051
0.0192
0.0718
0.2679

>> zeta=10*pi/180

zeta =

1.7453e-001

>> A=[1,0,0; 0,cos(phi),sin(phi); 0,-sin(phi),cos(phi)]

A=

1.0000e+000

8.6603e-001

-5.0000e-001

5.0000e-001
8.6603e-001

>> B = [cos(theta),sin(theta),0;-sin(theta),cos(theta),0; 0,0,1]

B=

9.3969e-001

3.4202e-001

-3.4202e-001

9.3969e-001

0 1.0000e+000

>> C = [ 1,0,0; 0,cos(zeta),sin(zeta);0,-sin(zeta),cos(zeta)]

C=

1.0000e+000

9.8481e-001

1.7365e-001

-1.7365e-001

9.8481e-001

>> X=[1;2;3]

X=

1
2
3

>> A1=A*B*C

A1 =

9.3969e-001

3.3682e-001

5.9391e-002

-2.9620e-001

7.1461e-001

6.3372e-001

1.7101e-001

-6.1309e-001

7.7128e-001

>> inv(A1)
ans =
9.3969e-001

-2.9620e-001

1.7101e-001

3.3682e-001

7.1461e-001

-6.1309e-001

5.9391e-002

6.3372e-001

7.7128e-001

>> X2=inv(A1)*X
X2 =
8.6033e-001
-7.3232e-002
3.6407e+000

Question 5
(ii) LU decomposition code
function X=TFM(A,B)
[L,U,P]=lu(A)
%forward substitution code
%Y=frdsub(A,B)
%LY=B
Y=inv(L)*B
%Back substitution
%UX=Y
X=inv(U)

(iii)
>> A= [1 3 1 5;2 1 0 3;4 2 2 1;-3 1 3 2]
A =
1

-3

>> B=[4;5;11;3]
B =
4
5
11
3

>> TFM(A,B)
L =
1.0000

0.2500

1.0000

-0.7500

1.0000

1.0000

0.5000

-0.2500

1.0000

(b) Lower triangular method code


function X=frdsub(A,B)
% input -A is an n x n lower-triangular nonsingular matrix
%
-B is an n x 1 matrix
%output -X is the solution to the linear system AX = B
%Find the dimension of B and initialize X
n=length(B);
X=zeros(n,1);
X(1)=B(1)/A(1,1);
for k=2:n
X(k)=(B(k)-A(k,1:k-1)*X(1:k-1))/A(k,k);
end
command
>> frdsub([2 ,0,0,0;-1,4,0,0;3,2,-1,0;1,-2,6,3],[6;5;4;2])

ans =

3
2
9
-17

You might also like