1
00:00:00,150 --> 00:00:06,600
So let's get started with our physical example, where we use a Monte-Carlo algorithm to solve a very,

2
00:00:06,600 --> 00:00:07,920
very difficult problem.

3
00:00:08,700 --> 00:00:15,930
So here we are considering magnets and you probably know a ferromagnetic, for example, a piece of

4
00:00:15,930 --> 00:00:17,960
iron and it's magnetic.

5
00:00:17,970 --> 00:00:26,940
It attracts some other materials and probably also know that inside of this material, you have small

6
00:00:26,940 --> 00:00:27,650
atoms.

7
00:00:27,720 --> 00:00:35,850
And basically, you kind of consider these as small individual magnets and these magnets they can orient

8
00:00:35,850 --> 00:00:40,710
with respect to each other and, for example, the fairer magnets they will all be pointing along the

9
00:00:40,710 --> 00:00:41,750
same direction.

10
00:00:41,790 --> 00:00:47,070
So all the small magnets will be parallel, and therefore they will form this large magnet.

11
00:00:47,910 --> 00:00:49,800
And then there are other forms of magnetism.

12
00:00:50,250 --> 00:00:57,840
For example, you could have just randomly oriented individual magnets and then overall, the magnetism

13
00:00:57,840 --> 00:01:00,090
of the whole sample would cancel out.

14
00:01:00,930 --> 00:01:04,080
And this is something we want to simulate in the following.

15
00:01:04,769 --> 00:01:10,770
So you can already imagine that this is very, very difficult to simulate because in the real material,

16
00:01:11,070 --> 00:01:14,470
we have so many atoms and so many individual magnets.

17
00:01:14,850 --> 00:01:22,170
And all of these magnets can be oriented along different orientations, so they have X, Y and Z components.

18
00:01:22,470 --> 00:01:28,920
So we have so many variables that we have to investigate, and what we are searching for is the configuration

19
00:01:28,920 --> 00:01:30,600
with the lowest and energy.

20
00:01:31,350 --> 00:01:37,080
And it turns out, since we have so many variables, we cannot straightforwardly calculate the state

21
00:01:37,080 --> 00:01:41,790
with the lowest energy, and this is where the Monte Carlo algorithm comes into play.

22
00:01:42,660 --> 00:01:50,520
We will basically randomly change the orientation of individual magnets and see if this decreases the

23
00:01:50,520 --> 00:01:51,480
energy or not.

24
00:01:52,230 --> 00:01:54,810
And if it decreases the energy, then that's good.

25
00:01:55,260 --> 00:01:59,970
And if it increases the energy, then we basically say, OK, the step.

26
00:02:00,180 --> 00:02:02,400
The random reorientation does not count.

27
00:02:03,330 --> 00:02:04,220
So that's the whole idea.

28
00:02:04,230 --> 00:02:08,419
We use randomness and sometimes it will decrease the energy.

29
00:02:08,430 --> 00:02:11,070
It's good and will it increases the energy.

30
00:02:11,340 --> 00:02:15,150
We will just disregard this change of the configuration.

31
00:02:15,240 --> 00:02:16,500
That's the whole idea here.

32
00:02:17,400 --> 00:02:20,670
So I will, of course, go into the more detail in the following.

33
00:02:21,000 --> 00:02:28,770
But let us start with generating our actual set up, our starting configuration.

34
00:02:29,550 --> 00:02:35,850
So we will set up a grids, an array of positions and that each of these positions, there will be a

35
00:02:35,850 --> 00:02:40,200
magnetic moment and then we will plot this configuration.

36
00:02:40,200 --> 00:02:43,420
And then I can explain to you much better what we will do afterwards.

37
00:02:44,160 --> 00:02:48,510
But you can see already here in the notebook, we will then consider the energy, which I'm going to

38
00:02:48,510 --> 00:02:49,680
explain to you soon.

39
00:02:50,070 --> 00:02:56,400
And then we will run the Monte Carlo, which is basically a loop of a so-called metropolis steps, which

40
00:02:56,400 --> 00:02:59,100
I have also already explained to you very briefly.

41
00:02:59,100 --> 00:03:01,680
But we will get into more detail later on.

