辅导案例-129L

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
Phys 129L: Homework 4, due Nov 2
Instructions: name the files that should for the rpograms
ex1 hw4 permnum.py, ex2 hw4 permnum.py where your permnum-
ber is attached to the each of the files, and make a single di-
rectory for them called hw4 permnum . After doing that, run
the command tar -cvf hw4 permnumber.tar hw4 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 hw4 permnumber.tar
-C testdir . The last part dumps the contents into the direc-
tory testdir.
1
1. Write a program that computes planetary orbits around a star. To
simplify the problem, solve the following set of differential equations:
∂2t ~x = −
~x
|x|3 (1)
where ~x is a vector in 2 dimensions. The program should ask for input
the two coordinates for ~x(0) and the velocity ~v(0) = ∂t~x(0), it should
also ask for the time interval δt and the total runtime ttot.
Use the Leapforg algorithm to solve the problem: first evolve the ve-
locity a half step in time and go from there. Have the program display
the following information
(a) Display the orbit on a plot. (Save one such plot to a file, indicate
the δt and initial conditions in the plot labels and turn it in.)
(b) Check that angular momentum is exactly conserved by the nu-
merics L = x1v2 − x2v1 (except rounding off error)
(c) Check how well conservation of energy works
E =
~v2
2
− 1/|x|
(d) Plot both of these (Angular momentum and energy) as a function
of time for the same data as the previous plot, and turn it in.
Additional note: In the initial conditions you want to have the ve-
locity |v| < 1/

|x| to make sure that the orbit closes, and δt ∼ (0.01 –
0.1)|x|/|v|. It is best if the velocity is mostly perpendicular to x (makes
a more rounded ellipse). Make sure that the orbit lasts long enough to
form a closed ellipse. (ttot ∼ 20|x|/|v|).
2. Card shuffling; This is an exercise for dictionaries. Write a program
that generates a shuffled deck of cards. The cards come in four suits:
(’clubs’, ’diamonds’, ’hearts’, ’spades’), and are ordered as (’A’, ’2’,
. . . ,’10’, ’J’,’Q’,’K’).
(a) Combine these into a deck (easiest way is to set up an array of
tuples where each tuple is an element of a suit followed with a
card type).
2
(b) Create a dictionary that pairs the numbers from (0, . . . , 51) with
the card list.
(c) Set up a second array that you fill with random cards chosen from
the dictionary. Use random.choice() from the random library to
do this. Make sure each card is only used once. (It is convenient
to use the dictionary.pop() method to do this).
(d) Display the shuffled deck on screen by writing each card as ” Q of
hearts”, ”3 of diamonds” etc.
(e) Ask user if they want another shuffle each time, or exit the pro-
gram.
3. Reverse polish notation calculator: Use the simple calculator code
discussed in class and provided in Gaucho space, and modify it in the
following way:
(a) Implement the following unary operations: log(x), log10(x), exp(x),√
x, 1/x, cosh(x), sinh(x), tanh(x),Γ(x) (for the Gamma function
call the numpy special function). Also, the complex conjugate,
inverse trigonometric functions and inverse hyperbolic functions
(arcsin, etc). Make sure to catch errors and do something about
them when the result fails so that the calculator does not break.
(b) Implement the following binary operations: xy, logy(x)
(c) Implement a way to get e, pi into the stack, by calling the constants
from numpy.
(d) You should also add the option of visualizing the full stack (similar
to “help”, “clear” and “quit”), to swap the last two element of the
stack and to copy the last entry of the stack.
The process is two step: you need to modify the dictionaries for
the types of functions, and you need to implement the functions
separately. Make sure that functions that are not part of the
dictionaries appear in the help call.
3

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468