Saturday, 14 March 2015

Taylor series with Python and Sympy

Here I am again using my beloved Python and doing maths stuff.

Today I’d like to post a short piece of code I made after a review of Taylor series I did. This script lets you input (almost) any function, provided that it can be represented using Sympy and output the Taylor series of that function up to the nth term centred at x0.

Sympy is a great module for basic symbolic mathematics, it works fine and it is really simple to use even if you are new to Python.

Here is the output of the plot function for the function sin(x) approximating up to the 9th term:


Note that in the console output the series is written backwards, however I think it could be fixed.

Here is the code I used

This code can be customized to return Taylor expansions for any function you’d like to use (of course provided it can be represented using Sympy).

Finally, the code used to generate the plot


  1. Replies
    1. Thanks! I'm glad you liked it! :)

  2. Replies
    1. Thank you for reading the post! :)

  3. How do you define what h is in the tailor series for this code?

    1. Hi, could you please explain your question? I can't find any h in the code

  4. Do you have some examples for multivariable function

  5. Do you mind if I use the code for my final project in a math course I am taking?

    1. Not at all, just please reference this article if you can.