42
00:03:02,520 --> 00:03:05,160
For now, let's generate the starting configuration.

43
00:03:06,180 --> 00:03:14,370
So as I said, we have a array of magnetic moments and they sit at different positions and altogether,

44
00:03:14,370 --> 00:03:16,800
this array of magnetic moment forms.

45
00:03:17,160 --> 00:03:20,640
Our thorough magnets or our magnet in general.

46
00:03:21,720 --> 00:03:28,500
And you see we can give each of these magnets an index, for example, one to end, but we could also

47
00:03:28,500 --> 00:03:32,260
arrange them in the grid and then each of these magnets would have two indices.

48
00:03:32,280 --> 00:03:35,730
This is how we are actually going to do it in our program later on.

49
00:03:36,750 --> 00:03:41,250
And what's important is that the length of each moment is fixed.

50
00:03:41,670 --> 00:03:45,300
And in our case, we will say the length is fixed to one.

51
00:03:46,230 --> 00:03:52,680
So this means every magnetic moment has a length of one, but the orientation is not fixed.

52
00:03:52,680 --> 00:03:53,460
It is two.

53
00:03:53,460 --> 00:03:54,570
It can be totally random.

54
00:03:54,570 --> 00:03:58,010
It can basically show into any direction.

55
00:03:58,020 --> 00:04:03,960
It can point along any direction on a sphere of a ball, basically.

56
00:04:05,490 --> 00:04:06,480
So how do we do this?

57
00:04:06,720 --> 00:04:12,930
How do we generate this or orientation of each of these moments randomly?

58
00:04:14,190 --> 00:04:20,550
So we could say, OK, let's consider an X component, the Y component, the Z component, and let's

59
00:04:20,550 --> 00:04:24,600
generate those and then divide it by the length of the vector.

60
00:04:25,530 --> 00:04:27,430
This would be OK.

61
00:04:27,630 --> 00:04:35,790
But actually, it wouldn't give a good representation of randomness, because then not all points or

62
00:04:35,790 --> 00:04:41,850
not, all orientations of these vectors, I would be at equal proportions.

63
00:04:42,750 --> 00:04:49,890
So it's much better to straightforwardly generate moments that already have a length of one, and we

64
00:04:49,890 --> 00:04:52,950
can do this by using spherical coordinates.

65
00:04:53,610 --> 00:04:54,840
What I've written down here.

66
00:04:55,470 --> 00:04:59,890
So this means since the length is fixed, we have from these very quick.

67
00:04:59,970 --> 00:05:05,310
Ordinates, the radius, which is fixed to one, and then we only have the polar angle fight and die

68
00:05:05,320 --> 00:05:06,750
as a to angle fighter.

69
00:05:07,560 --> 00:05:11,700
So in some textbooks, it's also labeled differently these angles here.

70
00:05:12,990 --> 00:05:19,050
So what do we have to do then is we have to generate random angle fire in the range of zero two two

71
00:05:19,050 --> 00:05:24,180
pi and a random angle theta in the range of zero to PI.

72
00:05:24,660 --> 00:05:26,460
And this is what we will start with.

73
00:05:27,270 --> 00:05:30,060
Let me create a cell here and let me start writing.

74
00:05:31,110 --> 00:05:37,830
Well, first of all, we must say how many of these moments we have and this I call length.

75
00:05:38,430 --> 00:05:41,010
So I say, let's start with 10.

76
00:05:41,400 --> 00:05:46,020
And I want to consider a quadratic grids of moments.

77
00:05:46,350 --> 00:05:51,090
So the total number of moments would then be length squared.

78
00:05:51,420 --> 00:05:56,940
So 100 in our case, and now we need to generate these random angles.

79
00:05:57,420 --> 00:06:01,950
And I start with random five and this would be a random number.

80
00:06:01,950 --> 00:06:10,560
So ENPI dot, random thought round and the number of moments that we were, what a number of angles

81
00:06:10,560 --> 00:06:12,420
that we are going to generate is numb.

82
00:06:13,170 --> 00:06:15,960
So like this and peddled random dot random.

83
00:06:16,800 --> 00:06:21,090
And then I said the range should be from zero to two pi.

