<a href="https://colab.research.google.com/github/iampramodyadav/FEA/blob/main/Shape_Function.ipynb" target="_parent"></a>



---



# Natural Coordinate: Shape Funtion and its derivative

Pramod Kumar Yadav (@iampramodyadav)




---



- This programme calculates the values of shape functions and their derivatives.
- plot the shape funtions




## Function for shape function
- [nptel reference](https://nptel.ac.in/content/storage2/courses/112104116/lecture-11/11_10.htm)
- [SymPy Documentation](https://docs.sympy.org/latest/tutorial/basic_operations.html)


```python
from sympy import *
```


```python
z=Symbol('z')
```


```python
def SHAPE(p,z):
  '''
  SHAPE(p,z)
  p: order (p) of approximation
  z:value of natural coordinate
  This function return shape funtions values at given x
  '''
  z=Symbol('z')
  n=[]

  for i in range(0, p+1):
    point=-1
    point=point+2*i/p
    n.append(point)

  shape=[1]*(p+1)
  for i in range(0,p+1):
    for j in range(0,p+1):
      if i!=j:
        shape[i]=shape[i]*((z-n[j])/(n[i]-n[j]))
  return shape
```


```python
print(SHAPE.__doc__)
```

    
      SHAPE(p,z)
      p: order (p) of approximation
      z:value of natural coordinate
      This function return shape funtions values at given x
      


## Function for derivative of shape function
- [SymPy Documentation](https://docs.sympy.org/latest/tutorial/calculus.html)
- [Reference](https://stackoverflow.com/questions/47998930/differentiation-of-a-list-of-functions-with-respect-to-a-list-of-variables)




```python
def dSHAPE(p,z):
  '''
  dSHAPE(p,z)
  p: order (p) of approximation
  z:value of natural coordinate
  This function return derivative of shape funtions values at given x
  '''  
  z=Symbol('z')
  f=SHAPE(p,z)
  z=symbols('z')
  return [simplify(diff(f1, z)) for f1 in f]
```


```python
print(dSHAPE.__doc__)
```

    
      dSHAPE(p,z)
      p: order (p) of approximation
      z:value of natural coordinate
      This function return derivative of shape funtions values at given x
      




---




```python
df=dSHAPE(3,z)
print('Derivative of shape function:\n',df)
```

    Derivative of shape function:
     [-1.6875*z**2 + 1.125*z + 0.0625000000000001, 5.0625*z**2 - 1.125*z - 1.6875, -5.0625*z**2 - 1.125*z + 1.6875, 1.6875*z**2 + 1.125*z - 0.0624999999999999]



```python
f=SHAPE(3,z)
print('Shape funtion:\n',f ,'\n')

print('Simplified shape funtion:\n',[simplify(f1) for f1 in f])
```

    Shape funtion:
     [(0.25 - 0.75*z)*(0.5 - 0.5*z)*(-1.5*z - 0.5), (0.5 - 1.5*z)*(0.75 - 0.75*z)*(1.5*z + 1.5), (1.5 - 1.5*z)*(0.75*z + 0.75)*(1.5*z + 0.5), (0.5*z + 0.5)*(0.75*z + 0.25)*(1.5*z - 0.5)] 
    
    Simplified shape funtion:
     [-0.5625*z**3 + 0.5625*z**2 + 0.0625*z - 0.0625, 1.125*(z - 1)*(z + 1)*(1.5*z - 0.5), -1.125*(z - 1)*(z + 1)*(1.5*z + 0.5), 0.5*(0.75*z + 0.25)*(z + 1)*(1.5*z - 0.5)]



```python
simplify(f[0]) #first term
```




$\displaystyle - 0.5625 z^{3} + 0.5625 z^{2} + 0.0625 z - 0.0625$




```python
df[0] #deruvative of first term
```




$\displaystyle - 1.6875 z^{2} + 1.125 z + 0.0625000000000001$





---



## Visualization: Plotting shape functions


```python
import seaborn as sns
```


```python
def plotSHAPE(p,z):
  '''
  plotSHAPE(p,z)
  p: order (p) of approximation
  z:value of natural coordinate
  This function return plot of shape funtions
  '''
  sns.set()
  sns.set_style("whitegrid", {'grid.linestyle': '--'})

  z=Symbol('z')
  f=SHAPE(p,z)
  p1=plot(0,(z,-1,1),show=False)
  for i in f:
    p2=plot(i,(z,-1,1),show=False)
    p1.append(p2[0])

  return p1.show()
```


```python
print(plotSHAPE.__doc__)
```

    
      plotSHAPE(p,z)
      p: order (p) of approximation
      z:value of natural coordinate
      This function return plot of shape funtions
      



```python
plotSHAPE(1,z)
```


```python
plotSHAPE(2,z)
```


```python
plotSHAPE(3,z)
```


```python
plotSHAPE(4,z)
```


```python
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = 14, 8

plotSHAPE(5,z)
```



---



c705e5c1fe658a1782dce67252f0e3d2ab11820b
ipynb
Jupyter Notebook
Shape_Function.ipynb
iampramodyadav/FEA
c47058171d0bc5ab23e85162ee62c13a66b6d918
MIT
2021-07-25T13:24:08.000Z
2021-07-25T13:24:08.000Z
Shape_Function.ipynb
iampramodyadav/FEA
c47058171d0bc5ab23e85162ee62c13a66b6d918
MIT
Shape_Function.ipynb
iampramodyadav/FEA
c47058171d0bc5ab23e85162ee62c13a66b6d918
MIT
Qwen/Qwen-72B
1. YES
2. YES