```python
import os, sys
sys.path.insert(0, os.path.join(os.pardir, 'src'))
from varform1D import solver
import sympy as sym
x, b = sym.symbols('x b')
f = 1

# Compute basis functions and their derivatives
N = 4
psi = {0: [x**(i+1) for i in range(N+1)]}
psi[1] = [sym.diff(psi_i, x) for psi_i in psi[0]]

# Galerkin

def integrand_lhs(psi, i, j):
    return psi[1][i]*psi[1][j]

def integrand_rhs(psi, i):
    return -f*psi[0][i]

Omega = [0, 1]

u, c = solver(integrand_lhs, integrand_rhs, psi, Omega,
              verbose=True, symbolic=True)
print(('Galerkin solution u:', sym.simplify(sym.expand(u))))

# Least squares
psi = {0: [x**(i+2) for i in range(N+1)]}
psi[1] = [sym.diff(psi_i, x) for psi_i in psi[0]]
psi[2] = [sym.diff(psi_i, x) for psi_i in psi[1]]

def integrand_lhs(psi, i, j):
    return psi[2][i]*psi[2][j]

def integrand_rhs(psi, i):
    return -f*psi[2][i]

Omega = [0, 1]

```

    ...evaluating matrix... (0,0): 1
    (0,1): 2*x
    (0,2): 3*x**2
    (0,3): 4*x**3
    (0,4): 5*x**4
    rhs: -x
    (1,1): 4*x**2
    (1,2): 6*x**3
    (1,3): 8*x**4
    (1,4): 10*x**5
    rhs: -x**2
    (2,2): 9*x**4
    (2,3): 12*x**5
    (2,4): 15*x**6
    rhs: -x**3
    (3,3): 16*x**6
    (3,4): 20*x**7
    rhs: -x**4
    (4,4): 25*x**8
    rhs: -x**5
    
    A:
     Matrix([[1, 1, 1, 1, 1], [1, 4/3, 3/2, 8/5, 5/3], [1, 3/2, 9/5, 2, 15/7], [1, 8/5, 2, 16/7, 5/2], [1, 5/3, 15/7, 5/2, 25/9]]) 
    b:
     Matrix([[-1/2], [-1/3], [-1/4], [-1/5], [-1/6]])
    coeff: [-1, 1/2, 0, 0, 0]
    approximation: x**2/2 - x
    ('Galerkin solution u:', x*(x - 2)/2)



```python

```

5250be5fafafd6e50cd59c51cfc8422c8aec3741
ipynb
Jupyter Notebook
Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/FINITE_ELEMENTS/INTRO/EXERCICES/06_CABLE_XN.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/FINITE_ELEMENTS/INTRO/EXERCICES/06_CABLE_XN.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/FINITE_ELEMENTS/INTRO/EXERCICES/06_CABLE_XN.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