84
00:06:21,660 --> 00:06:26,460
So far, it's from zero to one, so we just have to multiply by two pipe.

85
00:06:28,770 --> 00:06:31,550
And now we do a very similar thing for Theta, of course.

86
00:06:31,550 --> 00:06:34,500
So I will copy this and write Theta here.

87
00:06:35,910 --> 00:06:40,890
And Theta only goes from zero to pi and not to two pi.

88
00:06:42,300 --> 00:06:49,110
So if we run this and look at the result, you will see, for example, four random fire we have now

89
00:06:49,140 --> 00:06:56,250
an array of 100 moments or not moments yet, but only angles yet in the range of zero to two pi.

90
00:06:57,990 --> 00:07:05,340
So no way will do something that is not really necessary at this point, but we will need it anyway

91
00:07:05,340 --> 00:07:06,570
for the plotting later on.

92
00:07:06,990 --> 00:07:09,840
So I will structure these angles here

93
00:07:12,300 --> 00:07:15,030
to basically reshape the array.

94
00:07:15,600 --> 00:07:17,310
So you will see in a second what this does.

95
00:07:17,800 --> 00:07:28,020
So I will write here dot reshape and then I'll write length for the number of points in the X direction,

96
00:07:28,740 --> 00:07:31,530
length for the number of points in the wider action.

97
00:07:32,040 --> 00:07:35,400
And one for the number of points in the Z direction.

98
00:07:36,690 --> 00:07:40,020
And I do the same thing also for the other one, I run it.

99
00:07:40,680 --> 00:07:44,190
And you see, it's now the data as before.

100
00:07:44,310 --> 00:07:48,140
It's, of course, a bit of a different values because I have rerun those random commands.

101
00:07:48,140 --> 00:07:53,780
So the values differ now, of course, but we have now just reshaped a light array.

102
00:07:53,790 --> 00:07:56,160
We have basically introduced some brackets.

103
00:07:56,880 --> 00:08:01,200
And the reason is because now we can access certain elements here.

104
00:08:01,410 --> 00:08:09,980
For example, we could write zero zero and then we would get the fire, maybe another zero here.

105
00:08:10,530 --> 00:08:14,280
This would then be the randomly generated angle.

106
00:08:14,280 --> 00:08:17,280
Phi for X equals zero.

107
00:08:17,280 --> 00:08:19,200
Y equals zero, z equals zero.

108
00:08:19,560 --> 00:08:27,390
And for example, we could also do like this so we can really address know the individual angles and

109
00:08:27,390 --> 00:08:28,710
facilitate the same thing.

110
00:08:30,240 --> 00:08:36,120
So now we generate the magnetization, so we don't need this one in here anymore, so we generate the

111
00:08:36,150 --> 00:08:37,530
array of magnetic moments.

112
00:08:37,799 --> 00:08:39,419
According to this equation.

113
00:08:40,559 --> 00:08:46,950
So it is an array and pitot array, and now we must write to three components.

114
00:08:47,280 --> 00:08:48,090
So it's pretty easy.

115
00:08:48,090 --> 00:08:59,670
And to cosine and then random five times and p don't sign random theta.

116
00:09:00,870 --> 00:09:05,160
It would be the X component or the component with the zero index.

117
00:09:05,760 --> 00:09:13,230
Then for the Y component, we have a very similar thing we have here and P Dot sign random phi times.

118
00:09:13,230 --> 00:09:20,310
And if you don't sign and theta and for the Z component, we have an odd cosine random data.

119
00:09:21,330 --> 00:09:23,140
So I hope I didn't make a typo here.

120
00:09:23,160 --> 00:09:23,610
Let's run.

121
00:09:23,610 --> 00:09:25,170
It doesn't give an arrow.

122
00:09:25,170 --> 00:09:25,590
That's good.

123
00:09:26,280 --> 00:09:28,050
And now we can look at it once again.

124
00:09:28,620 --> 00:09:35,250
This is now our magnetic magnetization, and we could now write, for example, or something like this.

125
00:09:37,650 --> 00:09:43,930
This will now be the magnetic moment and X equal five y equal to Z equal to zero.

