#! /usr/bin/env python3 # def bvp_example ( ): from sympy import cos from sympy import diff from sympy import pi from sympy import pprint from sympy import symbols import sympy print ( '' ) print ( 'bvp_example():' ) print ( ' sympy version: ' + sympy.__version__ ) print ( ' We define a boundary value problem of the form:' ) print ( ' -u" + u u\' = f(x).' ) print ( ' We want the exact solution to be' ) print ( ' u(x) = (2/pi) * (1 - cos (pi*x/2) ).' ) print ( ' We need to manufacture the right hand side f(x).' ) x = symbols ( 'x' ) u0 = 2 * ( 1 - cos ( pi * x / 2 ) ) / pi print ( '' ) print ( ' u(x):' ) print ( '' ) pprint ( u0 ) u1 = diff ( u0, x ) print ( '' ) print ( ' dudx(x):' ) print ( '' ) pprint ( u1 ) u2 = diff ( u0, x, 2 ) print ( '' ) print ( ' d2udx2(x):' ) print ( '' ) pprint ( u2 ) f = - u2 + u0 * u1 print ( '' ) print ( ' f(x) = -u" + u u\'' ) print ( '' ) pprint ( f ) return if ( __name__ == "__main__" ): bvp_example ( )