def test_odefun_harmonic(): mp.dps = 15 # Harmonic oscillator f = odefun(lambda x, y: [-y[1], y[0]], 0, [1, 0]) for x in [0, 1, 2.5, 8, 3.7]: # we go back to 3.7 to check caching c, s = f(x) assert c.ae(cos(x)) assert s.ae(sin(x))
def test_odefun_sinc_large(): mp.dps = 15 # Sinc function; test for large x f = sinc g = odefun(lambda x, y: [(cos(x) - y[0]) / x], 1, [f(1)], tol=0.01, degree=5) assert abs(f(100) - g(100)[0]) / f(100) < 0.01
plt.plot(space, fapprox) legendList.append('series, N = ' + str(n)) plt.xlabel('x') plt.ylabel('f') plt.xlim(space[0], space[-1]) plt.ylim(-2, 2) #plt.title("f = %s, together with Taylor approximations of f" % f) plt.legend(legendList, loc=3) #plt.savefig('../figs/sympy_exercise2b.png') #### Problem 3a #### from sympy.mpmath import odefun theta0 = 0.1 # initial value of thetea theta = odefun(lambda t, y: [y[1], -y[0]], 0, [theta0, 0], tol=0.5, degree=2, method='taylor') """odefun solves an ODE initial value problem using Taylors method. odefun(lambda t, y: [y[1], -y[0]], 0, [theta0, 0] ) means that t is the independent variable to perform series expansions around. y: [[y[1], -y[0]]], is the derivates of y written in the standard form of higher order ODE's written as a set of 1st order ODE's: [y1', y2'] 0, means that the initial values are taken at t = 0. this is also the t that odefun will evaluate the taylor series around [theta0, 0] are the initial values of [y1, y2]""" time = np.linspace(0, 5 * 2 * np.pi, 10) y = np.zeros_like(time) for k, t in enumerate(time): y[k] = theta( t )[0] # evaluate the solution at time t, theta(t) is a vector of [theta, theta']. [0] means assigning Y[k] to theta
def test_odefun_sinc_large(): mp.dps = 15 # Sinc function; test for large x f = sinc g = odefun(lambda x, y: [(cos(x)-y[0])/x], 1, [f(1)], tol=0.01, degree=5) assert abs(f(100) - g(100)[0])/f(100) < 0.01
def test_odefun_rational(): mp.dps = 15 # A rational function f = lambda t: 1/(1+mpf(t)**2) g = odefun(lambda x, y: [-2*x*y[0]**2], 0, [f(0)]) assert f(2).ae(g(2)[0])
def test_odefun_rational(): mp.dps = 15 # A rational function f = lambda t: 1 / (1 + mpf(t)**2) g = odefun(lambda x, y: [-2 * x * y[0]**2], 0, [f(0)]) assert f(2).ae(g(2)[0])
fapprox = fTaylorfunc(space) # calculate from 0 to 2pi plt.plot(space, fapprox) legendList.append('series, N = ' + str(n)) plt.xlabel('x') plt.ylabel('f') plt.xlim(space[0], space[-1]) plt.ylim(-2, 2) #plt.title("f = %s, together with Taylor approximations of f" % f) plt.legend(legendList, loc=3) #plt.savefig('../figs/sympy_exercise2b.png') #### Problem 3a #### from sympy.mpmath import odefun theta0 = 0.1 # initial value of thetea theta = odefun(lambda t, y: [y[1], -y[0]], 0, [theta0, 0], tol = 0.5, degree=2, method='taylor') """odefun solves an ODE initial value problem using Taylors method. odefun(lambda t, y: [y[1], -y[0]], 0, [theta0, 0] ) means that t is the independent variable to perform series expansions around. y: [[y[1], -y[0]]], is the derivates of y written in the standard form of higher order ODE's written as a set of 1st order ODE's: [y1', y2'] 0, means that the initial values are taken at t = 0. this is also the t that odefun will evaluate the taylor series around [theta0, 0] are the initial values of [y1, y2]""" time = np.linspace(0,5*2*np.pi, 10) y = np.zeros_like(time) for k, t in enumerate(time): y[k] = theta(t)[0] # evaluate the solution at time t, theta(t) is a vector of [theta, theta']. [0] means assigning Y[k] to theta sampletimes = np.linspace(0, 1, 4) print "analytic: Taylors solution:" for sampletime in sampletimes: