Python Assignment1
Python Assignment1
Python Assignment1
1. A ball is thrown vertically up in the air from a height 0 above the ground at an initial
velocity 𝑣0 . Its subsequent height and velocity 𝑣 are given by the equations
1
= 0 + 𝑣0 𝑡 − 𝑔𝑡 2
2
𝑣 = 𝑣0 − 𝑔𝑡
where 𝑔 = 9.8 is the acceleration due to gravity in 𝑚/𝑠 2 . Write a script that finds the
height and velocity 𝑣 at a time 𝑡 after the ball is thrown. Start the code by setting 0 = 1.2
(meters) and 𝑣0 = 5.4 (m/s) and have your code print out the values of height and velocity.
Then use the script to find the height and velocity after 0.5 seconds. Then modify your script
to find them after 2.0 seconds.
3. Create an array of 9 evenly spaced numbers going from 0 to 29 (inclusive) and give it the
variable name 𝑟. Find the square of each element of the array (as simply as possible).
Find twice the value of each element of the array in two different ways:
(i) using addition and (ii) using multiplication.
4. The position of a ball at time 𝑡 dropped with zero initial velocity from a height 0 is
given by
𝑦 = 0 − 0.5𝑔𝑡 2
where 𝑔 = 9.8 𝑚/𝑠 2 . Suppose 0 = 10 𝑚. Find the sequence of times when the ball
passes each half meter assuming the ball is dropped at 𝑡 = 0. Hint: Create a NumPy
array for y that goes from 10 to 0 in increments of -0.5 using the arrange function.
Solving the above equation for t, show that
2 0 −𝑦
𝑡 = .
𝑔
(i) Using this equation and the array you created, find the sequence of times when the
ball passes each half meter.
Δ𝑦
(ii) Recalling that the average velocity over an interval Δ𝑡 is defined as 𝑣 = , find the
Δ𝑡
average velocity for each time interval in the problem using NumPy arrays.
5. Write a program to tell the nature of the roots and values of the roots of a quadratic
equation 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0, 𝑎 ≠ 0.
6. Write a program to calculate the factorial of a positive integer input by the user.
(a) Write the factorial function using a Python while loop.
(b) Write the factorial function using a Python for loop.
Check your programs to make sure they work for 0,1, 2, 3, 4,5
7. Write a program to calculate the sum of natural n natural numbers 1 + 2 + ⋯ + 𝑛.
Calculate the sum when 𝑛 = 10
8. Write a program to calculate the sum of squares of natural n natural numbers 12 + 22 +
⋯ + 𝑛2 . Calculate the sum when 𝑛 = 5.
9. Write a program to find the smallest integer n such that 3𝑛 ≥ 2000
10. Plot the function 𝑦 = 3𝑥 2 for −3 ≤ 𝑥 ≤ 3. Include enough points (say 100 points) so
that the curve you plot appears smooth. Label the axes x and y.
11. Plot the functions 𝑦 = 𝑠𝑖𝑛 𝑥 and 𝑦 = 𝑐𝑜𝑠 𝑥 for −2𝜋 ≤ 𝑥 ≤ 2𝜋 on the same plot. Plot
𝑦 = 𝑠𝑖𝑛 𝑥 in the color red and 𝑦 = 𝑐𝑜𝑠 𝑥 in the color blue and include a legend to label
the two curves. Place the legend within the plot, but such that it does not cover either of
the sine or cosine traces.
13. Write the program for (i) surface plot (ii) contour plot on 𝑥𝑦 −plane of the function
𝑥
1− +𝑥 5 +𝑦 5
𝑓(𝑥, 𝑦) = 2
2 2 on the domain -−3 ≤ 𝑥 ≤ 3 𝑎𝑛𝑑 − 3 ≤ 𝑦 ≤ 3
𝑒 −𝑥 −𝑦