Programming Assignment 1 The following Python program is from a class task that we did together. It uses matplotlib to g

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Programming Assignment 1 The following Python program is from a class task that we did together. It uses matplotlib to g

Post by answerhappygod »

Programming Assignment 1 The Following Python Program Is From A Class Task That We Did Together It Uses Matplotlib To G 1
Programming Assignment 1 The Following Python Program Is From A Class Task That We Did Together It Uses Matplotlib To G 1 (278.16 KiB) Viewed 56 times
Programming Assignment 1 The following Python program is from a class task that we did together. It uses matplotlib to generate the animation of an expanding/shrinking circle. ## Expanding / Contracting Circle import matplotlib.pyplot as plt # Takes care of ending the program when the close button # on the figure window is clicked. def on_close (event): global finish plt.close('all') finish = True plt.connect('close_event', on_close) # Sets up the figure window and axes. ax = plt.gca () ax.set_xlim (0, 50) ax, set ylim(0, 50) ax.set_aspect (1) # Creates and displays the initial circle. rad, x, y = 2, 25, 25 cir = plt.Circle ((x, y), rad, lw=1.5, fc='red', ec='red') ax.add_patch(cir) ### Main algorithm for expanding/shrinking circle. # The amount by which radius is updated in each iteration. delta rad = 0.3 # Lower and upper bounds of the radius. These bounds cannot be changed # and must remain 1 and 10. rad_lower, rad_upper = 1, 10 finish = False while not finish: # Whenever the radius hits the upper or lower bound, # reverse the direction of radius update. if rad == rad upper or rad == rad_lower: delta_rad = -delta_rad # Display radius update on screen. cir.set radius (rad) plt.pause (0.01) # Update the radius to a new value. rad += delta_rad ### End of program The above code should change the direction of radius update (increment or decrement) when it hits the boundary of rad_upper and rad_lower. But there is a bug in the code and the circle keeps on expanding. The actual Python file is attached to this assignment for you to play around with it in the development environment. Questions: 1. [10 marks] What is the bug and why does the circle keep on expanding? You need to answer is sufficient detail, so we know that you have a good understanding. 2. [10 marks] Suggest two solutions to the above problem. In other words, suggest two different ways we can fix the code and make it run correctly. 3. [5 marks] Which of the above two that you suggested is a better solution? Give reasons for your answer.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply