```python
import sympy as sym
# Mesh: |--------|-------|
#       0      pi/2      pi
#
# Basis functions:
#
#   phi_0   phi_1   phi_2
#     \      /\      /
#      \    /  \    /
#       \  /    \  /
#        \/      \/
#     |-------|-------|
#     0      pi/2     pi

x = sym.Symbol('x')
A = sym.zeros(3,3)
f = sym.sin

phi_0 = 1 - (2*x)/sym.pi
phi_1l = 2*x/sym.pi          # left part of phi_1
phi_1r = 2 - (2*x)/sym.pi    # right part of phi_1
phi_2 = x/(sym.pi/2) - 1
node_0 = 0
node_1 = sym.pi/2
node_2 = sym.pi

# Diagonal terms
A[0,0] = sym.integrate(phi_0**2,  (x, node_0, node_1))
A[1,1] = sym.integrate(phi_1l**2, (x, node_0, node_1)) + \
         sym.integrate(phi_1r**2, (x, node_1, node_2))
A[2,2] = sym.integrate(phi_2**2,  (x, node_1, node_2))

# Off-diagonal terms
A[0,1] = sym.integrate(phi_0*phi_1l, (x, node_0, node_1))
A[1,0] = A[0,1]

A[1,2] = sym.integrate(phi_1r*phi_2, (x, node_1, node_2))
A[2,1] = A[1,2]

print(('A:\n', A))  # Can compare with general matrix, h=pi/2

b = sym.zeros(3,1)

b[0] = sym.integrate(phi_0*f(x),  (x, node_0, node_1))
b[1] = sym.integrate(phi_1l*f(x), (x, node_0, node_1)) + \
       sym.integrate(phi_1r*f(x), (x, node_1, node_2))
b[2] = sym.integrate(phi_2*f(x),  (x, node_1, node_2))

print(('b:\n', b))

c = A.LUsolve(b)
print(('c:\n', c))

for i in range(len(c)):
    print(('c[%d]=%g' % (i, c[i].evalf())))
print(('u(pi/2)=%g' % c[1]))

# For reports
print((sym.latex(A)))
print((sym.latex(b)))
print((sym.latex(c)))

```

    ('A:\n', Matrix([
    [ pi/6, pi/12,     0],
    [pi/12,  pi/3, pi/12],
    [    0, pi/12,  pi/6]]))
    ('b:\n', Matrix([
    [1 - 2/pi],
    [    4/pi],
    [1 - 2/pi]]))
    ('c:\n', Matrix([
    [      6*(4/3 - 4/pi)/pi],
    [24*(-7/6 + 7/pi)/(7*pi)],
    [ 7*(8/7 - 24/(7*pi))/pi]]))
    c[0]=0.114771
    c[1]=1.15847
    c[2]=0.114771
    u(pi/2)=1.15847
    \left[\begin{matrix}\frac{\pi}{6} & \frac{\pi}{12} & 0\\\frac{\pi}{12} & \frac{\pi}{3} & \frac{\pi}{12}\\0 & \frac{\pi}{12} & \frac{\pi}{6}\end{matrix}\right]
    \left[\begin{matrix}1 - \frac{2}{\pi}\\\frac{4}{\pi}\\1 - \frac{2}{\pi}\end{matrix}\right]
    \left[\begin{matrix}\frac{6 \left(\frac{4}{3} - \frac{4}{\pi}\right)}{\pi}\\\frac{24 \left(- \frac{7}{6} + \frac{7}{\pi}\right)}{7 \pi}\\\frac{7 \left(\frac{8}{7} - \frac{24}{7 \pi}\right)}{\pi}\end{matrix}\right]



```python

```

fdb651c0d26e4aa2d3e83aa499d6c3bb62d839f1
ipynb
Jupyter Notebook
Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/FINITE_ELEMENTS/INTRO/EXERCICES/11_FE_SIN_P1.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/FINITE_ELEMENTS/INTRO/EXERCICES/11_FE_SIN_P1.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/FINITE_ELEMENTS/INTRO/EXERCICES/11_FE_SIN_P1.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
2022-02-09T15:41:33.000Z
2022-02-11T07:47:40.000Z
Qwen/Qwen-72B
1. YES
2. YES