You are on page 1of 5

ME 471 Quiz 1

March 13, 2003


Solution
1.a) Derive the weak form for the boundary value problem:
4x2 u00 + 16xu0 + 9u = 9x3 , 1 < x < 3,

2u0 (1) + 3u(1) = 4, u0 (3) + u(3) = 6

1.b) By using the general purpose ordinary differential equation solver developed in class (or otherwise), develop a Matlab function m-file based on the finite element method that will produce
an approximate solution to the boundary value problem of part 1.a. Your file should accept
as its single input parameter the number of elements to be used in computation of the solution. If you wish to compare your results with the exact solution to the problem, it is given by
u(x) = x3/2 (1 + 1.5 ln(x/3)) + x3 /9.
1.a) Multiply the given ODE by a test function v and integrate over the interval [1, 3]. Use integration
by parts to find
Z 3
Z 3

2
00
2
0 3
4x vu dx = 4x vu 1
(4x2 v 0 u0 + 8xvu0 ) dx.
1

Now use the boundary conditions to eliminate the u (1), u0 (3) with the result
Z

(4x2 v 0 u0 + 8xvu0 + 9vu) dx =

v9x3 dx 4x2 v(6 u)|x=3 + 4x2 v(2 1.5u)|x=1 ,

where this must hold for all test functions.


1.b)Using the suggested approach to the solution, the ODE solver can be modified to produce the
solution as shown below:
function prob1(n)
%---------------------------------------------------------------------------%
% Variable descriptions
%
k = element matrix
%
f = element vector
%
kk = system matrix
%
ff = system vector
%
index = a vector containing system dofs associated with each element
%
bcdof = a vector containing dofs associated with boundary conditions
%
bcval = a vector containing boundary condition values associated with
%
the dofs in bcdof
%---------------------------------------------------------------------------%-----------------------------------% input data for control parameters
%-----------------------------------nel=n;
nnel=2;
ndof=1;
nnode=nel+1;
sdof=nnode*ndof;
xleft=1;
xright=3;

% number of elements
% number of nodes per element
% number of dofs per node
% total number of nodes in system
% total system dofs
% left hand end point
% right hand end point

lhbc=2; rhbc=6;
% parameters in boundary conditions
%----------------------------------------% input data for nodal coordinate values
%----------------------------------------gcoord=linspace(xleft,xright,nnode);
%----------------------------------------------------% input data for nodal connectivity for each element
%----------------------------------------------------nodes=[1:nel;2:nel+1];
%----------------------------------------% initialization of matrices and vectors
%----------------------------------------ff=zeros(sdof,1);
% initialization of system force vector
kk=zeros(sdof,sdof);
% initialization of system matrix
index=zeros(nnel*ndof,1); % initialization of index vector
k=zeros(2,2); f=zeros(2,1);
%----------------------------------------------------------------% computation of element matrices and vectors and their assembly
%----------------------------------------------------------------for iel=1:nel
% loop for the total number of elements
nl=nodes(iel,1); nr=nodes(iel,2); % extract nodes for (iel)-th element
xl=gcoord(nl); xr=gcoord(nr);% extract nodal coord values for the element
eleng=xr-xl;
% element length
index=feeldof1(iel,nnel,ndof);% extract system dofs associated with element
k=elstif(xl,xr);
% compute element matrix
f=elload(xl,xr);
% compute element vector
[kk,ff]=feasmbl2(kk,ff,k,f,index); % assemble element matrices and vectors
end
%----------------------------%
apply boundary conditions
%----------------------------av=acoef(xright);
ff(nnode)=ff(nnode)-av(1)*rhbc;
kk(nnode,nnode)=kk(nnode,nnode)-av(1);
av=acoef(xleft);
ff(1)=ff(1)+av(1)*lhbc;
kk(1,1)=kk(1,1)+av(1)*1.5;
%---------------------------% solve the matrix equation
%---------------------------fsol=kk\ff;
%---------------------

