# # xpdf ../../EBook/Elementary_Differential_Equations_10th_Edition.pdf 261 & # import sympy from sympy import * t, A, B, C = symbols('t A B C') y = A * sin(t) + B * cos(t) + C * exp(-t) pt1 = diff(y, t, 4) pt2 = 2*diff(y, t, 3) pt3 = diff(y, t, 2) pt4 = 8*diff(y, t, 1) pt5 = -12*y residual = ( pt1 + pt2 + pt3 + pt4 + pt5 - ( 12*sin(t) - exp(-t) ) ) print(residual) # Enforce the initial conditions: # c1, c2, c3, c4 = symbols('c1 c2 c3 c4') y = c1 * exp(t) + c2 * exp(-3*t) + c3 * cos(2*t) + c4 * sin(2*t) - (4./5) * sin(t) - (2./5) * cos(t) + (1./20) * exp(-t) print(y.subs(t, 0)) print(diff(y, t, 1).subs(t, 0)) print(diff(y, t, 2).subs(t, 0)) print(diff(y, t, 3).subs(t, 0)) # Verify the coefficients we compute for c1, c2, c3, and c4: # c1 = 2.0250000 c2 = 0.1403846 c3 = 1.1846154 c4 = -0.3769231 y = c1 * exp(t) + c2 * exp(-3*t) + c3 * cos(2*t) + c4 * sin(2*t) - (4./5) * sin(t) - (2./5) * cos(t) + (1./20) * exp(-t) pt1 = diff(y, t, 4) pt2 = 2*diff(y, t, 3) pt3 = diff(y, t, 2) pt4 = 8*diff(y, t, 1) pt5 = -12*y lhs = ( pt1 + pt2 + pt3 + pt4 + pt5 ) print(simplify(lhs)) print(y.subs(t, 0)) print(diff(y, t, 1).subs(t, 0)) print(diff(y, t, 2).subs(t, 0)) print(diff(y, t, 3).subs(t, 0)) print( [81./40, 73./520, 77./65, -49./130] ) # check that these match the numbers in the back of the book