Download presentation
Presentation is loading. Please wait.
Published byAmelia Ramsey Modified over 9 years ago
1
1DFEM Implementation Example: % example_1D.m files clear n = 7; % no interior points h = 1/(n+1); a = inline('x+2','x'); c = inline('1','x'); f = inline('-x^2+5*x+3','x'); uexact = inline('x*(1-x)','x'); K = sparse(n,n); M1 = sparse(n,n); L = sparse(n,1); x = h:h:1; i=1;K(i,i)=(a((x(i)+x(i+1))/2)+a(x(i)/2))/h; i=n;K(i,i)=(a((x(i)+x(i+1))/2)+a((x(i)+x(i-1))/2))/h; for i=2:n-1 K(i,i)=(a((x(i)+x(i+1))/2)+a((x(i)+x(i-1))/2))/h; end for i=1:n-1 K(i,i+1) = -a( (x(i)+x(i+1))/2 )/h; K(i+1,i) = K(i,i+1); end v = ones(n, 1) ; M1 = diag(4*v,0)+diag(v(1:n-1),-1)+diag(v(1:n-1),1); M = (h/6)*M1; A = K + M; L(1) = h*(f((x(1))/2)+f((x(1)+x(2))/2))/2; L(n) = h*(f((x(n)+x(n-1))/2)+f((x(n)+1)/2))/2; for i=2:n-1 L(i) = f((x(i)+x(i-1))/2)+f((x(i)+x(i+1))/2); L(i) = h*L(i)/2; end U = A\L uexact(h) Usol = [0;U;0]; xnodes = [0;x']; ezplot('x*(1-x)',[0,1]); hold on plot(xnodes,Usol,'r-o') grid on hold off
2
Local Basis function If we restrict any global basis function on an interval Kj then it becomes either the zero function or one of these local basis functions
3
1DFEM Implementation Partition of the interval [0,1] Node Coordinate vector Global labeling (nodes) 1 2 3 4 5 6 7 8 9 Element labeling (interval) 1 2 3 4 5 6 7 8 Local labeling (nodes) 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 2468 1357 Global-local labeling matrix 1 st elem 2ed elem 3ed elem 8 th elem Global label of the first node Global label of the second node Example: Find the coordinate of the 2ed node in the fifth element
4
Three Matrices Partition of the interval [0,1] Nodes Coordinate vector Global local labeling Matrix Boundary nodes vector Mass Matrix stiffness Matrix
5
Assemble the Mass Matrix Mass Matrix n=9 # nodes M=8 # elements Contribution from K 1 Contribution from K 2 Contribution from K 3 Contribution from K 5 Contribution from K 6 Each entries of the matrix is a sum of several Contribution from elements. These contributions are integrals of two local basis functions in the same element
6
Element labeling (interval) 1 2 3 4 5 6 7 8 We generate all possible contributions in each element (this is called local mass matrix) How many integrations ?? 32 24 Assemble the Mass Matrix
7
Local Basis function
8
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 1 2 3 4747 3636 4 2121 3232 2 3 4 5757 4646 5
9
1DFEM Implementation % main.m file p=[0,0.2,0.4,0.45,0.5,0.55,0.6,0.8,1]; t=[1:8; 2:9]; e=[1,9]; [M] = createM(p,e,t) % createM.m file function [M] = createM(p,e,t) n = length(p); % number of nodes m = size(t,2); % number of elements M = sparse(n,n); for j=1:m loc2glb = t(1:2,j); % global labeling for the nodes in Kj xcoord = p(loc2glb); % coordinates of the nodes hj = xcoord(2)-xcoord(1); l_mass = (hj/6)*[2, 1;... 1, 2]; % local mass matrix M(loc2glb,loc2glb) = M(loc2glb,loc2glb) + l_mass; % Add contributions end
10
Stiffness Matrix Stiff Matrix
11
% main.m file p=[0,0.2,0.4,0.45,0.5,0.55,0.6,0.8,1]; t=[1:8; 2:9]; e=[1,9]; [K] = createK(p,e,t) Stiffness Matrix % createK.m file function [K] = createK(p,e,t) n = length(p); % number of nodes m = size(t,2); % number of elements a = inline('x+2','x'); K = sparse(n,n); for j=1:m loc2glb = t(1:2,j); % global labeling for the nodes in Kj xcoord = p(loc2glb); % coordinates of the nodes hj = xcoord(2)-xcoord(1); xb = (xcoord(1)+xcoord(2))/2; ab = a(xb); l_stif = (ab/hj)*[1, -1;... -1, 1]; % local stiffness matrix K(loc2glb,loc2glb) = K(loc2glb,loc2glb) + l_stif; % Add contributions end
12
Load vector Stiff Matrix Local Load vector
13
% createL.m file function [L] = createL(p,e,t) n = length(p); % number of nodes m = size(t,2); % number of elements f = inline('-x^2+5*x+3','x'); L = sparse(n,1); for j=1:m loc2glb = t(1:2,j); % global labeling for the nodes in Kj xcoord = p(loc2glb); % coordinates of the nodes hj = xcoord(2)-xcoord(1); xb = (xcoord(1)+xcoord(2))/2; fb = f(xb); l_L = (hj/2)*fb*[1; 1]; % local load vector L(loc2glb) = L(loc2glb) + l_L; % Add contributions end % main.m file p=[0,0.2,0.4,0.45,0.5,0.55,0.6,0.8,1]; t=[1:8; 2:9]; e=[1,9]; [L] = createL(p,e,t) Load vector
14
Weak Boundary Conditions Weak Example: Consider the partition 1 2 3 4
15
Boundary Conditions Mass Matrix stiffness Matrix impose boundary conditions 1 2 3 4 5 6 7 8 9 We need to remove contributions coming from these two red local functions for i=1:length(e) ib = e(i); A(ib,:)=sparse(1,n); A(:,ib)=sparse(n,1); A(ib,ib)=1; L(ib)=0; end
16
Boundary Conditions % non uniform mesh p=[0,0.2,0.4,0.45,0.5,0.55,0.6,0.8,1]; t=[1:8; 2:9]; e=[1,9]; % % uniform mesh % p=0:0.125:1; n = length(p); t=[1:n-1;2:n]; % e=[1,n]; n = length(p); % number of nodes m = size(t,2); % number of elements uexact = inline('x*(1-x)','x'); [M] = createM(p,e,t) [K] = createK(p,e,t) [L] = createL(p,e,t) A = K + M; for i=1:length(e) ib = e(i); A(ib,:)=sparse(1,n); A(:,ib)=sparse(n,1); A(ib,ib)=1; L(ib)=0; end U = A\L; ezplot('x*(1-x)',[0,1]); hold on; plot(p,U,'r-o'); grid on; hold off Impose boundary conditions
17
Uniform Mesh Uniform mesh (using 8 elements) Non-uniform mesh (using 8 elements)
18
Higher Order Polynomial a more general finite element space, consisting of piecewise polynomials of degree r − 1, where r is an integer ≥ 2, with the above piecewise linear case included for r = 2, thus Π k denotes the space of polynomials of degree ≤ k. Example: r = 2 S h space of cont pw-linear Example: (HW) r = 3 S h space of cont pw-quadratic 1 2 3 45
19
Example: r = 4 S h space of cont pw-cubic Local mass matrix Local stiffness matrix Higher Order Polynomial
20
1 2 3 1 2 3 45 6 7 8 9 1 2 1 2 10 34 3 4 4 5 4 5 67 6 7 7 8 7 8 9 9393 Remark: The matrix is not tridiagonal
21
Theorem Approximation properties of S h Theorem 5.1+5.2:
22
Reference element mapping
23
Example: r = 4 S h space of cont pw-cubic Local mass matrix Local stiffness matrix C element 1
24
Example: C element.vs. C element 1 1 C 0 C All global basis functions are continuous (only) All global basis functions and their first derivatives are continuous
25
Weak 4 th order differential equation Weak 1 C 0 C It is required to use element
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.