% analytical solution
%--------------------for i=1:nnode
x=gcoord(i);
esol(i)=x^(-1.5)*(-1+1.5*log(x/3))+x^3/9;
end
%-----------------------------------% print both exact and fem solutions
%-----------------------------------err=abs(esol-fsol);
num=1:1:sdof;
fprintf(%10s %10s %10s %10s\n,node, fem, exact, abs err);
fprintf(%10d %10.4f %10.4f %10.4f\n, [num; fsol; esol; err] );
plot(gcoord,fsol,k, gcoord,esol,ko);
%--------------------------------------------------------------------function k=elstif(xl,xr)
% approximate coefficients by their values at the midpoint of
% the element
xm=(xl+xr)/2; h=xr-xl;
av=acoef(xm); bm=bcoef(xm); cm=ccoef(xm);
k=-(av(1)/h)*[1 -1;-1 1]+((bm-av(2))/2)*[-1 1;-1 1]+(cm*h/6)*[2 1;1 2];
%--------------------------------------------------------------------function f=elload(xl,xr)
xm=(xl+xr)/2; h=xr-xl; fm=fcoef(xm);
f=(fm*h/2)*[1;1];
%--------------------------------------------------------------------function va=acoef(x)
va=[4*x^2;8*x];
%--------------------------------------------------------------------function b=bcoef(x)
b=16*x;
%--------------------------------------------------------------------function c=ccoef(x)
c=9;
%--------------------------------------------------------------------function f=fcoef(x)
f=9*x^3;
%=====================================================================
% utility files from Textbook
%=====================================================================
function [index]=feeldof1(iel,nnel,ndof)
%---------------------------------------------------------% Purpose:
%
Compute system dofs associated with each element in one%
dimensional problem
%
% Synopsis:
%
[index]=feeldof1(iel,nnel,ndof)

%
%
%
%
%
%

Variable Description:
index - system dof vector associated with element "iel"
iel - element number whose system dofs are to be determined
nnel - number of nodes per element
ndof - number of dofs per node

%-----------------------------------------------------------

edof = nnel*ndof;
start = (iel-1)*(nnel-1)*ndof;
for i=1:edof
index(i)=start+i;
end
function [kk,ff]=feasmbl2(kk,ff,k,f,index)
%---------------------------------------------------------% Purpose:
%
Assembly of element matrices into the system matrix &
%
Assembly of element vectors into the system vector
%
% Synopsis:
%
[kk,ff]=feasmbl2(kk,ff,k,f,index)
%
% Variable Description:
%
kk - system matrix
%
ff - system vector
%
k - element matrix
%
f - element vector
%
index - d.o.f. vector associated with an element
%-----------------------------------------------------------

edof = length(index);
for i=1:edof
ii=index(i);
ff(ii)=ff(ii)+f(i);
for j=1:edof
jj=index(j);
kk(ii,jj)=kk(ii,jj)+k(i,j);
end
end

function [kk,ff]=feaplyc2(kk,ff,bcdof,bcval)
%---------------------------------------------------------% Purpose:
%
Apply constraints to matrix equation [kk]{x}={ff}
%
% Synopsis:
%
[kk,ff]=feaplybc(kk,ff,bcdof,bcval)
%

% Variable Description:
%
kk - system matrix before applying constraints
%
ff - system vector before applying constraints
%
bcdof - a vector containging constrained d.o.f
%
bcval - a vector containing contained value
%
%
For example, there are constraints at d.o.f=2 and 10
%
and their constrained values are 0.0 and 2.5,
%
respectively. Then, bcdof(1)=2 and bcdof(2)=10; and
%
bcval(1)=1.0 and bcval(2)=2.5.
%----------------------------------------------------------n=length(bcdof);
sdof=size(kk);
for i=1:n
c=bcdof(i);
for j=1:sdof
kk(c,j)=0;
end
kk(c,c)=1;
ff(c)=bcval(i);
end

You might also like