Python代写 - Phys 129L: Homework
时间:2020-11-24
Instructions: name the files that should for the rpograms
ex1 hw7 permnum.py, ex2 hw7 permnum.py where your permnumber is attached to the each of the files, and make a single directory for them called hw7 permnum . After doing that, run
the command tar -cvf hw7 permnumber.tar hw7 permnum/ .
This command archives all the homework files into a single file
that can be turned into Gaucho space, read online in google how
to use tar. Verify everything is in the file by first creating a test
directory called testdir and doing tar -xvf hw7 permnumber.tar
-C testdir . The last part dumps the contents into the directory testdir. Turn in instructions of how to use your code when
reading external files
1
1. Legendre polynomials: This is an exercise where you use the polynomial routines in numpy to generate the Legendre polynomials recursively. The Legendre polynomials are defined in the following way:
• The zeroth order polynomial is constant P0 = 1
• The n-th polynomial is of degree n • The n-th polynomial is orthogonal to all previously generated
polynomials in the interval [ 1, 1]
Z 11 Pn(x)Pm(x)dx = 0, for all m < n
• The polynomial is normalized so that Pn(1) = 1
• Following this rule one finds P1(x) = x
It is easy to see that if we take xPn(x), that is is orthogonal to all
Pm with m < n 1, but not to Pn 1. This motivates the following
recursion relation
Pn+1(x) = Nn+1(xPn(x) + αnPn 1(x))
This new polynomial is still orthogonal to all those that appear below
n 1. By adjusting αn it can be made orthogonal to Pn 1. This is done
by integrating P˜n+1(x) = (xPn(x) + αnPn 1(x))) ∗Pn 1(x), and solving
for αn. Nn+1 is obtained by evaluating P˜n+1(1) and normalizing Pn+1
correctly from there.
In principle there could have also been a term linear in Pn. However,
these do not appear because the even Legendre polynomials are even
functions of x and the odd ones are odd. This way xPn(x) is already
orthogonal to Pn(x) (even versus odd functions).
(a) Write a program that recursively generates the Pn from the recursion relation above.
(b) To do this, compute αn by computing the norm R 11 Pn 1(x)Pn 1(x)
and the overlap R 11 xPn(x)Pn 1(x)dx. This is done most easily using the integration function for polynomials that is available for
poly1d objects and evaluating that.
2
(c) Having both the overlap and the norm, the integral of xPn(x) +
αnPn 1(x) with Pn 1(x) must vanish. This gives you a linear
equation to solve for αn.
(d) Then evaluate Nn+1 by evaluating xPn(x) + αPn 1(x) at x = 1.
(e) The program should ask for a maximum value of n to generate Pn
for.
(f) The program should print all the polynomials up to Pn+1(x).
(g) The program should plot the first seven Pn(x) polynomials and the
last one (This is to keep the plot tidy without too many superposed
graphs).
2. Magnetic wires: Modify the 2D-electrostatic code shown in class
to take in wires with current that produce magnetic fields (you need
to modify the vectors so that they circulate around the loops with
the correct right hand rule). In this case there is no need to have a
magnetostatic potential shown.
Ask that the current of the wire be used (instead of the charge).
Plot the streamplot and the quiver plot for two wires carrying the
same amount of current in the same direction, and in the the opposite
direction. Make sure that the plots are saved to a file from the program
itself.