Approximating the Zeros of a Function (Using Python, It’s Very Easy)

Video Series: Functions and Their Graphs (Step-by-Step Tutorials for Precalculus)

(D4M) — Here is the video transcript for this video.

in this episode we’ll practice approximating the zeros or root of a function
using the python programming language don’t worry everything is explained
let’s do some math [Music] all right welcome back everybody um
before we get started with the uh python part
uh let’s quickly review what a zero of a function is so the zeros of a function
are those inputs whose output is zero so we can look at this
you know and just say we we have a generic graph here a function right here
so here’s a zero here’s a zero and here’s a zero so like this is a graph of
a function and this would be say root number one let’s call it root one um
[Applause] and let’s say here this would be root number two

and this would be root number three and this function just keeps going down
and so this is just decreasing decreasing here
and a little bit increasing but anyways we get to a root and we get to another
root and we get to another root so we call those uh x values those inputs
where the heights the or the outputs are zero
so those are roots and we can go with a formal definition here if f is a
function then the x values for which you plug in and get out 0
are called the zeros of f or sometimes we call them the roots of f
all right and so in the last episode we did some examples where we looked at
lots of graphs and we talked about finding the zeros
so in this uh episode we’re going to use python to help us find these uh
approximating these roots here so before we move on though i’d like to

mention that this episode is part of the series functions and their graphs
step-by-step tutorials for beginners especially if you’re in a pre-calculus
course this is a good video to help you understand uh zeros and help help you
define them now you can find the zeros using a graphing calculator
but if you don’t have the money or you know you just don’t want to buy a
graphing calculator then you can do it if you have some kind of computer or if
you have a phone or something like that so check out the link below in the
description and you can start with your own python
notebook and you can follow along just click on the link below and then i
think you click on something like new document excuse me
and then you’ll be able to do everything
that we’re going to do here so let’s get started let’s go to the code now
so let’s get this out of the way here all right

so let’s uh zoom in here and make this bigger here and
let’s get our setup going here so i’m in the python notebook
and these are the packages that i’m going to import to make things quick and
easy so we’re going to import matplot library and numpy and senpai
and this right here for this video right here this is different uh we haven’t
looked at this um yet and so we’re going to import the fsolve from this package
right here and this will allow us to do a lot of work really quick all right
now because we’re going to be making some graphs
i talked about this in previous episodes but this is the way i like to customize
my axes so i call this function pc for precalculus axes
and so yeah just use that if you don’t want to customize your axes you don’t
have to but i use this function i use this definition right here
all right so let’s execute both of those i’m going to click in the cell and hit
shift enter and then click in the cell right here and hit shift enter

and then well let’s go look at the functions that we’re going to use for
this episode here in fact actually this uh function right
here is the exact same one we used in the last episode where we talked about
finding the or approximating the relative extrema so i’m going to plot
the function first so we’re going to input a function
and i got some default values here so the default for the input of x minimum
is minus 10 and x maximum is 10 and the increment is
0.1 so these are just some friendly default values to help you get started
but the point is is that we’re going to define a figure and some axes
we’re going to customize the axes we’re going to give it a whole bunch of input
inputs sorry give it a bunch of inputs we’re going to go from -10 to 10
in whatever increments we want and then we’re going to calculate the
outputs so we’re going to take the each of the inputs

and calculate them so we need to have a function uh set into this function right
here then we’re going to plot it and then
we’re going to show the plot now once we
get a good plot going then we’ll be able to try to find the zeros of of that
and the way to do that is by using this function right here
which you have to give it a function and you have to give it a guess now to find
that guess is what we’re going to use this function for so we’re going to plot
the function we’re going to look at it and we’re going to make some kind of
educated guess and then the the meaty part the reason why this is
only one line long right here is because we’re using the fsolve function
and that takes in a function and a guess and actually gives you a whole bunch of
values right here so i’m only going to choose the first or the zeroth
uh entry in the array and that will be the root and so you
want to use this function right here when you’re certain that there’s only
one zero in that around that guess right there so i’ll

talk about that by looking at some examples right here
all right so let’s execute those two right there and get those defined
and so there we go so one two three four we’ve executed four cells so far in our
notebook and now let’s go look at some examples here
now these are going to actually be the exact same uh functions that we worked
with in the previous episode but that that episode talked about
finding relative extrema so now we’re finding the um zeros so find the zeros
all right so find the zeros of the function right so i’m going to define f1
as a function and i’m using f1 because this is the
first example and i’m just using generic f right so there’s the name of my
function f1 so it’s a minus two x squared so there’s
how we do an x squared and then plus one

