numbers – Elektromagnetisme http://elektromagnetisme.no The home of FYS1120 Mon, 20 Oct 2014 11:23:54 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.3 28429679 The beauty of Mayavi http://elektromagnetisme.no/2010/10/07/the-beauty-of-mayavi/ http://elektromagnetisme.no/2010/10/07/the-beauty-of-mayavi/#comments Thu, 07 Oct 2010 07:07:53 +0000 http://mindseye.no/?p=214 Continue reading ]]>

Four charges with different magnitude plotted in 3D using Mayavi

In one of my earlier posts about Mayavi, I wrote about how you could visualize 2D field line plots using the flow function. At the end of that post I added that Mayavi is actually best at 3D plotting, and to follow up on that I’ll show you some of these plots with a few example Python scripts you might try out on your own.

First of all, you might want to know how to install Mayavi. For those lucky ones of you who have freed yourself and jumped on the Linux bandwagon, installing Mayavi should be quite easy. If you are using Ubuntu in particular, you may just install the package mayavi2 using either Synaptic or apt-get. If you are on Windows or Mac, you may either install Enthought’s own Python distribution (EPD) or give a shot at compiling on your own. Just note that EPD is quite expensive, even though all its components are open source, but if you are a student or academic user you could go ahead and download the academic version for free. It is basically the same as the commercial one, but with an academic license. (Kudos to Enthought for both making Mayavi open source, building an business model around it and still providing a great solution for students!)

Now, Enter 3D!

The way you do your plots in Mayavi depends on what you want to express. Most likely, you would prefer to show some simple plots giving just the necessary amount of information to tell you how the electric field behaves around your charges. A simple example of this is shown below:

Four charges in a Mayavi plot

The simple plots often give you a great perspective about what happens in the electric field

On the other hand, you might want to give a strong visualization to show off the density and beauty of an electric field. In such a case, increasing the resolution of the flow/streamline seeds gives you a greater amount of field lines, which could result in plots like this:

An highger seed resolution gives a more dense plot.

Note that the plot shows the same four charges as above, but from a different angle and, of course, with more field lines.

The greatest part of using Mayavi to visualize these plots, however, comes from the fact that you may rotate the plot in real time in the Mayavi scene view. This gives you great control and insight of what you are plotting as you may rotate it as if it was a physical object in front of you. To show how interesting this may be, I’ve created a short video rotating around the same plot as above, recorded using Mayavi’s animation features:

You may even animate the charges individually, showing you what happens when a charge moves through space.

Note that the video below shows some artifacts around the charges. I believe I could have tweaked the settings a bit more to avoid these, but I decided it was good enough for the purpose of showing Mayavi’s capabilities.

If you would like to test out these plots on your own, you can download the source code here:

And if you couldn’t get enough of those field line plots, here are all the above and a some more, stacked in one set:

Four charges in a Mayavi plot High resolution plot of four charges in a square Low resultion plot of four charges perfectly aligned in a square Using planes and spheres together as seeds yields more interesting results. A medium resolution dipole plot with spheres as seeds. Using a plane as a flow/streamline seed between the two charges in a dipole ]]>
http://elektromagnetisme.no/2010/10/07/the-beauty-of-mayavi/feed/ 7 214
Sweet first proof http://elektromagnetisme.no/2010/09/09/sweet-first-proof/ http://elektromagnetisme.no/2010/09/09/sweet-first-proof/#respond Thu, 09 Sep 2010 20:28:12 +0000 http://mindseye.no/?p=33 Continue reading ]]>

Carl Friedrich Gauss (1777 – 1855) by G. Biermann (1824-1908)

Earlier today, enjoying a warm cup of coffee with a friend of mine,we got into a discussion about some math and ended up contemplating on how to prove the sum formula for the n first natural numbers.

The proof is rumored to first have been done by Gauss when he was only a child.

Let S_n = 1 + 2 + 3 + . . . + (n-2) + (n-1) + n

just rewriting the terms backwards we get

S_n = n + (n-1) + (n-2) + \ldots + 3 + 2 +1

now adding these two expressions for the sum we obtain

2 S_n = (n+1) + (n+1) + (n+1) + \ldots+ (n+1) + (n+1) + (n+1)

and since we had n terms in the original sum we now have n \cdot (n+1)‘s, so

2 S_n = (n+1) + (n+1) + (n+1) + \ldots+ (n+1) + (n+1) + (n+1) = n(n+1)

so..

S_n = \frac{ n(n+1) }{2}

Neat. On a computer this would severely reduce the number of operations that would have to be done to compute such a sum. Imagine having to sum up the first million natural numbers and let’s suppose the computer requires one operation for adding, multiplying, dividing and so forth. Then implementing this formula would reduce the numbers of operations from 10^6 to 3.

]]>
http://elektromagnetisme.no/2010/09/09/sweet-first-proof/feed/ 0 33