Series Expansion and Plotting

It is possible to expand many SymPy expressions as Taylor series. The series method makes this straightforward. At minimum, we must specify the expression and the variable in which to expand it. Optionally, we can also specify the point around which to expand, the maximum term number, and the direction of the expansion.
>>> from sympy import *
>>> x = Symbol('x')
>>> sin(x).series(x, 0)
x - x**3/6 + x**5/120 + O(x**6)
>>> series(sin(x), x, 0)
x - x**3/6 + x**5/120 + O(x**6)
>>> cos(x).series(x, 0.5, 10)
1.11729533119247 - 0.438791280945186*(x - 0.5)**2 + 0.0799042564340338*(x - 0.5)**3 + 0.0365659400787655*(x - 0.5)**4 - 0.00399521282170169*(x - 0.5)**5 - 0.00121886466929218*(x - 0.5)**6 + 9.51241148024212e-5*(x - 0.5)**7 + 2.17654405230747e- 5*(x - 0.5)**8 - 1.32116826114474e-6*(x - 0.5)**9 - 0.479425538604203*x + O((x - 0.5)**10, (x, 0.5))
In some cases, especially for numerical evaluation and plotting the results, it is necessary to remove the trailing O(n) therm. To do that just type .remove0(n)
>>> cos(x).series(x, 0.5, 10).removeO()
-0.479425538604203*x - 1.32116826114474e-6*(x - 0.5)**9 + 2.17654405230747e-5*(x - 0.5)**8 + 9.51241148024212e-5*(x - 0.5)**7 - 0.00121886466929218*(x - 0.5)**6 - 0.00399521282170169*(x - 0.5)**5 + 0.0365659400787655*(x - 0.5)**4 + 0.079904256 4340338*(x - 0.5)**3 - 0.438791280945186*(x - 0.5)**2 + 1.11729533119247
SymPy provide two inbuild function, Plot()  from the sympy.plotting module, and plot from sympy. mpmath.visualization. Generally for most purposes the Matplotlib should be the plotting tool of choice. (CHECK OUT THE POSTS MATPLOTLIB).
Here is an example of plotting Sympy Computation.
>>> from sympy import sin, series, Symbol
>>> import pylab
>>> x = Symbol('x')
>>> s10 = sin(x).series(x, 0, 10).removeO()
>>> s50 = sin(x).series(x, 0, 50).removeO()
>>> s = sin(x)
>>> xx = []
>>> y10 =[]
>>> y50 = []
>>> y = []
>>> for i in range(1000):
...     xx.append(i/100.0)
...     y10.append(float(s10.subs({x:i/100.0})))
...     y50.append(float(s50.subs({x:i/100.0})))
...     y.append(float(s.subs({x:i/100.0})))
...
>>> pylab.figure()

>>> pylab.plot(xx, y10, label='O(10)')
[]
>>> pylab.plot(xx, y50, label='O(50)')
[]
>>> pylab.plot(xx, y, label='sin(x)')
[]
>>> pylab.axis([0, 10, -4, 4])
[0, 10, -4, 4]
>>> pylab.xlabel('x')

>>> pylab.ylabel('f(x)')

>>> pylab.legned()
>>> pylab.savefig('sympy.pdf')
>>> pylab.savefig('sympy.png')
>>> pylab.show()


1 komentar:

  1. Free Slots Online | Casino Guru
    Best Free Casino Slot Games · 1xbet 주소 Wild West · 라이브 배팅 Videoslots · Keno 1xbet 우회 · Roulette · Blackjack · bet Baccarat · Craps · Video 인터넷 바카라 Poker.

    OdgovoriIzbriši