126
00:09:44,280 --> 00:09:47,610
And it has X, Y and Z components.

127
00:09:48,150 --> 00:09:52,650
And we can, of course, also address these components individually by writing here, for example,

128
00:09:52,650 --> 00:09:55,110
one, then we will get the Y component.

129
00:09:56,820 --> 00:09:57,260
OK.

130
00:09:58,260 --> 00:09:59,090
So that's pretty good.

131
00:09:59,100 --> 00:09:59,590
We have an.

132
00:10:00,080 --> 00:10:04,670
Our magnetization area and we have structured it already.

133
00:10:05,810 --> 00:10:11,360
So actually, I'm seeing I missed here the heading, so let me shift this up here.

134
00:10:11,450 --> 00:10:13,310
This is how I want to have it.

135
00:10:13,910 --> 00:10:14,330
OK.

136
00:10:14,510 --> 00:10:18,840
And now we can do just one more thing that is not really necessary.

137
00:10:18,920 --> 00:10:21,260
What I want to show you that everything worked just fine.

138
00:10:21,650 --> 00:10:23,660
I want to check the norm.

139
00:10:24,590 --> 00:10:27,320
And this we can do by writing magnetization.

140
00:10:29,630 --> 00:10:30,290
Zero.

141
00:10:30,800 --> 00:10:37,190
And then all the elements square plus magnetization one.

142
00:10:37,760 --> 00:10:39,560
All the elements square.

143
00:10:39,570 --> 00:10:48,260
So this basically means X component squared plus Y components squared plus C component square.

144
00:10:50,390 --> 00:10:53,960
And then we can also calculate a square root and Peter's square root.

145
00:10:56,390 --> 00:11:01,310
And you see, it gives as an array and a whole lot of number ones.

146
00:11:01,520 --> 00:11:06,140
So this means the norm of every one of these moments is indeed one.

147
00:11:06,800 --> 00:11:14,270
So, for example, we could now check once again here for X equals five y equal to the equals zero the

148
00:11:14,270 --> 00:11:17,030
norm as one as for every other moment.

149
00:11:18,020 --> 00:11:18,680
So this worked.

150
00:11:18,680 --> 00:11:25,940
We have now these am eyes in this whole array and the length is fixed to one, and all of these moments

151
00:11:25,940 --> 00:11:27,320
have been chosen randomly.

152
00:11:29,090 --> 00:11:33,320
So now we will continue and create the position.

153
00:11:33,740 --> 00:11:35,510
And this is really easy.

154
00:11:35,540 --> 00:11:44,150
We write what we call this coordinates or codes, and it's supposed to be an array, and the array is

155
00:11:44,150 --> 00:11:47,480
generated by using NP dot mesh grid.

156
00:11:48,950 --> 00:11:53,520
So this is something we have done already several times for plots, for example, for contour plots.

157
00:11:54,020 --> 00:11:58,070
And now we mash different ranges.

158
00:11:58,100 --> 00:12:06,530
So and Pinchot arrange length basically is a list of numbers starting from zero going to a length minus

159
00:12:06,530 --> 00:12:06,890
one.

160
00:12:07,100 --> 00:12:10,610
So basically zero one two three four five six seven eight nine.

161
00:12:11,240 --> 00:12:22,350
And then we do this also for the Y component and for the Z component, we only have a single value.

162
00:12:22,400 --> 00:12:28,880
We could here also consider basically a three dimensional sample.

163
00:12:28,890 --> 00:12:32,000
But for simplicity, I want to stick with a two dimensional sample.

164
00:12:32,420 --> 00:12:38,810
So to see value only has an index, or at least that just contains a single value, which is zero.

165
00:12:39,500 --> 00:12:44,820
OK, so now we have our list of coordinates and we have our list of magnetic moments.

166
00:12:45,320 --> 00:12:50,630
And now we can use a plot that is called a quiver plot.

167
00:12:51,530 --> 00:12:52,970
So let me show you how this works.

168
00:12:53,150 --> 00:12:57,380
Of course, it's a three dimensional plot because our arms are three dimensional.

169
00:12:57,380 --> 00:13:04,050
They have an X, Y and Z component so we can write our plots.

