1
00:00:00,120 --> 00:00:06,420
So now we know how we can propagate the solution for our differential equation of First Order.

2
00:00:06,720 --> 00:00:10,320
And we will now discuss an example, which is the radioactive decay.

3
00:00:11,280 --> 00:00:19,290
So the differential equation for the radioactive decay is that the change of the mass of a radioactive

4
00:00:19,290 --> 00:00:23,370
substance is given by the amount of the substance itself.

5
00:00:23,520 --> 00:00:28,140
Because the decay, you will of course, be faster, the more substance you have.

6
00:00:29,040 --> 00:00:35,190
So the change of Y is given by a negative value times y.

7
00:00:35,940 --> 00:00:41,280
So in general, there is, of course, actually here also a constant which describes two different elements.

8
00:00:41,640 --> 00:00:48,330
But I would say for simplicity, we just write Y dot is equal to minus y and we'll leave out the constant.

9
00:00:48,330 --> 00:00:49,440
We just set it to one.

10
00:00:50,700 --> 00:00:55,680
So this is how you would write that down as a physicist, but as a mathematician, you would write the

11
00:00:55,680 --> 00:00:58,830
Y by D T, which is per definition.

12
00:00:58,830 --> 00:01:02,640
This function f of T and Y is equal to minus Y.

13
00:01:03,780 --> 00:01:10,890
So we know now to propagate this using this solution, we have to just use here to value minus y.

14
00:01:11,040 --> 00:01:13,740
And since it's your Y and we have to write minus point.

15
00:01:13,740 --> 00:01:21,870
And so y and plus one is y n plus minus y end times h.

16
00:01:23,460 --> 00:01:31,140
And before we do this, we want to briefly discuss the analytical solution because then we can compare

17
00:01:31,140 --> 00:01:32,760
later on if our solution is correct.

18
00:01:33,390 --> 00:01:38,520
Well, this differential equation, you have to calculate the first derivative with respect to time,

19
00:01:38,850 --> 00:01:42,180
and the function is essentially the same as just a minus sign here.

20
00:01:42,450 --> 00:01:47,130
So the solution will, of course, be an exponential function, and due to this minus sign here, it

21
00:01:47,130 --> 00:01:50,310
will be its potential function of minus T.

22
00:01:51,300 --> 00:01:55,260
So this is also the typical solution which describes radioactive decay.

23
00:01:56,670 --> 00:01:58,410
So now let's program this.

24
00:01:58,920 --> 00:02:04,380
And first of all, we must define it here some, or we must load some modules.

25
00:02:04,770 --> 00:02:08,639
And so far, we will not really use many of those.

26
00:02:08,639 --> 00:02:13,890
We will probably use a plot loop and we will use numpy, but not separate yet.

27
00:02:14,220 --> 00:02:18,140
But these are all the modules that we are going to need throughout this notebook.

28
00:02:18,150 --> 00:02:20,400
So let's just load all of them at once here.

29
00:02:21,600 --> 00:02:22,040
OK.

30
00:02:22,620 --> 00:02:25,300
So please write this code in your notebook and run it.

31
00:02:25,320 --> 00:02:29,970
And since this is done now, we can start calculating and start programming.

32
00:02:31,170 --> 00:02:39,090
So basically, what we want to do is we want to write a loop because we want to iterate this process

33
00:02:39,090 --> 00:02:39,360
here.

34
00:02:40,110 --> 00:02:45,270
So I write for I in range and then we can write, for example.

35
00:02:45,650 --> 00:02:47,570
Um yeah.

36
00:02:48,120 --> 00:02:53,010
And Max, for example, where and Max is the number of steps.

37
00:02:53,490 --> 00:02:55,320
So let's use 24 now.

38
00:02:56,040 --> 00:03:06,900
This would be the number of iterations, and I will loop from zero to 19 in this case.

39
00:03:08,190 --> 00:03:09,330
So what will happen here?

40
00:03:10,560 --> 00:03:17,880
Well, the first thing that we must do is we must define F because we know that y m plus one is y and

41
00:03:17,880 --> 00:03:19,620
plus f times h.

42
00:03:20,580 --> 00:03:26,130
So we write F is equal to minus y in this case because this is our function.

