```python
from sympy.solvers import solve
from sympy import Symbol
from sympy import *
import matplotlib.pyplot as plt
import numpy as np
from scipy.sparse import *
from numpy.linalg import inv
from array import *
from scipy import linalg
from numpy import array

a = array( [2,3,4] )
x=Symbol('x')
# number of elements
nEle=3
# length of the domain
domainLen=1
xb=[]
xa=[]
for i in range(1,nEle+1):
    xb.append(i*(domainLen/3))
    xa.append((i-1)*(domainLen/3))
# problem data
a=1
c=-1
f=-x*x
k=[]
Ftemp=[]
# for ith element  # NOTE: [[element 1 k's],[element 2 k's], ...]
for i in range(nEle):
    psi_1 = (xb[i]-x)/(xb[i]-xa[i])
    psi_2 = (x-xa[i])/(xb[i]-xa[i])
    # print(psi_1)
    # print(psi_2)
    k.append([])
    Ftemp.append(integrate(f*psi_1 ,(x, xa[i], xb[i])))
    Ftemp.append(integrate(f*psi_2 ,(x, xa[i], xb[i])))
    k[i].append(integrate( a*diff(psi_1,x)*diff(psi_1,x)+c*psi_1*psi_2,(x, xa[i], xb[i])))
    k[i].append(integrate( a*diff(psi_1,x)*diff(psi_2,x)+c*psi_1*psi_2,(x, xa[i], xb[i])))
    k[i].append(integrate( a*diff(psi_2,x)*diff(psi_1,x)+c*psi_1*psi_2,(x, xa[i], xb[i])))
    k[i].append(integrate( a*diff(psi_2,x)*diff(psi_2,x)+c*psi_1*psi_2,(x, xa[i], xb[i])))

F=[]
F.append(Ftemp[0])
for i in range(0,len(Ftemp)-2,2):
    F.append(Ftemp[i+1]+Ftemp[i+2])
F.append(Ftemp[len(Ftemp)-1])

# print('k = ',k)
# print('Ftemp',Ftemp)
# print('F = ',F)
# three diagonals of the tridiagonal matrix
diagA=[]
diagB=[]
# for three element 4*4 k matrix
for i in range(nEle):
    diagA = np.append(diagA, k[i][2])
print('diagA',diagA)
# NOTE: no need for diagC as it will always be same as diagA

diagB = np.append(diagB, k[0][0])
for i in range(nEle-1):
    diagB = np.append(diagB, k[i][3]+k[i+1][0])
diagB = np.append(diagB, k[nEle-1][3])
print('diagB',diagB)

a=array([-3.05555555555556, -3.05555555555556, -3.05555555555555])
b=array([2.94444444444444, 5.88888888888889, 5.88888888888889, 2.94444444444444])

diagA=np.array(diagA, dtype=np.float64)
diagB=np.array(diagB, dtype=np.float64)
new_rr = np.array([diagB, diagA, diagA])

T = diags(new_rr, [0,-1, 1]).toarray()
print('\n',T, '\n', T[1][1],'\n')
d = np.array(T)
ainv = inv(d)
print(ainv)
print(linalg.inv(T).dot(F))
```

    diagA [-3.05555555555556 -3.05555555555556 -3.05555555555555]
    diagB [2.94444444444444 5.88888888888889 5.88888888888889 2.94444444444444]
    
     [[ 2.94444444 -3.05555556  0.          0.        ]
     [-3.05555556  5.88888889 -3.05555556  0.        ]
     [ 0.         -3.05555556  5.88888889 -3.05555556]
     [ 0.          0.         -3.05555556  2.94444444]] 
     5.888888888888888 
    
    [[-1.16239394 -1.4473978  -1.62713636 -1.68853773]
     [-1.4473978  -1.39476515 -1.56796777 -1.62713636]
     [-1.62713636 -1.56796777 -1.39476515 -1.4473978 ]
     [-1.68853773 -1.62713636 -1.4473978  -1.16239394]]
    [0.541326863541983 0.522652351332375 0.480108173167282 0.453152087458709]



