0% found this document useful (0 votes)
3 views

Notes

The document discusses the solution of differential equations using hat functions and introduces the finite element method. It presents a specific example involving a differential equation with boundary conditions, demonstrating the use of piecewise functions to approximate solutions. The document also includes calculations and comparisons with exact solutions, emphasizing the importance of dividing the domain into subdomains for improved accuracy.

Uploaded by

shreya29personal
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)
3 views

Notes

The document discusses the solution of differential equations using hat functions and introduces the finite element method. It presents a specific example involving a differential equation with boundary conditions, demonstrating the use of piecewise functions to approximate solutions. The document also includes calculations and comparisons with exact solutions, emphasizing the importance of dividing the domain into subdomains for improved accuracy.

Uploaded by

shreya29personal
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/ 12

Solution of differential equations using hat functions and

introduction to the finite element method


Narasimhan Swaminathan
February 6, 2025

1 Introduction
In the variational methods to solve differential equations, we had to assume functions in some fashion which looked
like
j=N
un = φ0 + ∑ c jφ j (1)
j=1

we assumed forms for φ0 and φ j depending on the boundary conditions. By assuming polynomials, our integration was
easier. However, the only way we had to improve the accuracy, was by increasing the order of the polynomials. This
is somewhat cumbersome. We now resort to slightly different kind of functions and the solution using these functions
is illustrated using examples.

2 Example
Consider the differential equation
d2u
− − u + x2 = 0 0 < x < 1 (2)
dx2
subject to boundary conditions

(i) u(x = 0) = 0; u(x = 1) = 0 (3)


du
(ii) u(x = 0) = 0; |x=1 = 1 (4)
dx
The weak form is

Weak Form

Z 1  Z 1
dwi duN
− wi uN dx = − wi x2 dx + (wi F)x=0 + (wi F)x=1 (5)
0 dx dx 0
Z 1  Z 1
dwi duN
− wi uN dx = − wi x2 dx + wi (0)F(0) + wi (1)F(1) (6)
0 dx dx 0

Now, we note the following conditions on wi


1. Must be 0 at both the ends for the first boundary condition and at the left end for the second boundary condition.
2. Must be differentiable as many times as needed by the weak form (Eq 48), which is once.
3. Must be linearly independent.
The approximate solution must
1. Must be satisfy the essential boundary conditions

1
2. Must be differentiable as many times as needed by the weak form (Eq 48), which is once.
3. Must involve linearly independent functions to approximate the solution meaningfully.

3 Piecewise functions
We define the following three functions shown in the Figure below. The functions have the following expressions

ϕ1 ϕ2 ϕ3

1 1 1
ϕ1

ϕ2

ϕ3
0.5 0.5 0.5

0 0 0
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1
x x x

(
x

1 − 0.5 0 < x < 0.5
φ1 = (7)
0 0.5 < x < 1
(
x
0.5 0 < x < 0.5
φ2 = 1 (8)
0.5 (1 − x) 0.5 < x < 1
(
0 0 < x < 0.5
φ3 = 1 (9)
0.5 (x − 1) + 1 0.5 < x < 1

Let us consider the functions

w1 = a1 φ1 (10)
w2 = a2 φ2 (11)
w3 = a3 φ3 (12)

1. Note that by choosing a1 = 0 and a3 = 0 we make w1 , w2 and w3 as zero at x = 0 and at x = 1. Similarly,


by choosing only a1 = 0, we get w1 , w2 and w3 = 0 only at x = 0 (as required by the second boundary
condition)
2. For the approximation, we will consider the following form
i=3
uN = ∑ ui φi = u1 φ1 + u2 φ2 + u3 φ3 (13)
i=1

Please note that if uN has to satisfy the given boundary conditions (first), then u1 = u3 must be 0 because
of the nature of φ1 and φ2 , they take on the value of 1 at x = 0 and x = 1, respectively.
3. Note that even if the first boundary condition in the strong from was

u(x = 0) = m; u(x = 1) = n m ̸= 0 n ̸= 0 (14)

then we would still require c1 = c3 = 0, but we would need u1 = m and u3 = n

2
4 Solution of the problem for the first boundary condition
Let us now write the three equations we will get for the first boundary condition. We do this from the weak form
Eq. 48.
i=3
 
Z 1 d ∑ ui φi i=3 Z 1
d(c1 φ1 ) i=1
(c1 φ1 )x2 dx + c1 φ1 (0)F(0) + c1 φ1 (1)F(1)

 − c1 φ1 ( ∑ ui φi ) dx = − (15)
0
 dx dx i=1
 0

i=3
 
Z 1 d ∑ ui φi i=3 Z 1
d(c2 φ2 ) i=1
(c2 φ2 )x2 dx + c2 φ2 (0)F(0) + c2 φ2 (1)F(1)

 − c2 φ2 ( ∑ ui φi ) dx = − (16)
0
 dx dx i=1
 0

i=3
 
Z 1 d ∑ ui φi i=3 Z 1
d(c3 φ3 ) i=1
− c3 φ3 ( ∑ ui φi ) dx = − (c3 φ3 )x2 dx + c3 φ3 (0)F(0) + c3 φ3 (1)F(1)

  (17)
0
 dx dx i=1 0

given that c1 = c3 = 0, we have only one equation. But simultaneously note that c1 , c2 , c3 cancel on both sides of the
three equations, making it appear as if wi was chosen to be φi and not ci φi . Hence, the only equation left is
i=3
 
Z 1 d ∑ ui φi i=3 Z 1
d(φ2 ) i=1
− φ2 ( ∑ ui φi ) dx = − (φ2 )x2 dx


 dx
 (18)
0 dx i=1 0

which on account of that fact that uN must satisfy the boundary conditions, requires u1 = u3 = 0. Hence, Eq. 18
becomes,
Z 1  Z 1
dφ2 du2 φ2
− φ2 (u2 φ2 ) dx = − (φ2 )x2 dx (19)
0 dx dx 0
(20)

giving
Z 1    Z 1
dφ2 dφ2
− φ2 φ2 dx u2 = − (φ2 )x2 dx (21)
0 dx dx 0

because φ2 is different in different regions of the domain, we have to split the integral and write it as follows
Z 0.5   Z 1  
dφ2 dφ2 dφ2 dφ2
− φ2 φ2 dx + − φ2 φ2 dx u2 =
0 dx dx 0.5 dx dx
Z 0.5 Z 1
− (φ2 )x2 dx − (φ2 )x2 dx (22)
0 0.5

We can now solve for u2 . Please do it. That is find u2 and compare to the exact solution at x = 12 . Note that at
x = 21 both φ1 and φ3 are identically 0, by definition. You should get u2 = −0.0397727272727273. The exact solution
is −0.04075910901317625. The solution at other points can be obtained as

uN = 0φ1 + (−0.0397727272727273)φ2 + 0φ3 (23)


uN = −0.0397727272727273φ2 (24)
x
That is, in the region 0 < x < 0.5, we have −0.0397727272727273 0.5 , while in the region 0.5 < x < 1, we have
−0.0397727272727273
0.5 (1 − x). You can see the plot of the solution in the jupyter notebook

3
Calculations_hat_functions_one_hat

February 6, 2025

[2]: import sympy as smp


import numpy as np
import matplotlib.pyplot as plt

[4]: x = smp.symbols("x")
phi21 = smp.symbols("phi_2^1")
phi22 = smp.symbols("phi_2^2")
u2 = smp.symbols("u_2")

[13]: phi21 = x/0.5


phi22 = 1/0.5*(1-x)
dphi21dx = smp.diff(phi21,x)
dphi22dx = smp.diff(phi22,x)
L1 = smp.integrate(dphi21dx*dphi21dx - phi21*phi21,(x,0,0.5))
L2 = smp.integrate(dphi22dx*dphi22dx - phi22*phi22,(x,0.5,1))
R1 = -smp.integrate(x*x*phi21,(x,0,0.5))
R2 = -smp.integrate(x*x*phi22,(x,0.5,1))

[14]: Eq = smp.Eq(((L1+L2)*u2),(R1+R2))

[15]: Eq
[15]:
3.66666666666667u2 = −0.145833333333333

[16]: smp.solve((Eq),(u2))

[16]: [-0.0397727272727273]

[17]: # Exact solution at x=0.5 is


def u(x):
return (np.sin(x) + 2*np.sin(1-x))/np.sin(1) + x*x - 2

[18]: u(0.5)

[18]: np.float64(-0.04075910901317625)

[30]: fig,ax = plt.subplots(figsize=(12,3))


x0 = np.arange(0,1,0.001)
x1 = np.arange(0,0.5,0.001)

1
x2 = np.arange(0.5,1,0.001)
actual_sol = u(x0)
ax.plot(x0,actual_sol)
upart_1 = -0.0397727272727273*x1/0.5
upart_2 = -0.0397727272727273/0.5*(1-x2)
ax.plot(x1,upart_1)
ax.plot(x2,upart_2)
ax.set_xlim([0,1])
ax.set_ylim([-0.05,0])
ax.set_xlabel("x")
ax.set_ylabel("Solution")
ax.legend(["Actual Solution", "Approximate Solution"])

[30]: <matplotlib.legend.Legend at 0x10bdaf6b0>

[ ]:

2
.
At this point, it is important for you to divide the subdomain into three equal parts and write hat functions for
each domain and solve the problem. You should solve for 2 unknowns and the approximate solution is likely to
be better than if you had only 2 subdivisions. This understanding is crucial in appreciating the finite element
method.
Now, suppose, we did not remove equations Eq. 15, 16 and 17 and simply cancelled out the constants c1 ,c2 , c3 , we
get the following,
i=3
 
Z 1 d ∑ u i φi i=3 Z 1
 d(φ1 ) i=1  dx = − (φ1 )x2 dx + F(0)

 dx − φ1 ( ∑ ui φi ) (25)
0 dx i=1
 0

i=3
 
Z 1 d ∑ ui φi i=3 Z 1
d(φ2 ) i=1
(φ2 )x2 dx


 dx − φ2 ( ∑ ui φi ) dx = − (26)
0 dx i=1
 0

i=3
 
Z 1 d ∑ u i φi i=3 Z 1
d(φ3 ) i=1
− φ3 ( ∑ ui φi ) dx = − (φ3 )x2 dx + F(1)


 dx
 (27)
0 dx i=1 0

which can be rewritten as


3 Z 1    Z 1
d(φ1 ) dφi
∑ 0 dx dx − φ1 φi dx ui = − 0 (φ1 )x2 dx + F(0) (28)
i=1
3 Z 1    Z 1
d(φ2 ) dφi
∑ 0 dx dx − φ2 φi dx ui = − 0 (φ2 )x2 dx (29)
i=1
3 Z 1    Z 1
d(φ3 dφi
∑ 0 dx dx − φ3 φi dx ui = − 0 (φ3 )x2 dx + F(1) (30)
i=1

which can be represented in matrix form as


      
k11 k12 k13 u1   f1  F(0)
k21 k22 k23  u2 = f2 + 0 (31)
k31 k32 k33 u3 f3 F(1)
     

here,
Z 1 
d(φi ) dφ j
ki j = − φi φ j dx (32)
0 dx dx
Z 1
fi = − φ1 x2 dx (33)
0

at this point we can set u1 = u3 = 0 (apply the boundary conditions).


   0    
k11 k12 k13  u
1
>   f1  F(0)
k21 k22 k23  u2 = f2 + 0 (34)
k31 k32 k33 u
  0  f3  F(1)
 3
>

the above set of equation has three unknowns u2 , F(0), F(1). The only equation required for finding u2 is the second
one, which is (the first row, first column; last row, last column is not needed to find u2 )

k22 u2 = f2 (35)

6
which is the same as what we got before. However, by retaining the first and the last equations we also have a way to
find out the secondary variable at the points where u was specified, which is often necessary.

k12 u2 = f2 + F(0) (36)


k32 u2 = f3 + F(1) (37)

Because, our solution for the interior point did not depend on whether we removed those equations, and retain-
ing them gave us a way to find the secondary variables, we can retain them, without affecting our results. Also,
this way of solving the problem, allows us to conveniently apply the boundary conditions, after writing down
the matrix form of the equations. Consequently, we need not be bothered about the constants ci s from now on
and simply assume that wi are the φi s. One of the drawbacks of this approach is that, if there was a secondary
variable applied at the interior point x = 0.5 we would not be able to apply that, as φ2 is becoming 0 at both
ends. The finite element method, can overcome this issue and make several aspects of solving the problem far
more general. This is what we do next.

5 Local Functions
Let us now consider the approximation of the solution locally over each sub-domain. We have over each domain, we
see the following
1. Over the subdomain 0 < x < 0.5 we have

Local approximation functions


 x  x
ueN1 = u1 1 − + u2 (38)
0.5 0.5

2. Over the subdomain 0.5 < x < 1 we have


 
u2 1
ueN2 = (1 − x) + u3 (x − 1) + 1 (39)
0.5 0.5

We could define locally a coordinate system x = x̄ + 0.5 and then we will have for Eq. 39. Note that x̄ = 0 when
x = 0.5
 
u2 1
ueN2 = (1 − x̄ − 0.5) + u3 (x̄ + 0.5 − 1) + 1 (40)
0.5 0.5
 
u2 1
ueN2 = (0.5 − x̄) + u3 (x̄ − 0.5) + 1 (41)
0.5 0.5

giving

Local approximation function (using local coordinates)


   
x̄ x̄
ueN2 = u2 1 − + u3 (42)
0.5 0.5

Please note the similarity between Eq. 42 and Eq. 38.

6 Finite element formulation


Let us consider the same problem as in

d2u
− − u + x2 = 0 0 < x < 1 (43)
dx2

7
subject to boundary conditions

(i) u(x = 0) = 0; u(x = 1) = 0 (44)

Instead of developing the weak form over the entire domain, we shall write it over each subdomain in the following
manner. We have
Z 0.5  1 1  Z 0.5
dwi duN 1 1
− wi uN dx = − w1i x2 dx + w1i (0)F(0) + wi (0.5)F(0.5) (45)
0 dx dx 0
dw2i du2N
Z 1   Z 1
− w2i u2N dx = − w2i x2 dx + w2i (0.5)F̄(0.5) + w2i (1)F(1) (46)
0.5 dx dx 0.5

The superfixes 1 and 2 over uN and wi indicate the subdomain and NOT square. Over the second domain, we may
simply write the weak form in local coordinates as follows.
Z 0.5  2 2  Z 0.5
d w̄i d ūN 2 2
− w̄i ūN d x̄ = − w̄2i (x̄ + 0.5)2 d x̄ + w̄2i (0)F̄(0.5) + w̄2i (0.5)F̄(1) (47)
0 d x̄ d x̄ 0

where the overbars on wi and uN indicate that the approximations are written over that domain, in local coordinate
system. We need to understand what the meaning of F is at this point, after we have written the weak form over each
subdomain. F was the natural boundary condition at the ends of the domain. What does it mean at a point interior to
the domain and how do we actually deal with it. To understand this, we will write these terms in terms of the actual
derivatives of uN in this subdomain as follows.

Z 0.5  1 1
du1N
 0.5
   1 
dwi duN duN
Z
− w1i u1N dx = − w1i x2 dx + w1i (0) (−1) + w1i (0.5) (1) (48)
0 dx dx 0 dx x=0 dx x=0.5
Z 0.5  2 2  Z 0.5  2   2 
d w̄i d ūN du du
− w̄2i ū2N d x̄ = − w̄2i (x̄ + 0.5)2 d x̄ + w̄2i (0) N
(−1) + w̄2i (0.5) N
(1) (49)
0 d x̄ d x̄ 0 dx x=0.5 dx x=1

we can write the second equation completely in local coordinates and we have the two weak forms as follows,

Z 0.5  1 1  1
du1N
 Z 0.5   
dwi duN 1 1 1 2 1 duN
− wi uN dx = − wi x dx + wi (0) (−1) + w1i (0.5) (1) (50)
0 dx dx 0 dx x=0 dx x=0.5
Z 0.5  2 2  Z 0.5  2   2 
d w̄i d ūN d ū d ū
− w̄2i ū2N d x̄ = − w̄2i (x̄ + 0.5)2 d x̄ + w̄2i (0) N
(−1) + w̄2i (0.5) N
(1) (51)
0 d x̄ d x̄ 0 d x̄ x̄=0 d x̄ x̄=0.5

We now take for the subdomain 0 < x < 0.5


 x 
w11 = c1 1 − (52)
0.5
x
w12 = c2 (53)
0.5
 x  x
u1N = u1 1 − + u2 (54)
0.5 0.5
and over the subdomain 0.5 < x < 1 (or) 0 < x̄ < 1
 

w̄21 = c̄1 1 − (55)
0.5

w̄22 = c̄2 (56)
  0.5
2 x̄ x̄
ūN = u2 1 − + u3 (57)
0.5 0.5

8
Now by substituting for w1i and u1N from Eq.52, 53 and 54 in Eq50 and for w2i and u2N from Eq.55, 56 and 57 in Eq51
and understanding that the ci s and c̄i s get canceled on both sides we get, the following. Carefully note the following.

w11 (0) = 1 (58)


w11 (0.5) = 0 (59)
w12 (0) = 0 (60)
w12 (0.5) = 1 (61)
w̄21 (0) = 1 (62)
w̄21 (0.5) = 0 (63)
w̄22 (0) = 0 (64)
w̄22 (0.5) = 1 (65)
(66)

These give the following four equations. This can be generated using the Jupyter notebook

9
[53]: import sympy as smp

[54]: x = smp.symbols("x")
u1 = smp.symbols("u_1")
u2 = smp.symbols("u_2")
u3 = smp.symbols("u_3")
psi1 = smp.symbols("psi_1")
psi2 = smp.symbols("psi_2")

[55]: half = smp.Rational(1/2)


psi1 = (1 - x / half)
psi2 = (x/half)
dpsi1dx = smp.diff(psi1,x)
dpsi2dx = smp.diff(psi2,x)

[56]: un1 = u1*psi1+ u2*psi2


un2 = u2*psi1 + u3*psi2
dun1dx = smp.diff(un1,x)
dun2dx = smp.diff(un2,x)

[57]: # First equation obtained from using w_1^1 and un^1


Eq1LHS = smp.integrate((dpsi1dx*dun1dx - psi1*un1),(x,0,half))
Eq1RHS_first_term = smp.integrate((-psi1*x*x),(x,0,half))

[58]: # Second equation obtained from using w_1^2 and un^1


Eq2LHS = smp.integrate((dpsi2dx*dun1dx - psi2*un1),(x,0,half))
Eq2RHS_first_term = smp.integrate((-psi2*x*x),(x,0,half))

[59]: # Third equation obtained from using w_1^2 and un^2


Eq3LHS = smp.integrate((dpsi1dx*dun2dx - psi1*un2),(x,0,half))
Eq3RHS_first_term = smp.integrate((-psi1*(x+half)*(x+half)),(x,0,half))

[60]: # Fourth equation obtained from using w_2^2 and un^2


Eq4LHS = smp.integrate((dpsi2dx*dun2dx - psi2*un2),(x,0,half))
Eq4RHS_first_term = smp.integrate((-psi2*(x+half)*(x+half)),(x,0,half))

[61]: print(Eq1LHS,"=",Eq1RHS_first_term)
print(Eq2LHS,"=",Eq2RHS_first_term)
print(Eq3LHS,"=",Eq3RHS_first_term)
print(Eq4LHS,"=",Eq4RHS_first_term)

11*u_1/6 - 25*u_2/12 = -1/96


-25*u_1/12 + 11*u_2/6 = -1/32
11*u_2/6 - 25*u_3/12 = -11/96
-25*u_2/12 + 11*u_3/6 = -17/96

10
 1 
11 25 1 duN
u1 − u2 = − + (−1) (67)
6 12 96 dx x=0
 1 
25 11 1 duN
− u1 + u2 = − + (1) (68)
12 6 32 dx x=0.5
 2 
11 25 11 d ūN
u2 − u3 = − + (−1) (69)
6 12 96 d x̄ x̄=0
 2 
25 11 17 d ūN
− u2 + u3 = − + (1) (70)
12 6 96 d x̄ x̄=0.5

Rewriting the equations Eq. 72 and Eq 73 in global cordinates, we get


Q11 -SV at left end of first subdomain
z }|  {
11 25 1 du1N
u1 − u2 = − + (−1) (71)
6 12 96 dx x=0
Q12 -SV at right end of first subdomain
z }|  {
du1N

25 11 1
− u1 + u2 = − + (1) (72)
12 6 32 dx x=0.5
Q21 -SV at left end of second subdomain
z }|  {
11 25 11 du2N
u2 − u3 = − + (−1) (73)
6 12 96 dx x=0.5
Q22 -SV at right end of second subdomain
z }|  {
25 11 17 du2N
− u2 + u3 = − + (1) (74)
12 6 96 dx x=0.5

Since, SV at right end of first subdomain and SV at left end of second subdomain are should be the same, we add the
second and the third of the above set of equations, giving a matrix form of the equations as follows
11 25 1 
   
 6 − 0   
u1 
− 
Q11 

 12 
 
 


 96 


 

 

 25 22 25     14   1 2

− −  u 2 = − + Q 2 + Q 1 (75)
 12
 6 12 







 96 




 

Q22
 25 11  u3    17    
0 − + − 
 
12 6 96
SV between the subdomains

In general, Q12 + Q21 is not zero. It is the value of the secondary variable applied at that location. It is zero, only
if no SV is applied there. The discontinuity in the slope is essentially created by the applied SV. Think about
bar problem with a point load somewhere in between

Consequently we have
• For the first boundary condition we can have
  
Unknown
11 25  0  1  
  
− − 

 6 0  u  

 Q
71 

 12 

>1




 96 






  1 



 25 22 25 14
     
 0
− −  u 2 = − + Q 1

2 +  Q
:
2
1
(76)
 96 
 12 6 12  
      
   0   
 

 25 11  
 
u
   17
−  

 
 Unknown


0 − + 3
>    
2
 
12 6 96 Q
 7
 
2

11
• For the second boundary condition we can have
  
Unknown
11 25  0  1  
  
− − 

 6 0  u

 Q
71 

 12 

1
> 
 



 96 






  1 



 25 22 25  14
     
 0
− −  u2 = − + 
Q 1

2 +  Q
:
2
1
(77)
 12
 6 12  

 

 

 96 








 25 11  
  unknown
 17   1 

u
  
0 − + 3
> −  
  
 2


12 6 96 Q7
 
 2

Note the following important points

Important points

1. With the exact same formulation, we were able to solve the problem with both the boundary condition.
While the set of equations remained the same, the boundary conditions could be applied more easily
2. The coefficient matrix, were the same for both the problems
3. The coefficient matrix was symmetric and also sparse (had zeros). This will be more clear, if you had
multiple subdomains.
4.
5. By definition of the local coordinates it was sufficient to integrate for the coefficient matrix only once
for the first subdomain. The equations for the subsequent subdomain, only involved replacing u1 with
u2 and u2 with u3
6. We can apply Secondary variable at the intermediate points also. This was not possible with the formu-
lations involving Global Hat functions.

12

You might also like