43
00:03:26,730 --> 00:03:30,060
This will, of course, change when we consider a different differential equation.

44
00:03:31,500 --> 00:03:41,100
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

45
00:03:41,100 --> 00:03:42,210
value of Y.

46
00:03:42,960 --> 00:03:47,910
And then we update it by the value of F and multiply by the step size of each.

47
00:03:49,680 --> 00:03:53,970
So of course, what we have to do now is we have to define these values here.

48
00:03:53,970 --> 00:03:58,320
We have to define H and we have to define some starting value off-line.

49
00:03:59,580 --> 00:04:00,300
So let me do this.

50
00:04:00,300 --> 00:04:06,920
We will define the step size, which is h is equal to zero point one.

51
00:04:06,930 --> 00:04:14,760
And also we will define the starting value, which is why.

52
00:04:15,120 --> 00:04:18,089
And here we can take basically any number doesn't really matter.

53
00:04:18,480 --> 00:04:20,010
It will just scale the function.

54
00:04:20,910 --> 00:04:22,680
So I just take one here.

55
00:04:23,850 --> 00:04:24,260
OK.

56
00:04:24,420 --> 00:04:27,360
So I think at this point, it should work already.

57
00:04:27,690 --> 00:04:29,430
And let's see what's the result.

58
00:04:30,030 --> 00:04:38,940
So we run this algorithm 20 times and then at the end, the result will be Y and it will be zero point

59
00:04:38,940 --> 00:04:40,380
one to one something.

60
00:04:41,400 --> 00:04:43,830
So we have started from one.

61
00:04:44,250 --> 00:04:49,830
So the amount or do you you could also see the mass of the radioactive substance, for example, was

62
00:04:49,830 --> 00:04:56,430
one kilogram and then 20 seconds later, the mass has reduced to 0.1 to one.

63
00:04:57,660 --> 00:04:59,940
So we don't really know yet if this is correct.

64
00:05:00,040 --> 00:05:08,650
Well, of course, we could just calculate and p e p of minus T, which is minus 20 in this case or

65
00:05:08,650 --> 00:05:09,460
minus and max.

66
00:05:10,640 --> 00:05:11,350
So.

67
00:05:15,620 --> 00:05:17,750
Obviously, you know this this is, of course, a mistake.

68
00:05:17,960 --> 00:05:24,350
So the time is, of course given by and max times, each so minus and max times each.

69
00:05:25,100 --> 00:05:31,850
And now we see the amount of substance, according to the analytical solution, is pretty similar to

70
00:05:31,850 --> 00:05:32,930
the numerical solution.

71
00:05:32,930 --> 00:05:35,240
But there is a small difference here.

72
00:05:36,080 --> 00:05:42,620
We can, of course, you know, increase the number of iterations or change to step size, for example,

73
00:05:42,620 --> 00:05:43,100
like this.

74
00:05:45,800 --> 00:05:48,410
And though you see the solution will be much closer.

75
00:05:49,160 --> 00:05:55,370
But we want to analyze the result, of course, in more detail and having just the final result is probably

76
00:05:55,370 --> 00:05:56,150
not sufficient.

77
00:05:56,390 --> 00:06:06,110
So what I will do here is I will collect data and I will define some lists and I will write the T values

78
00:06:06,110 --> 00:06:06,410
here.

79
00:06:06,930 --> 00:06:11,360
We could, of course, start from an emptiness, but I want to actually consider you two starting value

80
00:06:11,360 --> 00:06:11,810
of tea.

81
00:06:12,470 --> 00:06:17,660
And I want to also collect, of course, to my values.

82
00:06:18,020 --> 00:06:24,350
And here I also want to consider the starting value, which is one that we have to right.

83
00:06:24,350 --> 00:06:26,660
Here are tea values.

84
00:06:27,680 --> 00:06:40,280
Dots append is equal to PI times H and the Y values top per pound of Y.

85
00:06:41,210 --> 00:06:50,900
So if we run this, no, then we will see that tea values is a list starting from zero and then going

86
00:06:50,900 --> 00:06:53,570
to OK, maybe that's reduced as a bit.

87
00:06:53,930 --> 00:06:55,940
Let's go back to the initial parameters.

