C200留学生讲解、Computer Science辅导、讲解Python程序、Python语言辅导 辅导留学生 Statistics统计、回归

- 首页 >> Database
C200 Programming Assignment № 10
Computer Science
School of Informatics, Computing, and Engineering
Indiana University, Bloomington, IN, USA
April 5, 2019
Contents
Introduction 1
Problem 1: Random Walk 3
Starting Code for Problem One . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Using 100000 Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Random Output for Problem 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Problem 2: Making Maps 7
Starting Code for Problem 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Problem 3: It’s Not Real Butter 8
Geometry of Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Starting Code to Problem 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Output to Problem 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Assignment №10 Maps and Matrices and Correlation Page 1Introduction
In this homework, you’ll work on translating critical thinking to programming. This homework is
a bit less intense than the last one to give you a little breather! :)
Add a new folder to your C200 folder named Assignment10. In this folder, you will have:
– randomwalk.py
– mymap.py and hellobloomington.html
– complex.py
Make sure and commit your project and modules by 11pm, April 10th 2019.
As always, all the work should be your own. You will submit your work by committing your code
to your GitHub repository. You will not turn anything in on canvas. If your timestamp is 11:01P
or greater, the homework cannot be graded. So do not wait until 10:58P to commit your code.
Assignment №10 Maps and Matrices and Correlation Page 2Problem 1: Random Walk
A random walk is a stochastic process. A stochastic process is a series of values that are
not determined functionally, but probabilistically. The random walk is supposed to describe an
inebriated person who, starting from the bar, intends to walk home, but because of intoxication
instead randomly takes single steps either forward or backward, left or right. The person has
no memory of any steps taken, so theoretically, the person shouldn’t move too far from where
he or she starts. Random walks are used to model many phenomena, like the size of the web
or changes in financial instruments. We will model a 2D random walk with two arrays x and y
where x represents moving left or right and y represents forward or backward. The index i will
represent the step and x[i],y[i] will represent the location at step i. So, for i=0, we have x[0],y[0]
(starting place). Using random we choose from the list [1,2,3,4]. If the value is one then we
move right from the previous x position:
1 x[i] = x[i-1] + 1
2 y[i] = y[i-1]
If the value is two, then we move left from the previous x position:
1 x[i] = x[i-1] - 1
2 y[i] = y[i-1]
If the value is three, we move up from the previous y position:
1 x[i] = x[i-1]
2 y[i] = y[i-1] + 1
And when the value is four, we move down from the previous y position:
1 x[i] = x[i-1]
2 y[i] = y[i-1] - 1
Here is another way to describe this:
step(0) = 0 (1)
left step(n 1)
right step(n 1)
up step(n 1)
down step(n 1)
(2)
Assignment №10 Maps and Matrices and Correlation Page 3Figure 1: The first few steps of a random walk.
1 import numpy as np
2 import matplotlib.pyplot as plt
3 import random as rn
4
5
6 #translates a random int into a step along the random walk
7 #parameters: int i for the step index, numpy array x for tracking the left←-
/right location at index i,
8 #numpy array y for tracking the forward/backward location at index i
9 #return: none
10 def step(x,y,i):
11 direction = rn.randint(1,4)
12 #TO DO: implement this function
13
14
15 def graphit(x,y,n):
16 plt.title("Random {0} Walk\nLast Location {1},{2}".format(n,int(x[n←-
-1]),int(y[n-1])) )
17 plt.plot(x,y)
18 plt.plot([x[1],x[1]],[y[1]-10,y[1]+10], "b-")
19 plt.plot([x[1]-10,x[1]+10],[y[1],y[1]], "b-")
Assignment №10 Maps and Matrices and Correlation Page 420 plt.plot([x[n-1]-10,x[n-1]+10],[y[n-1],y[n-1]], "r-")
21 plt.plot([x[n-1],x[n-1]],[y[n-1]-10,y[n-1]+10], "r-")
22 plt.savefig("rand_walk"+str(n)+".png",bbox_inches="tight",dpi=600)
23 plt.show()
24
25
26 n = int(input("Number of steps: "))
27
28 x = np.zeros(n)
29 y = np.zeros(n)
30
31 for i in range(1,n):
32 step(x,y,i)
33
34 graphit(x,y,n)
Session Output
Number of Steps: 100000
Figure 2: The blue cross is where we started and the red cross is where we ended.
Assignment №10 Maps and Matrices and Correlation Page 5Programming Problem 1: Random Walk
Complete the step function.
Experiment with different numbers of steps. Do you see any number of steps that
seems to always move significantly away from the start?
Put your code into a new module named randomwalk.py
Assignment №10 Maps and Matrices and Correlation Page 6Problem 2: Maps
In this problem, you’ll use gmplot to make a map with markers for points of interest. The following
code centers the map at Showalter fountain (approximately). It then places two markers down,
one at Lindley Hall and one at Luddy Hall.
mymap.py
1 import gmplot
2
3 #create map Showalter Location
4 gmap = gmplot.GoogleMapPlotter(39.168451, -86.51891,15)
5
6 #Lindley Hall where we have C200
7 l1 = (39.165341,-86.523588)
8 #Luddy Hall the new SICE building
9 l2 = (39.172725,-86.523295)
10
11 #Indiana University -- Musical Arts Center
12
13
14 #list of points
15 lats = [l1[0],l2[0]]
16 lons = [l1[1],l2[1]]
17
18 #add points to map
19 gmap.scatter(lats, lons, ’red’, size=30, marker=False)
20 #add line
21 gmap.plot(lats,lons,’cornflowerblue’, size=30, marker=False)
22 #save to map
23 gmap.draw("Assignment10/hellobloomington.html")
Change
Install gmplot with pip.
Using the internet’s awesome power, find the longitude and latitude of IU’s Musical
Arts Center (where concerts are performed). Add the location to the code similar to
Lindley Hall and Luddy Hall and then add that point to the list of points and plot it.
Save the file into hellobloomington.html.
Put your code in a new module named mymap.py
Assignment №10 Maps and Matrices and Correlation Page 7Problem 3: Imaginary Numbers
In class we discussed how complex numbers are a pair of real numbers, a, b ∈ R and i is the
solution to i
2 = 1. We called a the real part and b the imaginary part. We found that Python has
a complex class too, but engineers chose j for i. In this problem, we’re extending our complex
number class to include a few more operations. Subtraction is straightforward. Division is a little
strange, and we’ll just provide the solution:
Modulus is a property of a complex number that describes its magnitude, i.e., how large it
is. We write modulus of a complex number a + bi as |a + bi|. The formula is simple:
|a + bi| =2 + b2 (7)
Look at Figure 3. The abscissa is the real part of the complex number, and the ordinate axis is
the imaginary part. By changing the sign of the imaginary part, we’re reflecting as shown in the
figure’s dotted line. In fact, we can calculate both ρ and θ directly from a + bi:
(9)
As an example, x = 1 + i. Let’s find the polar representation (ρ, θ).
are in radians (so is Python by default). To convert to degrees we multiply by 180
πor simply
use a conversion function. You’ll see this in the interactive Sessions I, II. Observe that a call to
the the abs( ) function returns the modulus and also that we can get the polar representation by
using cmath.
Assignment №10 Maps and Matrices and Correlation Page 8Figure 3: Complex Numbers as geometry. If we treat θ as time, any point on the line occurs at
the same time or what engineers call phase.
Session I with Complex Numbers
>>> x1 = complex(3,-1)
>>> x2 = complex(1,4)
>>> x1 + x2
(4+3j)
>>> x1*x2
(7+11j)
>>> x3 = complex(-3,1)
>>> x4 = complex(2,-4)
>>> x3+x4
(-1-3j)
>>> x3*x4
(-2+14j)
>>> x5 = complex(-2,1)
>>> x6 = complex(1,2)
>>> x5/x6
1j
>>> x7 = complex(0,3)
>>> x8 = complex(-1,-1)
>>> x7/x8
(-1.5-1.5j)
Assignment №10 Maps and Matrices and Correlation Page 9Session II with Complex Numbers
>>> x9 = complex(1,-1)
>>> abs(x9)
1.4142135623730951
>>> x10 = complex(4,-3)
>>> abs(x10)
5.0
>>> x11 = complex(3,2)
>>> x11*x11.conjugate()
(13+0j)
>>> x12 = complex(1,1)
>>> import cmath
>>> cmath.polar(x12)
(1.4142135623730951, 0.7853981633974483)
>>> import math
>>> math.degrees(cmath.polar(x12)[1])
45.0
My Complex Numbers
1 import math
2
3 class MyComplexNumber:
4 def __init__(self, rpart, ipart):
5 self.rpart = rpart
6 self.ipart = ipart
7 self.cn = [self.rpart, self.ipart]
8
9 def get_real(self):
10 return self.rpart
11
12 def get_imag(self):
13 return self.ipart
14
15 def __str__(self):
16 return str(self.cn)
17
18 #adds two complex numbers
19 def add(self, ix):
20 real_part = self.get_real() + ix.get_real()
21 imag_part = self.get_imag() + ix.get_imag()
22 iy = MyComplexNumber(real_part, imag_part)
23 return iy
24
Assignment №10 Maps and Matrices and Correlation Page 1025 #extends the ’+’ operator to add two complex numbers
26 #what’s the different between __add__(self,ix) and add(self,ix)?
27 def __add__(self,ix):
28 real_part = self.get_real() + ix.get_real()
29 imag_part = self.get_imag() + ix.get_imag()
30 iy = MyComplexNumber(real_part, imag_part)
31 return iy
32
33 #extends the ’-’ operator to subtract two complex numbers
34 #parameters: MyComplexNumber self, MyComplexNumber ix to subtract from←-
the MyComplexNumber self
35 #return: a new MyComplexNumber containing the result
36 def __sub__(self,ix):
37 #TO DO: implement this function
38
39 #extends the ’/’ operator to divide two complex numbers
40 #parameters: MyComplexNumber self, MyComplexNumber ix to divide into ←-
the MyComplexNumber self
41 #return: a new MyComplexNumber containing the result
42 def __truediv__(self,other):
43 #TO DO: implement this function
44
45 def __mul__(self,other):
46 real_part = self.get_real()*other.get_real() - self.get_imag()*←-
other.get_imag()
47 imag_part = self.get_real()*other.get_imag() + self.get_imag()*←-
other.get_real()
48 iy = MyComplexNumber(real_part, imag_part)
49 return iy
50
51 #calculates the modulus of the self MyComplexNumber
52 #parameters: MyComplexNumber self
53 #return: the value of the modulus
54 def modulus(self):
55 #TO DO: implement this function
56
57 #converts the self MyComplexNumber to polar representation
58 #parameters: MyComplexNumber self
59 #return: a tuple (rho, theta) as defined in the text, but where theta ←-
is in degrees not radians
60 def polar(self):
61 #TO DO: implement this function
62
63
64 #TEST CASES
65 x1 = MyComplexNumber(3,-1) #3-i
66 x2 = MyComplexNumber(1,4) #1+4i
Assignment №10 Maps and Matrices and Correlation Page 1167 x3 = MyComplexNumber(-3,1) #-3+i
68 x4 = MyComplexNumber(2,-4) #2-4i
69 x5 = MyComplexNumber(-2,1)
70 x6 = MyComplexNumber(1,2)
71 x7 = MyComplexNumber(0,3)
72 x8 = MyComplexNumber(-1,-1)
73 x9 = MyComplexNumber(1,-1)
74 x10 = MyComplexNumber(4,-3)
75 x11 = MyComplexNumber(3,2)
76 x12 = MyComplexNumber(1,1)
77
78
79 print(x1 + x2)
80 print(x1 * x2)
81 print(x3 + x4)
82 print(x3 * x4)
83 print()
84 print(x5/x6)
85 print(x7/x8)
86 print(x9.modulus())
87 print(x10.modulus())
88 print()
89 print(x12.polar())
Session Output
[4, 3]
[7, 11]
[-1, -3]
[-2, 14]
[0.0, 1.0]
[-1.5, -1.5]
1.4142135623730951
5.0
(1.4142135623730951, 45.0)
Assignment №10 Maps and Matrices and Correlation Page 12Deliverable for Problem 3: Complex Numbers
Complete Sessions I and II. The output of the sessions are the same output as your
class!
The file complex.py has the starting code for this problem.
Complete the TO DO’s to finish the implementation of the MyComplexNumber class.
assignment №10 Maps and Matrices and Correlation Page 13

站长地图