from numpy import * from matplotlib.pyplot import * def f(x): return sin(x) f_coeff = [0, 1./362880., 0, -1./5040., 0, 1./120., 0, -1./6., 0, 1., 0] f3 = poly1d(f_coeff[-4:]) f5 = poly1d(f_coeff[-6:]) f10 = poly1d(f_coeff[-11:]) x = linspace(0, 2.0*pi, 1000) figure() plot(x, f(x), label='f(x)') plot(x, f3(x), label='3rd degree') plot(x, f5(x), label='5th degree') plot(x, f10(x), label='10th degree') axis([0, 2.0*pi, -1, 1]) legend() def g(x): return 1/(1+x*x) g_coeff = [-1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1] g3 = poly1d(g_coeff[-4:]) g5 = poly1d(g_coeff[-6:]) g10 = poly1d(g_coeff[-11:]) x = linspace(-5, 5, 1000) figure() plot(x, g(x), label='g(x)') plot(x, g3(x), label='3rd degree') plot(x, g5(x), label='5th degree') plot(x, g10(x), label='10th degree') axis([-5, 5, 0, 1.5]) legend() def h(x): return x**-12 - x**-6 h_coeff = [349713, -165958, 74295, -31032, 11914, -4116, 1239, -308, 57, -6, 0] h3 = poly1d(h_coeff[-4:]) h5 = poly1d(h_coeff[-6:]) h10 = poly1d(h_coeff[-11:]) x = linspace(1, 5, 1000) figure() plot(x, h(x), label='h(x)') plot(x, h3(x-1), label='3rd degree') plot(x, h5(x-1), label='5th degree') plot(x, h10(x-1), label='10th degree') axis([1, 2, -0.3, 0.1]) legend() show()