Uploaded by 360nac

PHYS220

advertisement
https://i.ytimg.com/vi/PGDvjG49kLc/maxresdefault.jpg
PHYS 220: Computational Methods
Dr. Hunter Sims
Lecture 3
Homework 1
Progress due Thursday September 1 at 8:30 AM
Due Friday September 2 at 11:59 PM
Upload your .py file to Blackboard (you will have 2
“attempts”, with the first being your progress)
8/30/2022
PHYS 220, FALL 2022
2
What should go in your self assessment?
Self assessment
• Text cell at the end of submission. At least 50 words.
• What kept you from sticking to your schedule?
• What did you have trouble with?
• What would you have done differently?
• Was any part of the assignment unclear? Interesting?
Challenging?
Do this! Even if you don’t complete the assignment!
Doing the outline and the self-assessment = 1 nearly free
points (out of 5, 20%)
8/30/2022
PHYS 220, FALL 2022
3
Lists and arrays
Numpy arrays
• Fixed data type (int, float, complex)
• Fixed size (sort of)
• Can operate on like numbers/variables
Example.
x = np.array([1,2,3]) stores the array [1,2,3] in x
y = np.zeros(5) stores the array [0,0,0,0,0] in y
8/30/2022
PHYS 220, FALL 2022
4
Doing math with arrays
Suppose x = np.array([0,1,2,3,4,5])
What is 2*x?
What is x+2?
What is x**2?
8/30/2022
PHYS 220, FALL 2022
5
Fun with indices
Suppose x = np.array([0,1,2,3,4,5])
x[2] is 2
x[-1] is 5
Array slicing:
x[:2] is [0,1]
x[2:] is [2,3,4,5]
8/30/2022
PHYS 220, FALL 2022
6
Python built-in: range
range(N) provides a list containing integers 0, 𝑁
range(N,M) provides a list containing integers [𝑁, 𝑀)
range(N,M,s) provides a list containing integers [𝑁, 𝑀),
counting by 𝑠
Examples.
x = range(10)
print(x[2])
Output: 2
x = range(4,10)
print(x[2])
Output: 6
x = range(0,10,2)
print(x[2])
Output: 4
x = range(10,-1,-1)
print(x[9])
Output: 0
8/30/2022
PHYS 220, FALL 2022
7
numpy.arange and numpy.linspace
numpy.arange works just like range, but for numpy arrays.
np.arange(start,end,increment) creates a numpy
array on the domain [start,end) counting by increment
Examples.
x = np.arange(0,11,1) stores the numpy array
[0,1,2,3,4,5,6,7,8,9,10] in x
x = np.arange(0,1,0.1) stores the numpy array
[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9] in x
8/30/2022
PHYS 220, FALL 2022
8
numpy.linspace is similar
np.linspace (start,end,number_of_divisions)
creates a numpy array on the domain [start,end]
Examples.
What does np.linspace(1,1,1) do?
What does np.linspace(0,1,10) do?
How can you get the array
[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0] using
np.linspace?
8/30/2022
PHYS 220, FALL 2022
9
Matplotlib
Our second-most-used module
Contains functions and keywords for plotting in 2D and
quasi-3D.
For plotting, we will be using matplotlib.pyplot
import matplotlib.pyplot as plt
8/30/2022
PHYS 220, FALL 2022
10
Plotting smooth data
plt.plot(x,y)
• Creates a line plot
• x is list/array with x values
• y is list/array with y values
Example.
You want to plot distance vs time. You have a distance array
and a time array.
plt.plot(time,distance)
plt.show()
8/30/2022
plt.show() outputs the plot to the
screen/notebook and (internally)
clears the plot
PHYS 220, FALL 2022
11
Plotting discrete data points
plt.scatter(x,y)
• Creates a scatter plot
• x is list/array with x values
• y is list/array with y values
Example.
You want to plot distance vs time. You have a distance array
and a time array.
plt.scatter(time,distance)
plt.show()
8/30/2022
PHYS 220, FALL 2022
12
Adding information to your plot
Adding a key/legend
• plt.legend()
• Put this after your plot statements
• Optional argument loc=‘’ gives location of key
• Use plt.plot(x,y,label=“Key label”)
Example.
You want to plot distance vs time. You have 2 distance arrays and
one time array.
plt.plot(time,distance0,label=‘5.0 m/s’)
plt.plot(time,distance1,label=‘10.0 m/s’)
???
plt.show()
8/30/2022
PHYS 220, FALL 2022
13
Adding information to your plot
Adding axis labels
• plt.xlabel(), plt.ylabel()
• Include both the physical quantity and the units
plt.plot(time,distance0,label=‘5.0 m/s’)
plt.plot(time,distance1,label=‘10.0 m/s’)
???
???
plt.legend()
plt.show()
8/30/2022
PHYS 220, FALL 2022
14
Adding information to your plot
Adding a title
• plt.title()
• Let the reader know what they’re looking at
plt.plot(time,distance0,label=‘5.0 m/s’)
plt.plot(time,distance1,label=‘10.0 m/s’)
???
???
???
???
plt.show()
8/30/2022
PHYS 220, FALL 2022
15
Adding information to your plot
Adding axes to plot
• plt.axhline(0), plt.axvline(0)
• Argument can be any value (but 0 gives x and y axes)
plt.plot(time,distance0,label=‘5.0 m/s’)
plt.plot(time,distance1,label=‘10.0 m/s’)
???
???
???
???
???
???
plt.show()
8/30/2022
PHYS 220, FALL 2022
16
Customizing the plot
Changing the x and y range plotted
• plt.xlim(a,b), plt.ylim(c,d)
• Restricts plotted region
plt.plot(time,distance0,label=‘5.0 m/s’)
plt.plot(time,distance1,label=‘10.0 m/s’)
???
???
plt.show()
8/30/2022
PHYS 220, FALL 2022
17
Customizing the plot
Adding additional labels within the plot
• plt.text(x_pos, y_pos, “text”)
• Adds a label “text” to the plot at (x_pos, y_pos) in terms of
the plot coordinate system.
plt.plot(time,distance0,label=‘5.0 m/s’)
plt.plot(time,distance1,label=‘10.0 m/s’)
???
plt.show()
8/30/2022
PHYS 220, FALL 2022
18
Download