170
00:13:04,050 --> 00:13:06,580
It's just a name for the plot fields.

171
00:13:06,590 --> 00:13:16,190
He got axes and then projection is equal to 3D two x to enable the 3D object.

172
00:13:17,420 --> 00:13:32,810
And now I will continue to just write R2 plots, dot quiver and then the syntax is as follows.

173
00:13:33,650 --> 00:13:38,330
So we write first course zero, which basically.

174
00:13:41,550 --> 00:13:48,420
Corresponds to the values for the for the values, so the X coordinates of our list coordinates.

175
00:13:48,750 --> 00:13:54,780
Then the Y coordinates and here the Z coordinates, and then we do the same thing for the magnetization.

176
00:13:55,260 --> 00:13:59,430
And here you see, I have constructed the magnetization in the way that we can use known in the same

177
00:13:59,430 --> 00:14:00,000
syntax.

178
00:14:00,600 --> 00:14:01,320
You could have done that.

179
00:14:01,740 --> 00:14:05,490
We could have structured your magnetization array, of course, in a different way.

180
00:14:05,970 --> 00:14:10,240
But I think in such a way, it's much easier to understand what's going on here.

181
00:14:11,220 --> 00:14:19,110
So when I run this, I get some really strange stuff looks awful, but at least it worked.

182
00:14:19,110 --> 00:14:20,190
We have now a plot.

183
00:14:20,610 --> 00:14:21,570
Now we must fix it.

184
00:14:22,080 --> 00:14:23,130
So what happened here?

185
00:14:24,210 --> 00:14:33,380
I think the problem is that our arrows have a length of one and the X and Y values on the axes.

186
00:14:33,390 --> 00:14:36,160
They are scaled from zero to 10, from zero to 10.

187
00:14:36,180 --> 00:14:36,780
That is good.

188
00:14:37,020 --> 00:14:39,060
This is really the data arrangement we have.

189
00:14:39,510 --> 00:14:47,010
But since we only have a single value for the Z coordinates, which is zero, the plot is really zoomed

190
00:14:47,010 --> 00:14:47,340
in.

191
00:14:48,210 --> 00:14:53,880
However, the arrows can have a length of one also in the Z and the C direction.

192
00:14:53,900 --> 00:14:57,390
So this means they are really out of range and this is what we're seeing here.

193
00:14:58,050 --> 00:15:02,760
So I think we just must fix the D Z limit for the plots.

194
00:15:03,210 --> 00:15:13,110
We can do this by writing our story a title arrow plot dot and then set underscore the z limb.

195
00:15:13,890 --> 00:15:18,480
Then we specified a limit, and I think it makes sense to use minus 1.1.

196
00:15:18,960 --> 00:15:19,800
And here we go.

197
00:15:20,850 --> 00:15:25,770
It still doesn't look so nice, but at least now we can see the whole arrows.

198
00:15:26,520 --> 00:15:34,020
So you see, it looks totally confusing here, and this is because we have randomly generated these

199
00:15:34,020 --> 00:15:34,590
arrows.

200
00:15:35,610 --> 00:15:42,930
So one more thing that I don't like is that the plot is actually distorted because this axis, this

201
00:15:42,930 --> 00:15:46,020
axis and this one, they all have the same length and the plot.

202
00:15:46,470 --> 00:15:51,150
But actually, this one has a length of 10 this often and this only of two.

203
00:15:51,630 --> 00:15:57,210
So we should specify the ratio of this plot or the ratio of the box.

204
00:15:57,930 --> 00:16:08,520
We can do this by writing arrow plots don't set on the school books aspect, and then we can write aspect

205
00:16:09,330 --> 00:16:13,950
is equal to and then we specify the three values.

206
00:16:14,220 --> 00:16:24,840
So I said the x axis has a length of length, and since it goes from those from zero to 10, we write

207
00:16:24,840 --> 00:16:27,660
length and the y axis also.

208
00:16:28,020 --> 00:16:31,190
And the z axis has a length of two, so I write two.

209
00:16:32,800 --> 00:16:36,030
Now you see all the arrows have the same length.

