Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying IPython Interactive Computing and Visualization Cookbook
  • Table Of Contents Toc
IPython Interactive Computing and Visualization Cookbook

IPython Interactive Computing and Visualization Cookbook

By : Cyrille Rossant
4.5 (13)
close
close
IPython Interactive Computing and Visualization Cookbook

IPython Interactive Computing and Visualization Cookbook

4.5 (13)
By: Cyrille Rossant

Overview of this book

Intended to anyone interested in numerical computing and data science: students, researchers, teachers, engineers, analysts, hobbyists... Basic knowledge of Python/NumPy is recommended. Some skills in mathematics will help you understand the theory behind the computational methods.
Table of Contents (17 chapters)
close
close
16
Index

Finding the root of a mathematical function


In this short recipe, we will see how to use SciPy to find the root of a simple mathematical function of a single real variable.

How to do it…

  1. Let's import NumPy, SciPy, scipy.optimize, and matplotlib:

    In [1]: import numpy as np
            import scipy as sp
            import scipy.optimize as opt
            import matplotlib.pyplot as plt
            %matplotlib inline
  2. We define the mathematical function f(x)=cos(x)-x in Python. We will try to find a root of this function numerically. Here, a root corresponds to a fixed point of the cosine function:

    In [2]: f = lambda x: np.cos(x) - x
  3. Let's plot this function on the interval [-5, 5] (using 1000 samples):

    In [3]: x = np.linspace(-5, 5, 1000)
            y = f(x)
            plt.plot(x, y)
            plt.axhline(0, color='k')
            plt.xlim(-5,5)
  4. We see that this function has a unique root on this interval (this is because the function's sign changes on this interval). The scipy.optimize module contains a few root-finding functions...

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
IPython Interactive Computing and Visualization Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon