0% found this document useful (0 votes)
70 views4 pages

Garlerkin FiniteElementmethod PDF

1) The document describes using Galerkin's method over n elements to numerically solve a second-order boundary value problem (BVP) with constant coefficients. 2) Integration by parts is used to reduce the order of differentiation, changing the problem into a matrix equation relating the nodal values within each element. 3) The matrix equation can be written in the form [K]{u}={f}, where [K] is the global stiffness matrix, {u} are the nodal values, and {f} contains the boundary conditions and integrals over each element.

Uploaded by

Huu Van Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views4 pages

Garlerkin FiniteElementmethod PDF

1) The document describes using Galerkin's method over n elements to numerically solve a second-order boundary value problem (BVP) with constant coefficients. 2) Integration by parts is used to reduce the order of differentiation, changing the problem into a matrix equation relating the nodal values within each element. 3) The matrix equation can be written in the form [K]{u}={f}, where [K] is the global stiffness matrix, {u} are the nodal values, and {f} contains the boundary conditions and integrals over each element.

Uploaded by

Huu Van Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

A lecture note on: Using integration by part to reduce the order of differentiation of ̃:

MATLAB code for solving 2nd-order homogeneous, constant ̃ ̃


coefficients BVPs via Galerkin’s Method over “ ” elements ( ( ( ))| ∫ )

Redmond Ramin Shamshiri ̃


October.26, 2010 ∫ ( ) ∫ ( ) ̃
Department of Agricultural and Biological Engineering
University of Florida
[email protected]
̃ ̃
∫ ∫ ( ) ∫ ( ) ̃

̃ ̃
function BVP_Galerkin2(a,b,c,t1,t2,x1,x2,ne) ( ) ( ) ( ) ( )

Download m-file
̃
http://www.mathworks.com/matlabcentral/fileexchange/40153-galerkins- Changing the variable to by substituting for and and
method-over--ne--elements-for-solving-2nd-order-homogeneous--c-c-bvp ( )
changing the integration domain by using the relation: ,
we obtain:
Galerkin’s method was used over elements in the entire domain
, - to numerically solve the following boundary value problem
(BVP). ( )
∫ [ ]2 3

∫ ( )[ ]2 3
(Eq. 1)
( ) , ( ) ( )
∫ ( ), ( ) ( )- 2 3
A general element, say element which has two nodes, say, ̃ ̃
( ) ( ) ( ) ( )
and such that is first considered. In order to apply Galerkin
method to one element at a time, a local coordinate such that
at node and at Node is introduced. The relation between
and for element is then: ( ). The approximate This equation can be written in matrix form as:
solution within the element e is given by: ̃( ) ( ) ( ),
where and are the interpolation functions and can be
expressed as a function of the variable as follow: ∫ [ ]2 3
( )

( ) . / ( ) { }
( )
∫ { }[ ]2 3
( ) ( ) ( )
( ) ( )
∫ { } , ( ) ( )- 2 3
Using the chain rule of differentiation, the derivatives of are: ( )
( ) ( ) ( ) ( )
( )
̃ ̃
Note that the terms ( ) and ( ) were not converted because
the boundary conditions do not use the approximation scheme. The
( )
above equation can be written in a matrix form as:

It can be seen that the interpolation functions satisfy the following


relation: ( )
( )
[ ] { } { } (Eq. 2)
( ) ( )
( )
( ) where
( ) [ ( )
] 0
( )
1 0
( )
1 0
( )
1

̃( ) and are nodal solution at nodes i and j, respectively.


The derivative of ̃( ) is obtained as: ( )
( )
0 1 ( )

̃
( )
[ ]2 3 ( )
[ ]
0 1
Applying the Galerkin method at the element level: ( )

̃ ̃
∫ 4 ̃5 ( ) ( ) ( )
( )
0 1 ∫ [ ]
( ) ( )
̃ ̃ [ ]
∫ ( ) ∫ ( ) ∫ ̃ ( )

Redmond Ramin Shamshiri December 2010 University of Florida


( ( )) ( ) ( ) 40
( ) ( )
0 1 ∫ [ ]
( ) ( ) ( ( )) Approximate dx(t)
30 Exact dx(t)
( )
[ ]

d2x/dt2 m/s 2
20
This equation is derived for each element
where is the number of elements. The right hand side of these 10
equations contain terms that are derivatives at the nodes ( ) and
0
( ) which are not generally known, however the second equation
for the element ( ) can be added to the first equation of element -10
( ) to eliminate the derivative term. Continuing this process for 1 2 3 4 5 6 7 8 9 10
Time (sec)
successive elements and the equations for the elements
1.2
will reduce to number of equations which is equal to the
Error of approximation
number of nodes. The equations to be solved take the following 1
form where is the global stiffness matrix. 0.8

Error (%)
0.6
( ) ( ) ( ) ( )
0.4
( ) ( ) ( )
0.2

[ ] 0
{ }
( ) -0.2
1 2 3 4 5 6 7 8 9 10
Time (s)
Figure 1. Solution to the differential equation in example

%The purpose of this program is to implement Galerkin


{ ( )} method over "ne"
, - * + * ( ) ( )+ (Eq. % individual element for solving the following general
3) 2nd order,
% homogeneous, Boundary Value problem (BVP) with
constant coefficients, and
Example: % then comparing the answer with the exact solution.
%======================================================
====================
( ) , ( ) % ax"(t)+bx'(t)+cx(t)=0 for t1<=t<=t2
% BC: x(t1)=x1 and x(t2)=x2
% >> BVP_Galerkin(a,b,c,t1,t2,x1,x2,ne)
Solved using elements % where "ne" is the number of elements
% The output of this program is
>>BVP_Galerkin2(1,2,3,1,10,2,0,64) % 1- The approximated x(t) vs. exact x(t)
% 2- The approximated x'(t) vs. exact x'(t)
% 3- The approximated x"(t) vs. exact x"(t)
2
%
1 ======Example==========================================
==================
0 % Equation x"(t)+ 0.5x'(t)+ 10x(t)=0
% Boundary values x(1)=2, x(10)=0;
-1
x(t) (m)

% Solution: We have:
-2 % a=1; b=2; c=3;
% t1=1; t2=10;
-3 % x1=2; x2=0;
Approximate x(t) % Using ne=64 elements,
-4 % >>BVP_Galerkin2(1,2,3,1,10,2,0,64)
Exact x(t)
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
-5
1 2 3 4 5 6 7 8 9 10 +++++++++++++++++++
Time (sec) % Sep.26 to Dec26, 2010. Ramin
5
Shamshiri
% [email protected]
% Doctoral student at the University of
0 Florida
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++
dx/dt (m/s)

-5

%=============================Program
-10 Begin================================
function BVP_Galerkin2(a,b,c,t1,t2,x1,x2,ne) % Declare
-15 Approximate dx(t)
function
Exact dx(t) %% Begin Approximate solution via Galerkin method over
-20 each individual element
1 2 3 4 5 6 7 8 9 10
Time (sec)
% Define element length
le = (t2-t1)/ne;
% Define t matrix: t=t1:t2
t_GM=t1:le:t2;
t_GM=t_GM';

Redmond Ramin Shamshiri December 2010 University of Florida


ylabel('x(t) (m)','FontSize',12);
% Building matrices arrays
K1 = (a/le) * [1,-1;-1,1]; % K1 matrix % Plotting the approximate dx
corresponding to the x"(t) subplot(2,2,2)
K2 = b*[-1/2 1/2;-1/2 1/2]; % K2 matrix plot(t_GM,dx_GM, '--', 'Color','r', 'LineWidth',3);
corresponding to the x'(t) hold on
K3 = -c*le*[1/3 1/6;1/6 1/3]; % K3 matrix xlabel('Time (sec)','FontSize',12);
corresponding to the x(t) ylabel('dx/dt (m/s)','FontSize',12);
Ke = K1+K2+K3; % Element stiffness
Matrix % Plotting the approximate d2x
subplot(2,2,3)
%*****Begin Assembly Global stiffness matrix****** plot(t_GM,d2x_GM, '--', 'Color','r', 'LineWidth',3);
k = zeros(ne+1); hold on
for i=1:ne+1 xlabel('Time (sec)','FontSize',12);
for j=1:ne +1 ylabel('d^2x/dt^2 m/s^2','FontSize',12);
if (i==j)
if(i==1) assignin('base', 'x_GM', x_GM); % Sending x_GM to
k(i,j)=Ke(1,1); workspace
elseif(i==ne+1) assignin('base', 'dx_GM', dx_GM); % Sending dx_GM
k(i,j)=Ke(2,2); to workspace
else assignin('base', 'd2x_GM', d2x_GM); % Sending d2x_GM
k(i,j)=Ke(1,1)+Ke(2,2); to workspace
end assignin('base', 't_GM', t_GM); % Sending t_GM to
elseif(i==j+1) workspace
k(i,j)=Ke(1,2); clc
elseif(j==i+1)
k(i,j)=Ke(2,1); %% End of Approximate solution via Galerkin method over
else each individual element
k(i,j)=0;
end %% =====Begin Calculating Exact Solution==========
end dt=0.001; % increment
end r=roots([a b c]); % Roots of auxiliary equation
%*******End Assembly Global stiffness matrix***** r1=r(1); r2=r(2);

%The Global f Matrix % Bulding t-axis values


f = zeros(ne+1,1); t=t1:dt:t2; t=t';
s=size(t);
%BC apply x(t1) = x1
f(1,1) = x1; % Bulding x(t) axis values
%BC apply x(t2) = x2 x_exact=zeros(s(1),1);
f(ne+1,1) = x2; if r1==r2 % For critically damped case

