So in the following, we want to, of course, solve more examples of differential equations and not
just the specific ordinary differential equation that characterizes a radioactive decay.
So what we're going to do is we are going to define a new function of herself, which basically mimics
these two cells here.
And in order to do this, let me just copy the code from these two cells.
And also this one.
And now, of course, we will change these cells and define a function.
So to define the function, we write death.
And then we give it a name.
And I recall it.
Euler Ozdemir, you can give it any name, basically, that is not given for other functions already.
And now we have to specify here the arguments and here will be the code.
And the code will, of course, be pretty similar to what we have written down here.
So let me first write down what we need as arguments we need definitely f where f will be the function
of the differential equation.
So when I scroll up here, it will be this one here.
So basically the first derivative of Y with respect to T, then we will need a starting time.
This is not really necessary because most of the time will be we will be starting at zero but could
be helpful to to find a starting time, then we will need a starting value.
So this will be essentially this one here.
So I will write y zero for the starting value of Y.
Then we need the number of iterations
which is and max, and then we need to step six.
Which is H.
And now I, of course, have to add all of these arguments to the function.
So I write f comma, t zero comma, y zero comma and max comma.
Page Sorry, it's too.
It's like this.
All right.
So now we have our arguments and we have here.
This commons define what these arguments mean.
So this is a recommendation from me that you do this for the functions that you define yourself so that
later on you understand what these arguments actually describe.
So now we need to get going.
So what we definitely are going to need is this one here.
So the preparation of the arrays and then we will also need the loop, of course.
But now we must change this maybe a bit because maybe it's not really correct anymore.
For example, we provide here the function F, so we do not need this command here anymore.
We already have our F.
And so here we are.
Right Y is equal to y plus f with the arguments t comma y and we multiply by h then what we also need
since we do not.
Yeah, since basically we have this time t zero now is we need to have time, an internal time running
in the system.
So of course, we could just add here a t0, but I think it's a bit more.
It's it gives you a bit of a better overview.
If you define here a variable called T, it's not really necessary.
You could also solve this differently, but I will define your variable T.
And so in each time step, we will increase T by H.
Of course, here just starting value, we will now use T and not zero.
And we will adhere to T values.
So I write just values append T and I think now we are almost finished.
We just need to load this y0 zero here.
So we need to write y is equal to y zero.
And I think this should already be sufficient.
So you see, we haven't really changed much.
Basically, we have just changed the function.
We have changed the starting value and then we have done this t0, which was optional, however.
So you see, it's really not difficult once you solve a specific example to define a function.
And this is also my recommendation that when you are not quite sure how to define the function, just
try to solve an example without a function and then use the solution to define the function later on.
Because you see, it was pretty easy to do this.
So the only thing that's missing here is the output, because all of this code is just internal in this
function, it will give no output yet.
So we need to write to return.
So we could write print, then it would just print what we whatever we want as the output.
But we want to actually use return so that we are then able to assign these values to variables.
So we print an array of T values and Y values.
So I have run this, of course, it gives me no output.
And now we can test this at our example.
This is always the best thing you should do first, when you define a function and you have already
solved an example, try to solve the example now with the function and see if the solution is still
correct.
So what we need is we need to call, of course, this function, so we write oiler potty and then we
must provide all of these arguments.
So we provide a function f, which I will call F40.
So this function we still have to define.
So let me write this down here so that we do it later on, so define function.
And here we call the other method.
And then we need to provide the t0, which I will just use zero, as in our previous example, then
for the Y0 we will use, yeah, we used one previously, but I will write here why zero zero is equal
to one.
We could also do it like this t0 and T zero is equal to one.
Sorry, two zero.
Then we have.
And Max and Max was equal to 20.
I think it was equal to 200.
And age was equal to zero point zero one.
OK, so now the only thing that's left to do is to define the function.
And this is not so easy, maybe at if you have never done it before, because here we have provided
this with the arguments T and why.
So we must define a function that also has these arguments.
So we cannot just right here minus y.
So I write the I'm sorry, the fine f o d e.
And then we provide the same arguments as above.
T come away, of course.
And then the only thing the function does is give the value of the functions, so return and the value
of the function is minus Y.
And I think now it should work.
So I run this and it gives me a new solution, which is a list of X. Well, I sort of T values and the
list of Y values.
So now we can store any solutions in the variables, so I will call this solution.
And now we can plot this so we can just take the plot from here.
And changed the T values to solution the first element, so index zero and then the Y values is the
other element.
And you see, it still gives the correct result, even though now the solution has been determined by
calling our all the method function that we have just programmed in this lecture.