PHYSICS 2235


PHYSICS 2235
Lab 7

Exercises
Assignment

tutorial.

This lab will focus creating, manipulating arrays using NumPy

Exercise 1.

In this exercise we will look at the multiple ways we can define an array.
#!  /usr/local/bin/python3
# How to define an array

import numpy as np

# first (array(N)) 
t = np.array([ 1., 4., -2., 7.])

# second (linspace(start, stop, N))
u = np.linspace(0, 10, 5)

#third (logspace(start, stop, N)) 
v = np.logspace(1,3,5)

#fourth (arange(start,stop,step))

w = np.arange(0, 10, 2)

#fifth (zeros(N), ones(N)) 

x = np.zeros(6)
y = np.ones(6) 

print(t) 
links explaning each line.
  • python3
  • numpy
  • first
  • second and third
  • fourth
  • fifth and sixth
  • why would I use an array?
  • Create an array in 1-degree increments of all the angles in degrees from 0 to 360 degrees.

    Exercise 2.

    In this exercise we will look at mathematical operations with arrays.
    #! /usr/bin/python3
    # How to define an array
    
    import numpy as np
    
    
    t = np.array([ 1.8, 3.2, 4.8, 5.5])
    
    
    u = t * 6 
    
    v = t**6 
    
    w = t + 6 
    
    y = np.sin(t)
    
    print(t) 
    
    
  • math operations
  • create an array z that is the natural logarithm of w.

    Exercise 3.

    Arrays can also be added, subtracted, multiplied, and divided by each other on an element-by-element basis.
    #! /usr/bin/python3
    # How to define an array
    
    import numpy as np
    
    a = np.array([34., -12, 5.])
    b = np.array([68., 5.0, 20.])
    
    
    c = a + b 
    
    e = a * b 
     
    
    print(a) 
    
    
  • arrays operations
  • create an array d and f. d is the a subtracted from b. f is a divided by b.

    Exercise 4.

    Slicing and inserting elements in an array.
    
    
    #! /usr/bin/python3
    # slicing an array
    
    import numpy as np
    
    a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
    
    
    b = a[1:4]
    
    c = a[2:]
    
    d = a[:-2]
    
    e = a[1:8:2]
    
    print(a[1:])
    
    
  • slice1
  • slice2
  • slice3
  • what are the elements returned by these slices:
  • a[0:2]
  • a[1:-1]
  • a[:-1:3]



    Assignment due next Monday by noon

    Problem 1

  • an array (rad) in 1-degree increments of all the angles in radiance from 0 to 360 degrees;
  • an array (arr1) from 12 to 17, not including 17, in 0.2 increments;
  • Create an array (arre) of 100 elements all equal to e.
  • Find the square of each element of the array (arr1). Find twice the value of each element of the array in two different ways: (i) (arradd) using addition and (ii)(arrmult) using multiplication.

    For each of the previous tasks print "the array xxxx is:" (array content).

    submit the code arrays.py

    Problem 2.

    A ball is dropped from a height of h = 10 m at time t = 0. You need to find the sequence of times when the ball passes each half meter from t =0. The position of the ball can be expressed as

    y = h - \frac{1}{2} g t^2.



    Your code should print out the y, t array as "the position array is:" (array content) "the time array is:" (array content) Submit your code in a file named ball.py.

    Hint: from the equation above solve for t.
    Hint: create an array y that goes from 10 to 0 in 0.5 m increments.
    Hint: at y=10 t=0. at y = 9.5 t= 0.319.

    Problem 3.

    The average velocity over an interval delta_t is defined as
    v_i = \frac{\Delta y}{ \Delta t}


    Find the average velocity array for each time interval of problem 2 using NumPy arrays.
    Hint: You can calculate the average velocity array entirely by slicing. Remember that:

    v_i = \frac{y_i- y_{i-1}}{ t_i - t_{i-1} }


    Hint: what does the slice y[1:2]-y[0:1] represent?
    Hint: can you generalize the above example to represent the full arrays for both
    \Delta y \Delta t

    Hint: the number of time intervals is one less than the number of times.
    Hint: the first element in the average velocity is -1.56.
    submit python code that prints the average velocity array in a file named vave.py.
    Back to main Physics 2235 page