210
00:16:36,420 --> 00:16:41,130
I mean, it looks sometimes as of the arrow was rather short, but this is just due to the projection

211
00:16:41,130 --> 00:16:42,540
due to the viewpoint.

212
00:16:43,380 --> 00:16:46,710
So now we really have our array of of arrows.

213
00:16:47,790 --> 00:16:51,700
So let me add some more options that I found to be useful here.

214
00:16:51,720 --> 00:17:00,630
So I think we don't really need the gray background here, so I will make the ground invisible.

215
00:17:02,460 --> 00:17:07,290
And this we can do by writing arrow plots, dot axes, falls.

216
00:17:09,030 --> 00:17:10,200
So you see, now it's gone.

217
00:17:10,740 --> 00:17:13,560
And just as a last thing, I will add another plot.

218
00:17:14,369 --> 00:17:23,900
So there's no Akuma plot, and then I will add here another one, a coordinate plot where I just point

219
00:17:23,910 --> 00:17:26,040
out where just plotted the points of the coordinates.

220
00:17:26,730 --> 00:17:35,130
And we do this by writing Arrow plots, dots get a three v and this time we own the plot, the coordinates,

221
00:17:35,130 --> 00:17:40,260
as I said, and for the color we can use, for example, red.

222
00:17:42,150 --> 00:17:47,160
So you see, now we have this Gritz of the positions and you see at each position, there is a magnetic

223
00:17:47,160 --> 00:17:54,570
moment indicated by this arrow and the arrow indicates the orientation of and so we see now this is

224
00:17:54,570 --> 00:17:55,890
here of a whole magnet.

225
00:17:56,220 --> 00:18:00,420
It consists of individual magnets and they have certain orientation.

226
00:18:01,890 --> 00:18:06,300
So one more thing we can, of course, now increase the number of points.

227
00:18:06,310 --> 00:18:10,830
For example, we could use 40 here, then we must rerun all of these cells.

228
00:18:10,830 --> 00:18:14,190
So maybe looking around the whole notebook to make it faster.

229
00:18:15,920 --> 00:18:23,420
And in this case, we run into the problem that the plot is just too small to see anything and to fix

230
00:18:23,420 --> 00:18:28,820
this a nice way is to use the following code.

231
00:18:29,630 --> 00:18:33,170
So this will change the standard size of all figures in his notebook.

232
00:18:33,620 --> 00:18:40,220
You basically access some property RC firearms for your adult fake size and you specify the size.

233
00:18:40,730 --> 00:18:43,130
And here you have to tune a bit what works best for you.

234
00:18:43,130 --> 00:18:44,030
I think for me.

235
00:18:44,540 --> 00:18:46,280
This one should work pretty well.

236
00:18:46,430 --> 00:18:51,650
But if you have a different screen and a different zoom in your browser and maybe you have to tweak

237
00:18:51,650 --> 00:18:52,370
these values.

238
00:18:52,610 --> 00:18:54,980
But the aim is just to make the plot a bit larger.

239
00:18:54,980 --> 00:19:00,530
And you see, now we have a really nice plot and everything looks totally confusing, but this is how

240
00:19:00,530 --> 00:19:01,010
we want it.

241
00:19:01,020 --> 00:19:08,570
This is how we want to start with a so-called paramagnetic where total magnetization is zero because

242
00:19:08,570 --> 00:19:14,360
we have all of these small moments and just random orientations, and so everything will cancel out

243
00:19:14,360 --> 00:19:14,900
in the end.

244
00:19:15,650 --> 00:19:19,640
This is how a magnet behaves at high temperatures.

245
00:19:20,160 --> 00:19:22,280
Everything becomes totally random.

246
00:19:23,270 --> 00:19:29,120
And in the next lecture, we will bring some order into this randomness by considering the energy and

247
00:19:29,120 --> 00:19:31,280
then by running the Monte Carlo algorithm.

248
00:19:32,300 --> 00:19:33,740
So I hope you are still excited.

249
00:19:34,280 --> 00:19:39,200
This wasn't a lot of work, but now we have our starting configuration, and let's go right into the

250
00:19:39,200 --> 00:19:39,920
next lecture.