so i’m going to define my function let’s hit shift enter
and now what i’m going to do with that function is i’m going to plot it before
i start trying to find a 0 for it i’m going to plot it and i’m just going to
use the standard window minus 10 to 10 and i think it has a zero through there
in fact maybe it has two it’s hard to tell um let’s find a better window
so i’m going to try minus one to one and yeah looks like we’re getting two
zeros right there zero right there and zero right there
so i’m going to try to find those two zeros right there so let’s first find
the zero right here the one on the left now you want to zoom out more
perhaps before you start trying to find zeros to convince yourself that those
are the only two zeros or we zoomed out really far um

and it just looks like it’s just increasing and then it’s just decreasing
and i say really far what do i mean by that i mean we really zoomed out we’re
getting minus 20 000 down here minus 20 000 is pretty large compared to these
numbers over here compared to the way this is structured
uh minus 20 000 significantly larger that it convinces me
that the only zeros we’re going to have are in here
so i’m going to go back to my my window here um now that i looked at it
uh zoomed out i’m much more confident that these are the only two here
now that looks pretty close to negative 0.75 so that’s what uh is going to be my
guess so i called my function here 0 function i’m going to use this with f1
so here we go we’re going to use the function called 0 not 0s and then f1 and
then my guess is going to be 0 negative 0.75 there we go

so there’s my 0 right there so that’s the actual x value
that we’re getting right there now at this point i would like to just
mention the difference between this episode and the last
the last episode we were trying to find um or we were finding the relative
extrema the relative extrema are y values output values
and so in that video or that episode we returned y values the heights where you
actually had the relative extrema in this episode we’re returning x values
because the roots are the actual x values right there so this is the actual
root right here the x value where the function height is zero
all right so now we can go try to find this one right here and what am i going
to use for a guess i could use like 0.6 0.8 0.4

but for this guess right here to find this one i don’t want to choose minus 1
1 is not a good guess for this root minus 1 is an okay guess for this root
but if i give it a guess of minus 1 it’s probably going to give me that one back
there so let’s just try that real quick so let’s try 0 again and
let’s get a new cell escape b and this time for my guess i’m going to
choose minus 1 which is a little bit farther away than minus 0.75
so if i tried -1 it should give me that one right there right
so that’s a bad guess for this one over here so this one over here i don’t know
exactly where it’s at but i’m going to guess 0.75 all right so let’s try that

there we go um and let’s take away the negative sign
all right there we go and now coincidentally we’re getting the same
number here but positive because this graph is symmetric by the way it’s
symmetric with respect to the y-axis so you could imagine that
whatever negative that was is where over here would be the same but positive
and so that that right there is is just confirming that all right so there’s uh
two roots for this function right here we found the two zeros
at least approximately all right and so now let’s go on to
another one right here um so let’s um again we’re going to find the zeros
or the roots let’s call it roots just for fun this time
and now let’s type in the definition so definition i’m going to call this f2

and then we’re going to go with 2 times and then so i’m going to return
so return 2 times and then x to the third
so we’ve got an exponent there and then minus two times x and n plus one
all right so there’s our function there and since i accidentally clicked on that
let’s change that real quick find the roots
all right so now let’s go here and make a new cell i use escape b for that
all right so now what we’re going to do is we’re going to plot this right here
so let’s go up here and copy that plot function here and
i’m going to use what windows should we use so let’s plot f2
and i’ll just use the default window there and so i’m going to use here um
about uh you know we’re looking for roots here this is just going to keep

going up and there’s not this is not going to
curve any it’s just going to it’s just going to be increasing here so i’m going
to say minus 2.5 to 2.5 all right and so that we’re definitely
getting a root here and are we getting one here or not let’s
wait on this one let’s find this one right here first so let’s just uh copy
that right there and get a new window or new cell sorry and then paste that in
and change our function we’re on the second example now
so for my guess i’m going to choose so for my root right here
um i’m just going to choose minus 1 minus 1 is close enough it’s not going
to get confused with that one [Music] so -1 here for my guess

there we go so it gave me minus 1.19 so that right there is going to be the
the zero right there is minus 1.19 and now let’s try to find this one over
here if there is even one right there at
all so to find out if there’s one or not there
i’m going to change the window here to a zero um and it seems like i got
um oops missing a comma all right so it doesn’t look like it
touches there let’s just uh double check our efforts there
yeah definitely doesn’t look like now you see how it’s a little bit uh
line segmenty like it’s a little bit segmented like that that’s because my
increments are pretty small right now or pretty large um
so you know that’s the default value right there i’ll throw in some other
zeros we can make it a little bit more curvier but that’s not going to change
the fact that there’s no zeros there it’s just not going to cross or touch

