----------------------------
Here are some sympy codes for calculating the element matrices of a beam in finite element method.
-------------------------------


```python
from sympy import *

tension_bottom = Symbol('T_b')
K = Symbol('K')
z = Symbol('z')
L = Symbol('L')
E,I = symbols('E I')
tension = (tension_bottom + K*z)
linear_density = Symbol('m')
loading = Symbol('p')

shape_function = Matrix([
1 - 3 * (z/L)**2 + 2 * (z/L)**3,
z * (1 - z/L)**2,
3 * (z/L)**2 - 2 * (z/L)**3,
z**2 / L * (z/L - 1)
])

shape_function_derivation = diff(shape_function, z)

shape_function_derivation_derivation = diff(shape_function, z, z)

M_integral = linear_density * shape_function * shape_function.T

K_integral = shape_function_derivation_derivation * shape_function_derivation_derivation.T *E *I

Kg_integral = shape_function_derivation * shape_function_derivation.T * tension

loading_integral = shape_function * loading

M = integrate(M_integral, (z, 0, L))

K = integrate( K_integral, (z, 0, L))

Kg = integrate(Kg_integral, (z, 0, L))

Loading = integrate(loading_integral, (z, 0, L))
```


```python
M = expand(M)
K = expand(K)
Kg = expand(Kg)
Loading = expand(Loading)
```


```python
init_printing()
```


```python
M
```


```python
K
```


```python
Kg
```


```python
Loading
```


```python

```

363f5ad7df7df2d86cdd2ff358fefd633eff0fa6
ipynb
Jupyter Notebook
FEMatrix.ipynb
BowenFu/fem-theory
f6ec16c9209b456c03c2f06875bba2f6d826770b
Apache-2.0
FEMatrix.ipynb
BowenFu/fem-theory
f6ec16c9209b456c03c2f06875bba2f6d826770b
Apache-2.0
FEMatrix.ipynb
BowenFu/fem-theory
f6ec16c9209b456c03c2f06875bba2f6d826770b
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES