Now, do we know what a derivative is?
Let us discuss how we can define this in Python.
So we basically have to implement one of these three equations.
But the problem is that we have this limit h goes to zero.
And of course, in numerics, the H can never really go to zero.
And also, it could happen that we have some great some data points, some X coordinate, some Y coordinates,
and we do not really know what is in between these data points.
So of course, in this case, we could first to add interpolation and we could interpolate the function
to have access to the points in between.
But also, it's quite convenient to just be able to calculate the derivative based on the points that
we have.
So what this means, basically, is that h will have some fixed value.
It will be the difference between two points or the difference of the X coordinates of the two points.
And then here we will have the difference of the Y coordinates of the two points.
And this is exactly what I have written down here.
In a good approximation, we can write down that the derivative of F at some point and is given by the
difference of the Y coordinates divided by the difference of the X coordinates.
And as discussed previously, we can do this in terms of forward differences, backward differences
and also central differences.
And in this case, since we don't have the limit h goes to zero, all three methods can give a different
result.
And this is what we will explore first.
We will implement these three methods and we will investigate how the derivatives will look different
for the three methods for this example, function.
We have chosen a bit of a more difficult function so that we can really see some maxima and minima and
the function.
It's sine function multiplied by X and then some third order polynomial function.
So as always, we will first import Nampai and much plot lib.
So let me run the cell and then I will first define our function.
So I write def half of X, and of course, the output will be a return function and it will just return
to value of the function.
So I write and pitot sine of X and then times X, and then the other turn will be minus one over 100
times X to the power of three.
So this is our function, which is now defined and now we can continue and we can, yeah, have a look
at the function.
So for this, I will write X list over the final list of X coordinates flyer and P total in space.
And for the plot range, I've chosen minus 10 to 10 and I will use 201 points because by doing so,
the difference between each of these points will be zero point one.
So we have now our X list.
I can define Y list, so this will be, you know, just f our function.
And then the argument will be D X list.
And since this is here and every since I wrote and put model in space, we can apply the function to
every element by writing it down like this.
And so now we are almost finished.
We can just write pal T dot plot and X List Comma Y list, and then we can add some labels for the XS
Plus Tot X label, which will be X and Y label will be F of X.
And we run the cell and have a look at our plot.
So now you can see here, it's really a pretty complicated plot with several maxima and minima.
And in the next step, we are going to calculate the derivative according to these three equations here.
So what we do next is we, of course, take our X list, so it's not really necessary to define it once
again.
But here we'll do it anyway, just in case we change something later on.
And then I will, first of all, calculate the derivative analytically.
So analytical the list.
So for derivative.
And so our function is has two parts.
So in X times x and then this polynomial, the derivative of the polynomial is, of course, very simple.
It's just three times one over 100 times x squared.
And then here for this function, since it's a product, we must use the product rule and we get two
terms for the derivative.
The first one will be cosine times X because the derivative of sine as cosine.
And then the other term will be just sine x times one because the derivative of X is one.
So we will have three terms in total.
This will be an impedance cosine of X lists times x list.
Then the other term will be A. Dots sine of X list times of one.
And then the last term will be minus three times one of 100 times X lists square.
And of course, now we can plot this.
So let's just copy the plot command and change this list here.
And let's right here derivative.
And I did the mistake.
This is oh no, not really a mistake.
But this is, of course, because for the dash here, for the derivative, we cannot use the same symbol.
So let me replace two symbols that let me write it down like this.
Then it should work.
OK.
So the resolution is not really that good in that plot, but I think I think it's sufficient to just
do it like that.
So you see, we have a derivative function, which also has several maxima and minima.
And for example, when the function has a maximum, it goes through zero here.
All right.
So it seems to be correct.
And now we can also add the numerical derivatives.
And for this, I will first of all, define in h value, which will be zero point one so that it corresponds
basically to the data points in our list.
So this h two step size of zero point one is the same difference between two points in this and p total
in space of our X list.
And now I can define a list, for example, for what D list?
And now I must
define it in this way.
So we take the value of the function at the next point.
Minus the values value of the function at the current point.
Divide it by H, basically.
So I write down the value of the function.
At the Find X list plus age minus, well, you have to function at X list and then we divide the whole
thing by H.
So the next thing that we can do is backwards differences here we take the value of the function minus
the value of the function at the previous point, and then we will also program the central differences,
which will be the difference of the following point and the previous point divided by 2H this time.
So let's program it.
Here we are.
So I will just copy this line twice, so I don't have to type so much and I will write backward and
central.
And then here we said we'd take the current point minus the previous point, divided by age.
And here we said we take the following point minus the previous point divided by two h.
All right.
So now we have created these three lists characterizing derivatives calculated by three different methods.
And now we can add these plots, and I just have to exchange the names for the lists.
So let me do this takes a few seconds, and I hope you also program along with me, and when I run this,
we basically see not a big difference.
I mean, we can still see here red blow to green blood, maybe a blue plot in the background.
So there is some small difference comparing the three plots, but the difference is quite small.
But of course, now on the following we want to analyze this in more detail.
And for this, I want to analyze now the arrow.
So I will copy the plot function and I will write for the y axis out of the derivative.
And so what we do is we take this list analytical list as a reference point and we will subtract the
other lists from these reference points.
So like this, and I will delete this list because this is no our reference until we will plot only
the difference.
This works because we choose the same X list for all of the plots, so we cannot just calculate the
difference of the why list.
And now I will add some color so that we know which plot corresponds to which function blue and green.
So when I run it, we see this is all a result.
This is the error of the derivative of X compared to the analytical result, which we know is the correct
one.
And we see that basically the red and the blue function have quite a large difference.
At some points it goes even up to 0.4, which is considerable if we consider our function as a change
from plus seven to minus 12.
So yeah, it is some error of a few percent.
And what's also interesting is that we can also see that the absolute error of the forward and the backwards
method is the same.
It's just a different sign.
But most importantly, the green function, which is the central method, has a much, much smaller
arrow.
It's much better to use this method for calculating the derivative.
And in the following lecture, we want to explore why this is the case.
So the name is why is the central differences methods typically better than the forwhat and the backroads
differences methods?