the x-axis there all right so we found one root for this right here
uh on example two and it was this one right here
all right so now let’s look at uh this one right here example three
x to the third minus three x squared minus nine plus six let’s execute that and
i’m going to borrow this copy and paste again
and come down here and get a new cell and maybe a second one just in case i
need one all right and now let’s plot f3 using the defaults
and there we go so it looks like it’s going to have some roots maybe one maybe
two maybe three let’s zoom in a little bit just to um
you know get a better better better picture better guesses

so how about minus three to um i don’t know five maybe
there we go yeah that’s a good window yeah so there we can see our three zeros
right there very good um and so now let’s just go up here and
copy and paste this and i’m gonna use it once and so i need to f3
and i’m going to get another one and another one
all right so there we go so we’re going to find three of them so what’s a good
guess for this first one here so the first one i’m just going to guess of -2
boom we got the root and now this one right here i’m just going to guess a 0.5
so i just guessed a 0.5 in here and it says 0.577

and then for this one over here i’ll just guess a 4.5
all right and there it is it came up with 4.656 so there we go
4.656 right there for that zero right there all right so um there’s uh the third
example we got two more to go to right here real quick find the roots
and our function here is x to the fourth minus four x to the third plus six
you know try to find the roots of this by hand
factoring or you know exactly how you’re going to do that how are you going to
solve this equation right here can you do that
so you’re not going to try factor by grouping or fact all right
so let’s execute and declare f4 so we got f4 defined

and now let’s go up here and borrow this and then get a new cell
and maybe we need a couple of them and i’m going to get a zero in there but
first i’m going to get a plot going all right so here we go i’m going to plot f4
and i just guess that the same window might work and it doesn’t look too shabby
it looks right here like there’s a zero and it looks right here there’s a zero
now i know this keeps going up because i’ve looked at this function in the last
episode we looked at it we changed the domain
or the x min and x max we changed the window a lot
same thing on this right here we know this is just going to keep decreasing
if you’re not clear on that change the window
so you can view it up here and you can view it up here more
but we’re going to try to find these roots right now these zeros
so i’m going to execute this twice here but they’re going to be for the function

f4 and for this guess right here for this root right here
i’m just going to guess 1.5 and then we actually get 1.3
and then for this one right here i’m just going to guess a 4
or since i already have in here typed up 4.5 i’ll just use that
and there we go 3.898 okay so there we go there’s the two roots of that
and then let’s do example five now find the roots
anything interesting couldn’t come up in this one all right
now actually um i like this example here because what this tells me
is that i don’t need a computer i can find them really fast
because this is already factored so x is 0 and x is 2.

those are going to be the zeros right there so we don’t need to go do anything
else and so uh we define the function here f5
and let’s get some cells going here and we plot
f5 and we said they’re going to happen between 0 sorry 0 and 2
so i’ll look at minus 1 to 3 and then we can see the 0 there and we
can see the 0 at 2. um and then we can just say 0 of f5 and
my guess would just be zero and you get out of zero and here my guess is

say oops parentheses f5 here let’s say my guess is say i don’t
know 1.8 or something just to be fun so it basically just gives you two
so you can see how it’s an approximation we know the exact values too
simply because in this example we can actually solve for it
and the other examples we couldn’t um i just realized
hope i just did it on this one that that’s mistyped there and
i did it there again too i wonder if i just did it in the last episode
all the way through but uh sorry about that um in any case um
let’s look at two more examples before we go here let’s look at example 6
and find the roots of the function and got it typed up already so x to the

3rd minus three x plus two all that is the numerator and then x
squared plus two x plus one all that is the denominator so let’s plot that
um let’s go up here to get the plot and we’re going to plot f6
and we got the divide by zero which we talked about in the last um example we
can just ignore that there’s a zero uh somewhere in here and
i mean it’s dividing by zero in here it’s it’s there’s an isotope that’s the
problem um so the question is does this have any
zeros right here so does it cross the x-axis anywhere here
so let’s zoom out here and see if we can figure this out here so it
looks like it is decreasing and then it starts to

