Some missing information in the oblig

As Finn mentioned in today’s lecture, there has been some information missing from the oblig:

The side walls in the well are made of conducting material.

Some of you have already been able to deduce this from that V=0 on the side walls tells us they are in equipotential, and knowing that conductors are equipotentials, the walls are most likely conductors. However, it should have been explicitly stated that they are conducting, since this does not always have to be the case.

Good luck with the rest of the oblig, and remember to attend to the group sessions if you have any questions. If you don’t have the time to attend, you can of course contact us by e-mail or here on the web pages.

SyntaxError: Non-ASCII character

There are probably going to be a few things to watch out for in the oblig. One of these is characters not being copied correctly from the source code in the note on Jacobi’s method. A common error that Python will spew out in this case is

SyntaxError: Non-ASCII character '\xe2' in file ... on line 40, 
but no decoding declared ...

This is caused by the apostrophe ‘ being incorrectly replaced as a quotation mark ` during copying. The difference is very subtle, but for Python there is a huge difference; The apostrophe is completely legal to indicate a text string. The quotation mark is not.

The solution is to replace the offending quotation marks with apostrophes (the latter is found on the *-button on Norwegian keyboards) or to simply copy the source code manually instead.

Also, do watch out for whitespaces sneaking into your source code while copying.

Sadly, there is often many errors like these that show up when copying source code from PDF’s. Make sure you verify that the source code is the same after copying.

The oblig is here

The oblig has been posted on the course pages.

You can find it here under “Obligatoriske oppgaver”.

Please note that last weeks exercises included an oblig warm-up. This can be useful if you want to test yourself with a simpler problem before going ahead with the whole oblig.

In addition, you’ll find the note about Jacobi’s method on this page, under the link named “Numerical solutions of Laplace’s equation”.

You’ll find the due date in the oblig itself.

Good luck!

Using Mayavi’s contour3d function

The Mayavi library that we’re using in this years course holds a great amount of functions to plot 3D and 2D data. Among these is the contour3d function that is very useful to plot equipotential surfaces in 3D space. This could for instance be used to plot a potential. In the exercise set from week 3, you may use this to visualize the potential inside and outside a uniformly charged sphere.

The solution program for this exercise is shown below:

from numpy import *
from enthought.mayavi.mlab import *

x,y,z = mgrid[-100:101:5., -100:101:5., -100:101:5.]
R = 40
Q = 1.0

r = sqrt(x**2 + y**2 + z**2)
V = 0*x
for i in range(len(r)):
    for j in range(len(r)):
        for k in range(len(r)):
            if r[i][j][k] < R:
                V[i][j][k] = Q / (8 * pi * R) * (3 - r[i][j][k]**2/R**2)
            else:
                V[i][j][k] = Q / (4 * pi * r[i][j][k])

contour3d(x, y, z, V, contours=20, opacity=0.5)

Note that we are setting contours=20 to have 20 equipotential surfaces and opacity=0.5 to be able to see through the surfaces. Otherwise we would only be able to see the outermost surface. You can read more about these and other settings by typing “help contour3d” in IPython (after importing mayavi) or by looking at the online reference.

The result is shown in the figure below:

A close up of the potential from a uniformly charged sphere. The rings are spherical shells coloured according to the level of the potential. They are semi-transparent, so that you can see the inner shells (the orange is the innermost shell.)

Do you see how the potential rises and lowers quickly close to the edge of the sphere? The edge of the sphere is located approximately where the color goes from yellow-ish to green. What does this tell you about the field in this area?

Calculating the gradient in Python

The NumPy package in Python serves us with a huge selection of useful functions for working with all kinds of numerical problems. One such function is gradient(V), that takes in a mgrid based matrix V and returns three mgrid matrices for each component of the gradient.

For instance, if you have the electric potential as a function of x, y and z, you can use NumPy’s gradient function to find the electric field through the relation \mathbf E = - \nabla V. An example of this is shown below:

from numpy import *
from enthought.mayavi.mlab import *

x,y,z = mgrid[-100:101:25., -100:101:25., -100:101:25.]

V = 2*x**2 + 3*y**2 - 4*z # just a random function for the potential

Ex,Ey,Ez = gradient(V)
Ex = - Ex
Ey = - Ey
Ez = - Ez

quiver3d(x,y,z,Ex,Ey,Ez) # plot the electric field just for fun

Note that this potential is random and not necessarily something you would find in a real electromagnetic problem. The resulting electric field is shown below:

The field generated from using NumPy's gradient function on our random potential.

 

Lecture on Thursday September 1

The «plenumsregning» on Thursday September the 1st will be replaced by a lecture on Maxwell’s first equation, electric work and energy, as well as vector fields such as the gradient of electric potential.

On the next «plenumsregning» we will continue to go through vector analysis, moving on with flux and divergence. Due to the 200 year anniversary here at UiO we will need to reorganize some rooms. Because of this, we will announce the exact date for the «plenumsregning» as soon as we know where and when it will be held.

Animating plots and waves in Python using matplotlib

matplotlib is an amazing framework to do visual plots in Python. It compares well with GnuPlot and beats Matlab’s plotting abilities by having more features. Although it does lack some 3D support, you may simply choose a different framework for 3D plots thanks to Python’s flexibility . In which case I would recommend Mayavi as a superb 3D plotting engine which I have written about before.

But now, let’s have a look at matplotlib’s animation capabilities. The script below shows a very easy approach to animation in matplotlib. This results in an animation of the standing wave shown here:

The script is as follows:
Continue reading

Using binary data instead of text files in Python

It’s been a while since we’ve posted anything here. Sorry about that. It has just been too much to do lately to find the time to write new posts.

This post is also arriving a bit late to be useful for anyone this semester, but what it shows might be useful for others. Or maybe for someone taking the FYS1120 course at a later time. So I’ll post it here still.

In mandatory exercise 2 we needed to load a huge file containing data from an AM radio signal. This was available a Matlab file and as a text file with all the values on each row. Those of us using Python realized quite quickly that most of the time spent on this exercise went to load the data before performing any computations on the data.

However, in proper hindsight, I thought it might have been a better idea to save the file as binary data instead of as a text file. After all, binary data saves space and usually is a bit quicker to load.

Doing this in NumPy is extremely simple. Just load the text file data and save it back in NumPy’s binary format:

Continue reading