So let's get started with our physical example, where we use a Monte-Carlo algorithm to solve a very,
very difficult problem.
So here we are considering magnets and you probably know a ferromagnetic, for example, a piece of
iron and it's magnetic.
It attracts some other materials and probably also know that inside of this material, you have small
atoms.
And basically, you kind of consider these as small individual magnets and these magnets they can orient
with respect to each other and, for example, the fairer magnets they will all be pointing along the
same direction.
So all the small magnets will be parallel, and therefore they will form this large magnet.
And then there are other forms of magnetism.
For example, you could have just randomly oriented individual magnets and then overall, the magnetism
of the whole sample would cancel out.
And this is something we want to simulate in the following.
So you can already imagine that this is very, very difficult to simulate because in the real material,
we have so many atoms and so many individual magnets.
And all of these magnets can be oriented along different orientations, so they have X, Y and Z components.
So we have so many variables that we have to investigate, and what we are searching for is the configuration
with the lowest and energy.
And it turns out, since we have so many variables, we cannot straightforwardly calculate the state
with the lowest energy, and this is where the Monte Carlo algorithm comes into play.
We will basically randomly change the orientation of individual magnets and see if this decreases the
energy or not.
And if it decreases the energy, then that's good.
And if it increases the energy, then we basically say, OK, the step.
The random reorientation does not count.
So that's the whole idea.
We use randomness and sometimes it will decrease the energy.
It's good and will it increases the energy.
We will just disregard this change of the configuration.
That's the whole idea here.
So I will, of course, go into the more detail in the following.
But let us start with generating our actual set up, our starting configuration.
So we will set up a grids, an array of positions and that each of these positions, there will be a
magnetic moment and then we will plot this configuration.
And then I can explain to you much better what we will do afterwards.
But you can see already here in the notebook, we will then consider the energy, which I'm going to
explain to you soon.
And then we will run the Monte Carlo, which is basically a loop of a so-called metropolis steps, which
I have also already explained to you very briefly.
But we will get into more detail later on.
For now, let's generate the starting configuration.
So as I said, we have a array of magnetic moments and they sit at different positions and altogether,
this array of magnetic moment forms.
Our thorough magnets or our magnet in general.
And you see we can give each of these magnets an index, for example, one to end, but we could also
arrange them in the grid and then each of these magnets would have two indices.
This is how we are actually going to do it in our program later on.
And what's important is that the length of each moment is fixed.
And in our case, we will say the length is fixed to one.
So this means every magnetic moment has a length of one, but the orientation is not fixed.
It is two.
It can be totally random.
It can basically show into any direction.
It can point along any direction on a sphere of a ball, basically.
So how do we do this?
How do we generate this or orientation of each of these moments randomly?
So we could say, OK, let's consider an X component, the Y component, the Z component, and let's
generate those and then divide it by the length of the vector.
This would be OK.
But actually, it wouldn't give a good representation of randomness, because then not all points or
not, all orientations of these vectors, I would be at equal proportions.
So it's much better to straightforwardly generate moments that already have a length of one, and we
can do this by using spherical coordinates.
What I've written down here.
So this means since the length is fixed, we have from these very quick.
Ordinates, the radius, which is fixed to one, and then we only have the polar angle fight and die
as a to angle fighter.
So in some textbooks, it's also labeled differently these angles here.
So what do we have to do then is we have to generate random angle fire in the range of zero two two
pi and a random angle theta in the range of zero to PI.
And this is what we will start with.
Let me create a cell here and let me start writing.
Well, first of all, we must say how many of these moments we have and this I call length.
So I say, let's start with 10.
And I want to consider a quadratic grids of moments.
So the total number of moments would then be length squared.
So 100 in our case, and now we need to generate these random angles.
And I start with random five and this would be a random number.
So ENPI dot, random thought round and the number of moments that we were, what a number of angles
that we are going to generate is numb.
So like this and peddled random dot random.
And then I said the range should be from zero to two pi.
So far, it's from zero to one, so we just have to multiply by two pipe.
And now we do a very similar thing for Theta, of course.
So I will copy this and write Theta here.
And Theta only goes from zero to pi and not to two pi.
So if we run this and look at the result, you will see, for example, four random fire we have now
an array of 100 moments or not moments yet, but only angles yet in the range of zero to two pi.
So no way will do something that is not really necessary at this point, but we will need it anyway
for the plotting later on.
So I will structure these angles here
to basically reshape the array.
So you will see in a second what this does.
So I will write here dot reshape and then I'll write length for the number of points in the X direction,
length for the number of points in the wider action.
And one for the number of points in the Z direction.
And I do the same thing also for the other one, I run it.
And you see, it's now the data as before.
It's, of course, a bit of a different values because I have rerun those random commands.
So the values differ now, of course, but we have now just reshaped a light array.
We have basically introduced some brackets.
And the reason is because now we can access certain elements here.
For example, we could write zero zero and then we would get the fire, maybe another zero here.
This would then be the randomly generated angle.
Phi for X equals zero.
Y equals zero, z equals zero.
And for example, we could also do like this so we can really address know the individual angles and
facilitate the same thing.
So now we generate the magnetization, so we don't need this one in here anymore, so we generate the
array of magnetic moments.
According to this equation.
So it is an array and pitot array, and now we must write to three components.
So it's pretty easy.
And to cosine and then random five times and p don't sign random theta.
It would be the X component or the component with the zero index.
Then for the Y component, we have a very similar thing we have here and P Dot sign random phi times.
And if you don't sign and theta and for the Z component, we have an odd cosine random data.
So I hope I didn't make a typo here.
Let's run.
It doesn't give an arrow.
That's good.
And now we can look at it once again.
This is now our magnetic magnetization, and we could now write, for example, or something like this.
This will now be the magnetic moment and X equal five y equal to Z equal to zero.
And it has X, Y and Z components.
And we can, of course, also address these components individually by writing here, for example,
one, then we will get the Y component.
OK.
So that's pretty good.
We have an.
Our magnetization area and we have structured it already.
So actually, I'm seeing I missed here the heading, so let me shift this up here.
This is how I want to have it.
OK.
And now we can do just one more thing that is not really necessary.
What I want to show you that everything worked just fine.
I want to check the norm.
And this we can do by writing magnetization.
Zero.
And then all the elements square plus magnetization one.
All the elements square.
So this basically means X component squared plus Y components squared plus C component square.
And then we can also calculate a square root and Peter's square root.
And you see, it gives as an array and a whole lot of number ones.
So this means the norm of every one of these moments is indeed one.
So, for example, we could now check once again here for X equals five y equal to the equals zero the
norm as one as for every other moment.
So this worked.
We have now these am eyes in this whole array and the length is fixed to one, and all of these moments
have been chosen randomly.
So now we will continue and create the position.
And this is really easy.
We write what we call this coordinates or codes, and it's supposed to be an array, and the array is
generated by using NP dot mesh grid.
So this is something we have done already several times for plots, for example, for contour plots.
And now we mash different ranges.
So and Pinchot arrange length basically is a list of numbers starting from zero going to a length minus
one.
So basically zero one two three four five six seven eight nine.
And then we do this also for the Y component and for the Z component, we only have a single value.
We could here also consider basically a three dimensional sample.
But for simplicity, I want to stick with a two dimensional sample.
So to see value only has an index, or at least that just contains a single value, which is zero.
OK, so now we have our list of coordinates and we have our list of magnetic moments.
And now we can use a plot that is called a quiver plot.
So let me show you how this works.
Of course, it's a three dimensional plot because our arms are three dimensional.
They have an X, Y and Z component so we can write our plots.
It's just a name for the plot fields.
He got axes and then projection is equal to 3D two x to enable the 3D object.
And now I will continue to just write R2 plots, dot quiver and then the syntax is as follows.
So we write first course zero, which basically.
Corresponds to the values for the for the values, so the X coordinates of our list coordinates.
Then the Y coordinates and here the Z coordinates, and then we do the same thing for the magnetization.
And here you see, I have constructed the magnetization in the way that we can use known in the same
syntax.
You could have done that.
We could have structured your magnetization array, of course, in a different way.
But I think in such a way, it's much easier to understand what's going on here.
So when I run this, I get some really strange stuff looks awful, but at least it worked.
We have now a plot.
Now we must fix it.
So what happened here?
I think the problem is that our arrows have a length of one and the X and Y values on the axes.
They are scaled from zero to 10, from zero to 10.
That is good.
This is really the data arrangement we have.
But since we only have a single value for the Z coordinates, which is zero, the plot is really zoomed
in.
However, the arrows can have a length of one also in the Z and the C direction.
So this means they are really out of range and this is what we're seeing here.
So I think we just must fix the D Z limit for the plots.
We can do this by writing our story a title arrow plot dot and then set underscore the z limb.
Then we specified a limit, and I think it makes sense to use minus 1.1.
And here we go.
It still doesn't look so nice, but at least now we can see the whole arrows.
So you see, it looks totally confusing here, and this is because we have randomly generated these
arrows.
So one more thing that I don't like is that the plot is actually distorted because this axis, this
axis and this one, they all have the same length and the plot.
But actually, this one has a length of 10 this often and this only of two.
So we should specify the ratio of this plot or the ratio of the box.
We can do this by writing arrow plots don't set on the school books aspect, and then we can write aspect
is equal to and then we specify the three values.
So I said the x axis has a length of length, and since it goes from those from zero to 10, we write
length and the y axis also.
And the z axis has a length of two, so I write two.
Now you see all the arrows have the same length.
I mean, it looks sometimes as of the arrow was rather short, but this is just due to the projection
due to the viewpoint.
So now we really have our array of of arrows.
So let me add some more options that I found to be useful here.
So I think we don't really need the gray background here, so I will make the ground invisible.
And this we can do by writing arrow plots, dot axes, falls.
So you see, now it's gone.
And just as a last thing, I will add another plot.
So there's no Akuma plot, and then I will add here another one, a coordinate plot where I just point
out where just plotted the points of the coordinates.
And we do this by writing Arrow plots, dots get a three v and this time we own the plot, the coordinates,
as I said, and for the color we can use, for example, red.
So you see, now we have this Gritz of the positions and you see at each position, there is a magnetic
moment indicated by this arrow and the arrow indicates the orientation of and so we see now this is
here of a whole magnet.
It consists of individual magnets and they have certain orientation.
So one more thing we can, of course, now increase the number of points.
For example, we could use 40 here, then we must rerun all of these cells.
So maybe looking around the whole notebook to make it faster.
And in this case, we run into the problem that the plot is just too small to see anything and to fix
this a nice way is to use the following code.
So this will change the standard size of all figures in his notebook.
You basically access some property RC firearms for your adult fake size and you specify the size.
And here you have to tune a bit what works best for you.
I think for me.
This one should work pretty well.
But if you have a different screen and a different zoom in your browser and maybe you have to tweak
these values.
But the aim is just to make the plot a bit larger.
And you see, now we have a really nice plot and everything looks totally confusing, but this is how
we want it.
This is how we want to start with a so-called paramagnetic where total magnetization is zero because
we have all of these small moments and just random orientations, and so everything will cancel out
in the end.
This is how a magnet behaves at high temperatures.
Everything becomes totally random.
And in the next lecture, we will bring some order into this randomness by considering the energy and
then by running the Monte Carlo algorithm.
So I hope you are still excited.
This wasn't a lot of work, but now we have our starting configuration, and let's go right into the
next lecture.