increase but does it ever actually cross so right here um it certainly crosses
um looks somewhere around negative 2.5 but what about here so let’s zoom in
around between let’s say negative 0.5 and 2. um
still can’t tell if it crosses or not let’s try positive 0.5 right there
oh there that looks like it almost touches at one let’s go to 1.2 and
let’s go to 0.8 and let’s make the increments better so that

it doesn’t look weird looks like it’s hitting zero right there all right
well i moved it let’s see what happens um when we find the zero
and let’s go between a zero point or let’s say the guess is one and
we’re getting a two let’s see here when i guess at the root of one
the zero gives me out the zero function gives me out a two um let’s see here
what if we move over to a 2.2 so it’s just giving me out gibberish

there’s no there’s nothing at two here um what happens if we actually go up here
and say what is f of six at one you see we actually get the zero out right there
um oh i see what’s happening um there we go all right that makes sense
f of six is my guess of one yeah we get the zero one there okay
all right that makes perfect sense all right so now let’s go um
so there’s one zero at one and now what about for the other side so
now let’s go back to the other side what was happening over there

um let’s see we need to go further and let’s say we go to negative one here
and how about changing that to a one so let’s see here
we’re looking somewhere around here um where does it cross maybe we need to go
a little further right and let’s just go to positive one here
positive five maybe so i can’t seem to remember what window where it looked good

for us so let’s just come back out and go to -10 to 10 and minus 2.5 2.5 and
yeah so why did i think there was a zero right there so what if we went to minus
25 all right so i think it crosses here but um so let’s try to zoom in now
i don’t seem to be having good luck on the left side there

so let’s say here this is i don’t need all this stuff over here so
let’s say negative 0.05 and where is a good guess over here so we can say the
uh here it is so let’s try the guess for over here let’s just guess negative 1.4
and it looks looking like it’s happening at negative two
so let’s go this out to negative three and negative one
and it’s hard to tell that it’s crossing right there at negative two [Music] um

but that’s what’s happening so this is decreasing right here
and then it’s just decreasing long term so what if we zoom out really far and
it doesn’t seem like it’s decreasing long term here but it is 300 all right so
we’re getting 0 at negative 2 here really close in here
and then after that it’s just decreasing but it’s doing it very slowly here
let me try this here just in case something uh got messed up here

let’s go and save this and then restart and run the kernel again here right
all right so let’s move on now to this last one here let’s get here
back in here let me move out of the way over here
all right so i’m going to define this function right here f7
for this function right here x over square root
and then we have square root of x squared minus 1 here
and then now let’s go over here and um get a new cell and then
we’re going to plot this f7 right here and let’s just use the standard window
here and we talked about last time about the domain of this function right here

from from 1 and on and -1 and on here so the domain is right there
uh open these are open right here so minus infinity to minus 1
union 1 to positive infinity here so that’s the domain of this right here so
we’re not going to see anything defined in here
and so we’re going to really break this up into two pieces here let’s look at it
the first one let’s plot the right half first so let’s say one two
five let’s say for example and then we get a nice curve right there
so the question is does this ever curve down and make a zero out of it so let’s
zoom out a little bit let’s try 50 here [Music]
and yeah so it’s just leveling off right there
um so we’re not going to have any zeros over here

and then let’s try the opposite let’s try -50 over here and a minus one here
and we’re going to get the same behavior over here
it’s just leveling off and we’re never going to get any zeros here so here’s a
function um [Music] you know here’s a here’s a function
right here that doesn’t have any zeros if you go to try to solve this
algebraically and say all right let me try to find the zeros here
so then you’ll say oh the x has to be zero which is true for the numerator has
to be zero but if you try zero in the denominator
it’s not defined x equals zero is not in the domain so this function right here
has no zeros all right so there we go so there’s
seven examples we dived in pretty good and um hope you got a better um feeling
for how you can use python and um get these zeros
so i look forward to seeing you in the next episode have a great day

if you enjoyed this video please like and subscribe to my channel and click
the bell icon to get new video updates

About The Author
Dave White Background Blue Shirt Squiggles Smile

David A. Smith (Dave)

Mathematics Educator

David A. Smith is the CEO and founder of Dave4Math. His background is in mathematics (B.S. & M.S. in Mathematics), computer science, and undergraduate teaching (15+ years). With extensive experience in higher education and a passion for learning, his professional and academic careers revolve around advancing knowledge for himself and others. His work helps others learn about subjects that can help them in their personal and professional lives.

Let's do some math together, today!

Contact us today.



About Us


© 2022

Privacy Policy | Terms of Service