Friday, 4 August 2017

Taylor series with Python and Sympy: Revised

More than 2 years ago I wrote a short post on Taylor series. The post featured a simple script that took a single variable function (a sine in the example), printed out the Taylor expansion up to the nth term and plotted the approximation along with the original function. As you can see on the right on the “Popular posts” bar, that post is one of the most popular and I’m told it appears among the first results on Google.

Figure_1-1

The script I wrote originally was a bit clunky, and there surely was room for improvement. Last week I received an email from a reader, Josh, who sent me an improved version of the original post.

What has changed compared to the original post? Here is a short summary of the changes:

- Lambdify (from the sympy package) is used to speed up the computation.

- The plot function can now handle generic single variable functions and accepts arguments for more customized plotting behaviour.

I also made some improvements to the formatting, which was a bit horrible to say the least : )

Here is an approximation of the function log(x + 1) from the 1st up to the 9th order by steps of 2.

Figure_1

Note that the approximation is evaluated also for x <= 1 which does not make sense and should not be considered. Anyway, as expected, the farther you go from x0, the worse the approximation becomes. If you look closely in the output you’ll also see a warning since the evaluation of log(x + 1) in the region x <= 1 can not be done, but this doesn’t prevent the plot to be drawn.

You can find the improved version of the script here below. Thanks to Josh for taking the time to making it and sending it to me! You can find more about him at his GitHub profile.

Thanks for reading this post!

5 comments:

  1. Very nice code indeed. I tried to convert it in R, but without success so far.

    ReplyDelete
    Replies
    1. Keep trying! Practice is very important!

      Delete
  2. Hiv disease for the last 3 years and had pain hard to eat and cough are nightmares,especially the first year At this stage, the immune system is severely weakened, and the risk of contracting opportunistic infections is much greater. However, not everyone with HIV will go on to develop AIDS. The earlier you receive treatment, the better your outcome will be.I started taking ARV to avoid early death but I had faith in God that i would be healed someday.As a Hiv patent we are advise to be taking antiretroviral treatments to reduce our chance of transmitting the virus to others , few weeks ago i came on search on the internet if i could get any information on Hiv treatment with herbal medicine, on my search i saw a testimony of someone who has been healed from Hiv her name was Achima Abelard and other Herpes Virus patent Tasha Moore also giving testimony about this same man,Called Dr Itua Herbal Center.I was moved by the testimony and i contacted him by his Email.drituaherbalcenter@gmail.com We chatted and he send me a bottle of herbal medicine I drank it as he instructed me to.After drinking it he ask me to go for a test that how i ended my suffering life of Hiv patent,I'm cured and free of Arv Pills.I'm forever grateful to him Drituaherbalcenter.Here his contact Number +2348149277967...He assure me he can cure the following disease..Hiv,Cancer,Herpes Virus,Epilepsy, fibromyalgia ,ALS,Hepatitis,Copd,Parkinson disease.Genetic disease,Fibrodysplasia Ossificans Progressiva,Factor V Leiden Mutation ,Fatal Familial Insomnia ,Dupuytren's disease,Fibrodysplasia Ossificans Progressiva,Fluoroquinolone Toxicity Syndrome,Inflammatory bowel disease ,Huntington's disease ,Diabetes,Fibroid...

    ReplyDelete
  3. Very good information. we need learn from real time examples and for this we choose good training institute, I'm interested to know about digital marketing which is quite interesting. i need a good training institute for my learning .. so am attending the free demo class which is provided by Apponix Technologies.
    https://www.apponix.com

    ReplyDelete
  4. It's a Best post! Thank's for sharing your knowledge to others, it was very informative and in depth one.
    Machine Learaning Using R Training in Electronic City

    ReplyDelete