So now we know how we can propagate the solution for our differential equation of First Order.
And we will now discuss an example, which is the radioactive decay.
So the differential equation for the radioactive decay is that the change of the mass of a radioactive
substance is given by the amount of the substance itself.
Because the decay, you will of course, be faster, the more substance you have.
So the change of Y is given by a negative value times y.
So in general, there is, of course, actually here also a constant which describes two different elements.
But I would say for simplicity, we just write Y dot is equal to minus y and we'll leave out the constant.
We just set it to one.
So this is how you would write that down as a physicist, but as a mathematician, you would write the
Y by D T, which is per definition.
This function f of T and Y is equal to minus Y.
So we know now to propagate this using this solution, we have to just use here to value minus y.
And since it's your Y and we have to write minus point.
And so y and plus one is y n plus minus y end times h.
And before we do this, we want to briefly discuss the analytical solution because then we can compare
later on if our solution is correct.
Well, this differential equation, you have to calculate the first derivative with respect to time,
and the function is essentially the same as just a minus sign here.
So the solution will, of course, be an exponential function, and due to this minus sign here, it
will be its potential function of minus T.
So this is also the typical solution which describes radioactive decay.
So now let's program this.
And first of all, we must define it here some, or we must load some modules.
And so far, we will not really use many of those.
We will probably use a plot loop and we will use numpy, but not separate yet.
But these are all the modules that we are going to need throughout this notebook.
So let's just load all of them at once here.
OK.
So please write this code in your notebook and run it.
And since this is done now, we can start calculating and start programming.
So basically, what we want to do is we want to write a loop because we want to iterate this process
here.
So I write for I in range and then we can write, for example.
Um yeah.
And Max, for example, where and Max is the number of steps.
So let's use 24 now.
This would be the number of iterations, and I will loop from zero to 19 in this case.
So what will happen here?
Well, the first thing that we must do is we must define F because we know that y m plus one is y and
plus f times h.
So we write F is equal to minus y in this case because this is our function.
This will, of course, change when we consider a different differential equation.
And now the value of Y, which we will call Y so y at the step and plus one will be given by the old
value of Y.
And then we update it by the value of F and multiply by the step size of each.
So of course, what we have to do now is we have to define these values here.
We have to define H and we have to define some starting value off-line.
So let me do this.
We will define the step size, which is h is equal to zero point one.
And also we will define the starting value, which is why.
And here we can take basically any number doesn't really matter.
It will just scale the function.
So I just take one here.
OK.
So I think at this point, it should work already.
And let's see what's the result.
So we run this algorithm 20 times and then at the end, the result will be Y and it will be zero point
one to one something.
So we have started from one.
So the amount or do you you could also see the mass of the radioactive substance, for example, was
one kilogram and then 20 seconds later, the mass has reduced to 0.1 to one.
So we don't really know yet if this is correct.
Well, of course, we could just calculate and p e p of minus T, which is minus 20 in this case or
minus and max.
So.
Obviously, you know this this is, of course, a mistake.
So the time is, of course given by and max times, each so minus and max times each.
And now we see the amount of substance, according to the analytical solution, is pretty similar to
the numerical solution.
But there is a small difference here.
We can, of course, you know, increase the number of iterations or change to step size, for example,
like this.
And though you see the solution will be much closer.
But we want to analyze the result, of course, in more detail and having just the final result is probably
not sufficient.
So what I will do here is I will collect data and I will define some lists and I will write the T values
here.
We could, of course, start from an emptiness, but I want to actually consider you two starting value
of tea.
And I want to also collect, of course, to my values.
And here I also want to consider the starting value, which is one that we have to right.
Here are tea values.
Dots append is equal to PI times H and the Y values top per pound of Y.
So if we run this, no, then we will see that tea values is a list starting from zero and then going
to OK, maybe that's reduced as a bit.
Let's go back to the initial parameters.
So it starts from zero goes to one point nine and we want until we have twice the value zero and we
want to give it instead to two.
So we can fix this by just writing here range starting at one, two and max plus one.
This is basically just because Python starts counting at zero.
And when you write it like this, Python starts counting at one and this is what we want in this case.
So that's why we run this and you know, you see the T values list is correct.
So let me add here a plot because now we have our tea values and we have our Y values and we can just
write Pulte.
Total scatter tea values come our way values, and you see, this is an exponential function.
Now, when we want to compare this with the analytical solution, we can write a pen pen total in space.
And we have to start at zero.
Go to and max times each and go instead of and max and for the Y values we write, Test Y is equal to
and NPR Dot XP.
We could also write.
Yeah.
Basically, the starting value of Hawaii, which in our case is one so MP.
He thought he XP times oops.
Sorry.
Minus Test T..
And now we can plop this
TLT dot plot.
Test t come up test y and I will put it in red.
So of course, it's always good to add some label just to make sure that you know what's plotted here.
Also, when you come back to the notebook later on, it's often very helpful to label the boxes.
And you see now finally,
that we plot y over T and the blue points are a numerical solution and the red one is the analytical
solution.
And you see, it is kind of OK, but here at the larger the time gets, there will be an arrow, which
we have also seen here by comparing these values.
And we have also seen that we can reduce the arrow by decreasing to step size.
And then in turn, we could, for example, increase the number of iterations.
So let's run this and you see now it looks much better, I think.
So this was an example where we have considered the radioactive decay, where the differential equation
is characterized by the fact that the change in the radioactive substance is given by the amount of
the substance itself.
So why not is given by minus constant times y.
And the analytical solution is, of course, in declining exponential function.
And we have just iterated this equation here and have reproduced the result pretty well.
So in the next lecture, we will define a function ourselves called Euler Odie for ordinary differential
equation.
And with this function, we will generally localize this cell here, which in our case was specific
to this particular differential equation.
But we will generalize this for a general first order differential equation.