% Display the Global stiffness matrix before striking C=inv([exp(r1*t1) t1*exp(r2*t1);exp(r1*t2)


row t2*exp(r2*t2)])*([x1;x2]);
K_Global=k; C1=C(1);
C2=C(2);
%Striking first and last rows
for i=2:ne+1 for i=1:s(1)
k(1,i) = 0;
k(ne+1,i) = 0; x_exact(i)=C1*exp(r1*t(i))+C2*t(i)*exp(r2*t(i));
end end
k(1,1) = 1;
k(ne+1,ne+1) = 1; else % For Under-damped or Over-damped case

C=inv([exp(r1*t1) exp(r2*t1);exp(r1*t2)
% Display the solvable stiffness matrix exp(r2*t2)])*([x1;x2]);
disp('The solvable stiffness matrix is') C1=C(1);
K_strike=k; C2=C(2);

%solving the result and finding the displacement for i=1:s(1)


matrix, {x} x_exact(i)=C1*exp(r1*t(i))+C2*exp(r2*t(i));
x_GM=inv(k)*f; end
end
% calculating x'(t)
dx_GM=zeros(ne+1,1); % calculating x'(t)
for i=1:ne dx_exact=zeros(s(1),1);
dx_GM(i,1)=(x_GM(i+1)-x_GM(i))/le; for i=2:s(1)
end dx_exact(i,1)=(x_exact(i)-x_exact(i-1))/dt;
end
% calculating x"(t)
d2x_GM=zeros(ne+1,1); % calculating x"(t)
for i=2:ne d2x_exact=zeros(s(1),1);
d2x_GM(i,1)=(dx_GM(i+1)-dx_GM(i))/le; for i=3:s(1)
end d2x_exact(i,1)=(dx_exact(i)-dx_exact(i-1))/dt;
end
% Plotting the Approximate solution x(t) in red
(Galerkin method over each individual element) % Plotting the exact solution x(t) in blue
figure1 = figure('Color',[1 1 subplot(2,2,1)
1],'units','normalized','outerposition',[0 0 1 1]); plot(t,x_exact, 'Color','b', 'LineWidth',2); hold on
subplot(2,2,1) legend1 = legend(subplot(2,2,1),'show');
plot(t_GM,x_GM, '--', 'Color','r', 'LineWidth',3); legend('Approximate x(t)','Exact x(t)')
hold on
xlabel('Time (sec)','FontSize',12); % Plotting the exact solution x'(t) in blue
subplot(2,2,2)
Redmond Ramin Shamshiri December 2010 University of Florida
plot(t,dx_exact, 'Color','b', 'LineWidth',2); hold on
legend1 = legend(subplot(2,2,2),'show');
legend('Approximate dx(t)','Exact dx(t)')

% Plotting the exact solution x"(t) in blue


subplot(2,2,3)
plot(t,d2x_exact, 'Color','b', 'LineWidth',2); hold on
legend1 = legend(subplot(2,2,3),'show');
legend('Approximate dx(t)','Exact dx(t)')

assignin('base', 'x_exact', x_exact); %


Sending x_exact to workspace
assignin('base', 'dx_exact', dx_exact); %
Sending dx_exact to workspace
assignin('base', 'd2x_exact', d2x_exact); %
Sending d2x_exact to workspace
assignin('base', 't', t); %
Sending t to workspace
clc
%% =========End of exact solution========

%% ==Producing errors between exact x(t) and


approximated x(t)====
error_GM=zeros (ne+1,1);
if r1==r2 % For critically damped case
C=inv([exp(r1*t1) t1*exp(r2*t1);exp(r1*t2)
t2*exp(r2*t2)])*([x1;x2]);
C1=C(1);
C2=C(2);

for j=1:ne+1

error_GM(j)=((C1*exp(r1*t_GM(j))+C2*t_GM(j)*exp(r2*t_GM
(j)))-
(x_GM(j)))/(C1*exp(r1*t_GM(j))+C2*t_GM(j)*exp(r2*t_GM(j
)));
end

else % For Under-damped or Over-damped case

C=inv([exp(r1*t1) exp(r2*t1);exp(r1*t2)
exp(r2*t2)])*([x1;x2]);
C1=C(1);
C2=C(2);

for j=1:ne+1

error_GM(j)=((C1*exp(r1*t_GM(j))+C2*exp(r2*t_GM(j)))-
(x_GM(j)))/(C1*exp(r1*t_GM(j))+C2*exp(r2*t_GM(j)));
end
end

% Plotting the error of Exact x(t) and approximated


x(t)
subplot(2,2,4)
plot(t_GM,error_GM, '-.', 'Color','r', 'LineWidth',2);
hold on
xlabel('Time (s)','FontSize',12);
ylabel('Error (%)','FontSize',12);
legend1 = legend(subplot(2,2,4),'show');
legend('Error of approximation')

assignin('base', 'error_GM', error_GM); % Sending


error to workspace
assignin('base', 't_GM', t_GM); % Sending
t_GM to workspace
assignin('base', 'ne', ne); % Sending
t_GM to workspace

%% ====End of Producing errors between exact x(t) and


approximated x(t)====
%=======Program ends=========

Redmond Ramin Shamshiri December 2010 University of Florida

You might also like