88
00:06:56,720 --> 00:07:03,590
So it starts from zero goes to one point nine and we want until we have twice the value zero and we

89
00:07:03,590 --> 00:07:05,930
want to give it instead to two.

90
00:07:06,710 --> 00:07:12,780
So we can fix this by just writing here range starting at one, two and max plus one.

91
00:07:12,800 --> 00:07:16,010
This is basically just because Python starts counting at zero.

92
00:07:16,010 --> 00:07:21,740
And when you write it like this, Python starts counting at one and this is what we want in this case.

93
00:07:22,370 --> 00:07:27,260
So that's why we run this and you know, you see the T values list is correct.

94
00:07:28,760 --> 00:07:37,790
So let me add here a plot because now we have our tea values and we have our Y values and we can just

95
00:07:37,790 --> 00:07:38,540
write Pulte.

96
00:07:38,570 --> 00:07:48,440
Total scatter tea values come our way values, and you see, this is an exponential function.

97
00:07:49,620 --> 00:07:58,370
Now, when we want to compare this with the analytical solution, we can write a pen pen total in space.

98
00:08:01,120 --> 00:08:02,860
And we have to start at zero.

99
00:08:03,340 --> 00:08:18,310
Go to and max times each and go instead of and max and for the Y values we write, Test Y is equal to

100
00:08:18,490 --> 00:08:20,250
and NPR Dot XP.

101
00:08:20,950 --> 00:08:21,850
We could also write.

102
00:08:23,020 --> 00:08:23,320
Yeah.

103
00:08:24,730 --> 00:08:29,630
Basically, the starting value of Hawaii, which in our case is one so MP.

104
00:08:29,630 --> 00:08:32,650
He thought he XP times oops.

105
00:08:32,770 --> 00:08:33,159
Sorry.

106
00:08:35,409 --> 00:08:37,710
Minus Test T..

107
00:08:39,640 --> 00:08:41,110
And now we can plop this

108
00:08:43,600 --> 00:08:45,190
TLT dot plot.

109
00:08:47,900 --> 00:08:56,810
Test t come up test y and I will put it in red.

110
00:08:58,550 --> 00:09:05,780
So of course, it's always good to add some label just to make sure that you know what's plotted here.

111
00:09:06,050 --> 00:09:11,750
Also, when you come back to the notebook later on, it's often very helpful to label the boxes.

112
00:09:12,560 --> 00:09:13,970
And you see now finally,

113
00:09:16,910 --> 00:09:23,240
that we plot y over T and the blue points are a numerical solution and the red one is the analytical

114
00:09:23,240 --> 00:09:23,780
solution.

115
00:09:24,470 --> 00:09:30,770
And you see, it is kind of OK, but here at the larger the time gets, there will be an arrow, which

116
00:09:30,770 --> 00:09:33,200
we have also seen here by comparing these values.

117
00:09:33,800 --> 00:09:39,170
And we have also seen that we can reduce the arrow by decreasing to step size.

118
00:09:39,980 --> 00:09:43,790
And then in turn, we could, for example, increase the number of iterations.

119
00:09:44,930 --> 00:09:50,610
So let's run this and you see now it looks much better, I think.

120
00:09:51,980 --> 00:09:58,430
So this was an example where we have considered the radioactive decay, where the differential equation

121
00:09:58,430 --> 00:10:04,520
is characterized by the fact that the change in the radioactive substance is given by the amount of

122
00:10:04,520 --> 00:10:05,540
the substance itself.

123
00:10:05,960 --> 00:10:10,090
So why not is given by minus constant times y.

124
00:10:10,670 --> 00:10:15,050
And the analytical solution is, of course, in declining exponential function.

125
00:10:15,440 --> 00:10:22,190
And we have just iterated this equation here and have reproduced the result pretty well.

126
00:10:23,120 --> 00:10:29,480
So in the next lecture, we will define a function ourselves called Euler Odie for ordinary differential

127
00:10:29,480 --> 00:10:29,960
equation.

128
00:10:30,500 --> 00:10:37,040
And with this function, we will generally localize this cell here, which in our case was specific

129
00:10:37,040 --> 00:10:38,990
to this particular differential equation.

130
00:10:38,990 --> 00:10:43,670
But we will generalize this for a general first order differential equation.

