Академический Документы
Профессиональный Документы
Культура Документы
The m-file provided below is an adoption of the Text file ex351.m (see the CD provided with the
text) to solve the ODE problem posed in Homework Set 4: Find the weak form for the differential
equation
u00 + u0 2u = 3, 0 < x < 1, u0 (1) + 2u(1) = 1, u V = {C 2 ([0, 1]) : u(0) = 0}.
Use the Galerkin method with piecewise linear finite elements and modify the program given in class
to solve this equation.
The weak form for this equation is: Find u V such that
Z 1
Z 1
0 0
0
3v dx v(1)(1 2u(1))
(v u + vu 2vu) dx =
0
for all v V. The adoption of ex351.m (which employs piecewise linear finite elements) to solve this
problem is:
function ex351b(n)
%---------------------------------------------------------------------------% EX3.5.1 -- Modified version of Text file
% to solve the ordinary differential equation given as
%
a u + b u + c u = 3, 0 < x < 1
%
u(0) = 0 and u(1)+2u(1)=1
% a=1, b=1, c=-2
% using n linear elements
%
% 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;
%
%
%
%
%
number of elements
number of nodes per element
number of dofs per node
total number of nodes in system
total system dofs
%----------------------------------------gcoord=linspace(0,1,nel+1);
%----------------------------------------------------% input data for nodal connectivity for each element
%----------------------------------------------------nodes=[[1:nel] [2:nel+1]];
%----------------------------------------% input data for coefficients of the ODE
%----------------------------------------acoef=1;
bcoef=1;
ccoef=-2;
end
%----------------------------%
apply boundary conditions
%----------------------------[kk,ff]=feaplyc2(kk,ff,bcdof,bcval); % only left hand bc imposed
% bc at x=1 results in forcing ff(nel+1)-(1-2u(nel+1))
ff(nel+1)=ff(nel+1)-1;
kk(nel+1,nel+1)=kk(nel+1,nel+1)-2;
%---------------------------% solve the matrix equation
%---------------------------fsol=kk\ff;
%--------------------% analytical solution
%--------------------c1=4*exp(-1)/3;
c2=(1.5-4*exp(-1)/3);
for i=1:nnode
x=gcoord(i);
esol(i)=c1*exp(x)+c2*exp(-2*x)-1.5;
end
%-----------------------------------% print both exact and fem solutions
%-----------------------------------fprintf(%10s %10s %10s %10s\n,x, fem soln,exac soln,error);
fprintf(%10.2f %10.6f %10.6f %10.6f\n,[gcoord; fsol; esol; abs(fsol-esol)]);
plot(gcoord,fsol,k, gcoord, esol, ko);
%--------------------------------------------------------------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:
%
%
%
%
%-----------------------------------------------------------
edof = nnel*ndof;
start = (iel-1)*(nnel-1)*ndof;
for i=1:edof
index(i)=start+i;
end
function [k]=feode2l(acoef,bcoef,ccoef,eleng)
%------------------------------------------------------------------% Purpose:
%
element matrix for (a u + b u + c u)
%
using linear element
%
% Synopsis:
%
[k]=feode2l(acoef,bcoef,ccoef,eleng)
%
% Variable Description:
%
k - element matrix (size of 2x2)
%
acoef - coefficient of the second order derivative term
%
bcoef - coefficient of the first order derivative term
%
ccoef - coefficient of the zero-th order derivative term
%
eleng - element length
%------------------------------------------------------------------% element matrix
a1=-(acoef/eleng); a2=bcoef/2;
k=[ a1-a2+2*a3
-a1+a2+a3;...
-a1-a2+a3
a1+a2+2*a3];
a3=ccoef*eleng/6;
%
% k=a1*[1,-1;-1,1]+a2*[-1, 1;-1 1]+ a3*[2,1;1,2]
%
function [f]=fef1l(xl,xr)
%------------------------------------------------------------------% Purpose:
%
element vector for f(x)=1
%
using linear element
%
% Synopsis:
%
[f]=fef1l(xl,xr)
%
% Variable Description:
%
f - element vector (size of 2x1)
%
xl - coordinate value of the left node
%
xr - coordinate value of the right node
%------------------------------------------------------------------% element vector
eleng=xr-xl;
f=[ eleng/2;
% element length
eleng/2];
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