Documente Academic
Documente Profesional
Documente Cultură
Solutions
5.8) The weak form of the given initial boundary value problem is
Z 3
Z 3
u
u
v u
v
10v dx + 0.1v(3, t) (3, t)
+ 0.1
dx =
t
x x
x
0
0
where u(0, t) = 100, u/x(3, t) = 100 exp(t), and u(x, 0) = 50. An m-file to solve this problem is
given below
function prob5_8(xvec, nstep, dt)
% 1D transient heat conduction problem:
% INPUTS:
% xvec = grid points in [0,3], xvec=[x(1)=0,x(2),...,x(n+1)=3]
% nstep = Number of time steps
% dt
= time increment
%
sdof=length(xvec); % number of nodes
n=sdof-1; % number of elements
nodes=[[1:n] [2:n+1]]; % configuration matrix
mm=zeros(sdof,sdof); kk=zeros(sdof,sdof); ff=zeros(sdof,1);
for iel=1:n
gn=nodes(iel,:);
h=xvec(gn(2))-xvec(gn(1));
ke=elstif(h);
me=elmass(h);
fe=elload(h);
for ii=1:2 % assemble mass and stiffness
gn1=gn(ii);
ff(gn1)=ff(gn1)+fe(ii);
for jj=1:2
gn2=gn(jj);
mm(gn1,gn2)=mm(gn1,gn2)+me(ii,jj);
kk(gn1,gn2)=kk(gn1,gn2)+ke(ii,jj);
end
end
end
uold=init_val(xvec); % set inital conditions
effrc=zeros(sdof,1); fnew=zeros(sdof,1);
t=0;
close all
hold on
plot(xvec, uold); % plot initial condition
for k=1:nstep % use implicit (backward difference) method
fnew=ff; fnew(sdof)=ff(sdof)+10*exp(-t-dt); % load vector at t+dt
effrc=mm*uold+dt*fnew; % effective forcing
aa=mm+dt*kk;
aa(1,:)=0; aa(1,1)=1; effrc(1)=100; % impose bc at x=0 and time t
fsol=aa\effrc;
uold=fsol;
if(rem(k,50)==0) plot(xvec,uold); end
t=t+dt;
end
esol=exac(xvec);
plot(xvec, uold,r, xvec, esol, o);
hold off
%============================================
% end of main program
%============================================
function me=elmass(h)
% element mass matrix
me=(h/6)*[2 1; 1 2];
%============================================
function ke=elstif(h)
% element stiffness matrix
ke=0.1*(1/h)*[1 -1; -1 1];
%============================================
function fe=elload(h)
% element load vector
fe=5*h*[1;1];
%============================================
function h=init_val(x)
h=50*ones(size(x));
%============================================
function u=exac(x)
% steady state solution
u=100+300*x-50*x.^2;
5.13) This and Problem 5.15 require the solution of the initial boundary value problem
u
u = 0,
t
0 < x, y < 1
with u given on the entire boundary of the unit square. Using forward time differencing leads to the
m-file (based on Ex5.11.1 of the texbook)
function prob5_13()
nx=4; ny=2;
nx1=nx+1; ny1=ny+1;
nel=nx*ny;
% number of elements
nnel=4;
% number of nodes per element
ndof=1;
% number of dofs per node
nnode=nx1*ny1;
% total number of nodes in system
sdof=nnode*ndof;
% total system dofs
deltt=0.01;
% time step size for transient analysis
stime=0.0;
% initial time
ftime=1;
% termination time
ntime=fix((ftime-stime)/deltt); % number of time increment
%--------------------------------------------% input data for nodal coordinate values
% gcoord(i,j) where i->node no. and j->x or y
initialization of
initialization of
solution vector
vector containing
initialization of
initialization of
% initialization
system vector
effective system vector
time history solution
system matrix
system matrix
of index vector
element
element
element
element
of 1st node
of 2nd node
of 3rd node
of 4th node
end
%----------------------------%
loop for time integration
%----------------------------for in=1:sdof
fsol(in)=0.0;
end
% initial condition
sol(1,1)=fsol(8);
sol(2,1)=fsol(9);
for it=1:ntime
fn=deltt*ff+(mm-deltt*kk)*fsol;
function [k]=felp2dr4(xleng,yleng)
%------------------------------------------------------------------% Purpose:
%
element matrix for two-dimensional Laplaces equation
%
using four-node bilinear rectangular element
%
% Synopsis:
%
[k]=felp2dr4(xleng,yleng)
%
% Variable Description:
%
k - element stiffness matrix (size of 4x4)
%
xleng - element size in the x-axis
%
yleng - element size in the y-axis
%------------------------------------------------------------------% element matrix
k(1,1)=(xleng*xleng+yleng*yleng)/(3*xleng*yleng);
k(1,2)=(xleng*xleng-2*yleng*yleng)/(6*xleng*yleng);
k(1,3)=-0.5*k(1,1);
k(1,4)=(yleng*yleng-2*xleng*xleng)/(6*xleng*yleng);
k(2,1)=k(1,2);
k(2,2)=k(1,1);
k(2,3)=k(1,4);
k(2,4)=k(1,3);
k(3,1)=k(1,3);
k(3,2)=k(2,3);
k(3,3)=k(1,1);
k(3,4)=k(1,2);
k(4,1)=k(1,4);
k(4,2)=k(2,4);
k(4,3)=k(3,4);
k(4,4)=k(1,1);
function [m]=felpt2r4(xleng,yleng)
%------------------------------------------------------------------% Purpose:
%
element matrix of transient term for two-dimensional Laplaces
%
equation using four-node bilinear rectangular element
%
% Synopsis:
%
[m]=felpt2r4(xleng,yleng)
%
% Variable Description:
%
m - element stiffness matrix (size of 4x4)
%
xleng - element size in the x-axis
%
yleng - element size in the y-axis
%------------------------------------------------------------------% element matrix
m=(xleng*yleng/36)*[4
2
1
2
2
4
2
1
1
2
4
2
2;
1;
2;
4];
function [kk]=feasmbl1(kk,k,index)
%---------------------------------------------------------% Purpose:
%
Assembly of element matrices into the system matrix
%
% Synopsis:
%
[kk]=feasmbl1(kk,k,index)
%
% Variable Description:
%
kk - system matrix
%
k - element matri
%
index - d.o.f. vector associated with an element
%-----------------------------------------------------------
edof = length(index);
for i=1:edof
ii=index(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
function [index]=feeldof(nd,nnel,ndof)
%---------------------------------------------------------% Purpose:
%
Compute system dofs associated with each element
%
% Synopsis:
%
[index]=feeldof(nd,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;
k=0;
for i=1:nnel
start = (nd(i)-1)*ndof;
for j=1:ndof
k=k+1;
index(k)=start+j;
end
end
5.15) Solving the same problem using Crank-Nicolson differencing leads to the m-file (based on
Ex5.11.5 of the texbook)
function prob5_15()
nx=4; ny=4;
nx1=nx+1; ny1=ny+1;
nel=nx*ny;
% number of elements
nnel=4;
% number of nodes per element
ndof=1;
% number of dofs per node
nnode=nx1*ny1;
% total number of nodes in system
sdof=nnode*ndof;
% total system dofs
deltt=0.01;
% time step size for transient analysis
stime=0.0;
% initial time
ftime=1;
% termination time
ntime=fix((ftime-stime)/deltt); % number of time increment
initialization of
initialization of
solution vector
vector containing
initialization of
initialization of
% initialization
system vector
effective system vector
time history solution
system matrix
system matrix
of index vector
nd(4)=nodes(iel,3);
x1=gcoord(nd(1),1);
x2=gcoord(nd(2),1);
x3=gcoord(nd(3),1);
x4=gcoord(nd(4),1);
xleng=x2-x1;
yleng=y4-y1;
element
of 1st node
of 2nd node
of 3rd node
of 4th node
end
%----------------------------%
loop for time integration
%----------------------------for in=1:sdof
fsol(in)=0.0;
end
% initial condition
sol(1,1)=fsol((nnode+1)/2);
kn=2*mm+deltt*kk;
for it=1:ntime
% store time history solution for middle node (assumes nnode is odd)
fn=deltt*ff+(2*mm-deltt*kk)*fsol;
sol(1,it+1)=fsol((nnode+1)/2); %
end
%------------------------------------
function [m]=felpt2r4(xleng,yleng)
%------------------------------------------------------------------% Purpose:
%
element matrix of transient term for two-dimensional Laplaces
%
equation using four-node bilinear rectangular element
%
% Synopsis:
%
[m]=felpt2r4(xleng,yleng)
%
% Variable Description:
%
m - element stiffness matrix (size of 4x4)
%
xleng - element size in the x-axis
%
yleng - element size in the y-axis
2
4
2
1
1
2
4
2
2;
1;
2;
4];
function [kk]=feasmbl1(kk,k,index)
%---------------------------------------------------------% Purpose:
%
Assembly of element matrices into the system matrix
%
% Synopsis:
%
[kk]=feasmbl1(kk,k,index)
%
% Variable Description:
%
kk - system matrix
%
k - element matri
%
index - d.o.f. vector associated with an element
%-----------------------------------------------------------
edof = length(index);
for i=1:edof
ii=index(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
function [index]=feeldof(nd,nnel,ndof)
%---------------------------------------------------------% Purpose:
%
Compute system dofs associated with each element
%
% Synopsis:
%
[index]=feeldof(nd,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;
k=0;
for i=1:nnel
start = (nd(i)-1)*ndof;
for j=1:ndof
k=k+1;
index(k)=start+j;
end
end