```python
import sympy as sym
x, L, C, D, c_0, c_1, = sym.symbols('x L C D c_0 c_1')

def model1(f, L, D):
    """Solve -u'' = f(x), u(0)=0, u(L)=D."""
    # Integrate twice
    u_x = - sym.integrate(f, (x, 0, x)) + c_0
    u = sym.integrate(u_x, (x, 0, x)) + c_1
    # Set up 2 equations from the 2 boundary conditions and solve
    # with respect to the integration constants c_0, c_1
    r = sym.solve([u.subs(x, 0)-0,  # x=0 condition
                   u.subs(x,L)-D],  # x=L condition
                  [c_0, c_1])       # unknowns
    # Substitute the integration constants in the solution
    u = u.subs(c_0, r[c_0]).subs(c_1, r[c_1])
    u = sym.simplify(sym.expand(u))
    return u

def model2(f, L, C, D):
    """Solve -u'' = f(x), u'(0)=C, u(L)=D."""
    u_x = - sym.integrate(f, (x, 0, x)) + c_0
    u = sym.integrate(u_x, (x, 0, x)) + c_1
    r = sym.solve([sym.diff(u,x).subs(x, 0)-C,  # x=0 cond.
                   u.subs(x,L)-D],              # x=L cond.
                  [c_0, c_1])
    u = u.subs(c_0, r[c_0]).subs(c_1, r[c_1])
    u = sym.simplify(sym.expand(u))
    return u

def model3(f, a, L, C, D):
    """Solve -(a*u')' = f(x), u(0)=C, u(L)=D."""
    au_x = - sym.integrate(f, (x, 0, x)) + c_0
    u = sym.integrate(au_x/a, (x, 0, x)) + c_1
    r = sym.solve([u.subs(x, 0)-C,
                   u.subs(x,L)-D],
                  [c_0, c_1])
    u = u.subs(c_0, r[c_0]).subs(c_1, r[c_1])
    u = sym.simplify(sym.expand(u))
    return u


def demo():
    f = 2
    u = model1(f, L, D)
    print(('model1:', u, u.subs(x, 0), u.subs(x, L)))
    print((sym.latex(u, mode='plain')))
    u = model2(f, L, C, D)
    #f = x
    #u = model2(f, L, C, D)
    print(('model2:', u, sym.diff(u, x).subs(x, 0), u.subs(x, L)))
    print((sym.latex(u, mode='plain')))
    u = model3(0, 1+x**2, L, C, D)
    print(('model3:', u, u.subs(x, 0), u.subs(x, L)))
    print((sym.latex(u, mode='plain')))

if __name__ == '__main__':
    demo()



```

    ('model1:', x*(D + L*(L - x))/L, 0, D)
    \frac{x \left(D + L \left(L - x\right)\right)}{L}
    ('model2:', -C*L + C*x + D + L**2 - x**2, C, D)
    - C L + C x + D + L^{2} - x^{2}
    ('model3:', (C*atan(L) - C*atan(x) + D*atan(x))/atan(L), C, D)
    \frac{C \operatorname{atan}{\left(L \right)} - C \operatorname{atan}{\left(x \right)} + D \operatorname{atan}{\left(x \right)}}{\operatorname{atan}{\left(L \right)}}



```python

```

066c129840a97eaf5f8341a7a803d54af933e636
ipynb
Jupyter Notebook
Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/FINITE_ELEMENTS/INTRO/SRC/39_U_XX_F_SYMPY.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/FINITE_ELEMENTS/INTRO/SRC/39_U_XX_F_SYMPY.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/FINITE_ELEMENTS/INTRO/SRC/39_U_XX_F_SYMPY.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