```python
from sympy.solvers import solve
from sympy import Symbol
from sympy import *
import matplotlib.pyplot as plt
import numpy as np
from scipy.sparse import *
from numpy.linalg import inv
from array import *
from scipy import linalg
x=Symbol('x')

def kfmatix(nEle,domainLen,a,c,f):
    xb=[]
    xa=[]
    for i in range(1,nEle+1):
        xb.append(i*(domainLen/3))
        xa.append((i-1)*(domainLen/3))
    k=[]
    Ftemp=[]
    # for ith element  # NOTE: [[element 1 k's],[element 2 k's], ...]
    for i in range(nEle):
        psi_1 = (xb[i]-x)/(xb[i]-xa[i])
        psi_2 = (x-xa[i])/(xb[i]-xa[i])
        # print(psi_1)
        # print(psi_2)
        k.append([])
        Ftemp.append(integrate(f*psi_1 ,(x, xa[i], xb[i])))
        Ftemp.append(integrate(f*psi_2 ,(x, xa[i], xb[i])))
        k[i].append(integrate( a*diff(psi_1,x)*diff(psi_1,x)+c*psi_1*psi_2,(x, xa[i], xb[i])))
        k[i].append(integrate( a*diff(psi_1,x)*diff(psi_2,x)+c*psi_1*psi_2,(x, xa[i], xb[i])))
        k[i].append(integrate( a*diff(psi_2,x)*diff(psi_1,x)+c*psi_1*psi_2,(x, xa[i], xb[i])))
        k[i].append(integrate( a*diff(psi_2,x)*diff(psi_2,x)+c*psi_1*psi_2,(x, xa[i], xb[i])))

    F=[]
    F.append(Ftemp[0])
    for i in range(0,len(Ftemp)-2,2):
        F.append(Ftemp[i+1]+Ftemp[i+2])
    F.append(Ftemp[len(Ftemp)-1])

    # print('k = ',k)
    # print('Ftemp',Ftemp)
    # print('F = ',F)
    # three diagonals of the tridiagonal matrix
    diagA=[]
    diagB=[]
    # for three element 4*4 k matrix
    for i in range(nEle):
        diagA.append(k[i][2])
    # print('diagA',diagA)
    # NOTE: no need for diagC as it will always be same as diagA

    diagB.append(k[0][0])
    for i in range(nEle-1):
        diagB.append(k[i][3]+k[i+1][0])
    diagB.append(k[nEle-1][3])
    # print('diagB',diagB)
    diagA=np.array(diagA, dtype=np.float64)
    diagB=np.array(diagB, dtype=np.float64)

    K = np.array( diags([diagB,diagA,diagA], [0,-1, 1]).todense() )
    return(K,F)

def solU(k,f):
    bc1=Symbol('bc1')
    bc2=Symbol('bc2')
    print('k is ',k)
    print(k[1][0])

# length of the domain
domainLen=1
# number of elements
nEle=3
# problem data
a=1
c=-1
f=-x*x

k,f=kfmatix(nEle,domainLen,a,c,f)
print('K matrix : \n',k)
print('f matrix : \n',f)
# boundary conditions

sol = solU(k,f)
# print('Solution, U : \n',linalg.inv(k).dot(f))
```

    K matrix : 
     [[ 2.94444444 -3.05555556  0.          0.        ]
     [-3.05555556  5.88888889 -3.05555556  0.        ]
     [ 0.         -3.05555556  5.88888889 -3.05555556]
     [ 0.          0.         -3.05555556  2.94444444]]
    f matrix : 
     [-0.00308641975308642, -0.0432098765432099, -0.154320987654321, -0.132716049382716]
    k is  [[ 2.94444444 -3.05555556  0.          0.        ]
     [-3.05555556  5.88888889 -3.05555556  0.        ]
     [ 0.         -3.05555556  5.88888889 -3.05555556]
     [ 0.          0.         -3.05555556  2.94444444]]
    -3.05555555556



```python

```


```python

```

991c75f1f391cf1df1e3fc150626a4ef9a365b9a
ipynb
Jupyter Notebook
.ipynb_checkpoints/aakash_cod)-checkpoint.ipynb
nimRobotics/FEM
1b8019109b66b5d95b7caeb113f8d306a2c3c226
MIT
.ipynb_checkpoints/aakash_cod)-checkpoint.ipynb
nimRobotics/FEM
1b8019109b66b5d95b7caeb113f8d306a2c3c226
MIT
.ipynb_checkpoints/aakash_cod)-checkpoint.ipynb
nimRobotics/FEM
1b8019109b66b5d95b7caeb113f8d306a2c3c226
MIT
Qwen/Qwen-72B
1. YES
2. YES