Homework Assignment #2



HW2: CS 1004 A-2014Note: This homework (and all remaining homework assignments) is a partner homework and must be completed by each partner pair. When you complete this assignment, you must not share your answers with any other student. Submit your assignment to the web-based Turnin using your team name. Make sure that you submit before the deadline! Q1Debugging skills on displaySkillsDG-1DG-2DG-3PF-1PS-2IO-1SM-3What is wrong with the following Python program? COMMENT: Pythagorean examplemain(): side1 = 3 side2 = 4 long side = side1*side1 + side2*side1 ** 0.5 print ("hypotenuse is long side")There are six defects (some Syntax, Some Logic). Identify all defects and explain how you would fix each one. Then write down what the output of the program should be if all defects are fixed. Recall that you can type this program into a module in IDLE and then run it by selecting Run Run Module in the IDLE menu. Once the Python shell appears, invoke the main method by typing main() at the >>> prompt. Report the printed output on the template.Q2Demonstrate function returning values SkillsPM-1PF-2PF-3Sound waves are constructed based on Sinusoidal data, which can easily be computed using the mathematical Sine function. For this problem define a function mySin(x) that uses the Taylor series to approximate sin of an angle x (measured in radians) using six terms as follows: mySinx?x-x33!+ x55!-x77!+x99!- x1111! Note that the factorial function is n!=n*n-1*n-2*…*3*2*1. For this question, calculate the factorials “by hand” (i.e., without using another function).Your function must return a value, not just print it out to the console. Note that because this computation is an approximation, there are some inputs which will result in values that are not within the expected [-1, 1] range of the Sine function. Sample Output in IDLE>>> mySin(3.1415)-0.0003523372052184259>>> mySin(1.5708)0.9999999437325974>>> mySin(7)-11.842203107463511Q3Demonstrate definite for loop SkillsPF-3PF-3CS-5CS-9SM-2SM-3PM-5The Taylor expansion from Question Q1 is an infinite computation and you can compute as many terms as you wish. The mySin(x) function you wrote in Q1 computes only six terms in the expansion, but a user may wish for more for increased accuracy. taylorSinx?x-x33!+ x55!-x77!+x99!- x1111!+…±x2n-12n-1 Define a function taylorSin(x, n) that computes n terms in the Taylor series, where n is specified as a parameter. When invoked with n = 6, the function result will be identical to the mySin(x) function you wrote for Question Q1.To demonstrate your knowledge of the for loop, your function must print information as it approximates the function value. Follow the format below exactly to receive full credit. I am asking you to print this information with each pass because I believe it will help you understand the dynamic behavior of for loops.There must be exactly n lines of output, and each line shows the sign of the term being added, the exponent of the value, and the approximation computed so far, that is, after each pass.For this question, you may use the math.factorial(n) function, but you can still complete this problem without it. Note that the signs alternate and the exponents are all increasing odd numbers. Sample Output in IDLE>>> taylorSin(3.14, 3)sign = 1, exponent = 1, approximation = 3.14sign = -1, exponent = 3, approximation = -2.019857333333334sign = 1, exponent = 5, approximation = 0.52384913485333360.5238491348533336 Q4Incrementally construct a listSkillsPF-3DT-6In mathematics, an arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15 … is an arithmetic progression with common difference of 2 that starts at 5. A sequence is uniquely determined by a0 (starting value), d (the common difference) and n (the number of terms to generate). You are to write a Python function that returns a list containing the values in such a sequence. Define a function arithmeticSequence(a0, d, n) that returns a list containing the first n terms in the sequence.Sample Output in IDLE>>> arithmeticSequence(5, 2.5, 7)[5, 7.5, 10.0, 12.5, 15.0, 17.5, 20.0]>>> arithmeticSequence(0, pi/4, 7) # pi is imported from the math module[0, 0.7853981633974483, 1.5707963267948966, 2.356194490192345, 3.141592653589793, 3.9269908169872414, 4.71238898038469]Q5Incrementally construct another listSkillsPF-3DT-6When plotting real-valued functions, you often need to create a list of evenly-spaced x-coordinates drawn from some range [low, high].Define a new function called generateSamples(low, high, count) that returns a new list containing the desired number of evenly-spaced samples, including both low and high.Low and high can be integers or real numbers. You may assume that low is strictly smaller than high.This is similar to the arithmeticSequence() function of the previous question, but you have to calculate the spacing between samples.Sample Output in IDLE>>> generateSamples(1.5, 2.5, 5)[1.5, 1.75, 2.0, 2.25, 2.5]>>> >>> generateSamples(0, 2 * pi, 9)[0.0, 0.7853981633974483, 1.5707963267948966, 2.356194490192345, 3.141592653589793, 3.9269908169872414, 4.71238898038469, 5.497787143782138, 6.283185307179586]Q6Demonstrate ability to plot values using matplotlibSkillsCS-1CS-2CS-3CS-9DT-10Often you will need to generate a plot of a real-valued function. Fortunately, the matplotlib package makes this easy for you.Define a function plotRealSinVersusComputed(low, high, number) that uses pyplot to plot two functions in the given [low, high] range with a number of evenly spaced x-coordinates. The taylorSin(x, n) function you wrote for question 3 The math.sin(x) function provided by Python This function must use the generateSamples() function that you wrote for question Q5. Start with a value of 6 for the parameter n of taylorSin().The window that appears will contain a plot that looks like the following:–Notice that the Taylor series approximation is fairly close in the range ±3.5 but that it diverges quickly for angle values more than ±5, when the parameter n of taylorSin is 6. Experiment with other values of n to see how it affects the convergence.Sample Output in IDLE>>> from math import pi>>> plotRealSinVersusComputed(-2*pi, 2*pi, 100)Homework TurnIn SpecificationEvery Python module you write must include comments that declare the name of the assignment and the author. For example,# HW1. # Authors: G. Heineman, H. Lauer…For this paired assignment, both names of the students must be entered into the file.You will submit this assignment using turnin. If you are unable to log into turnin, please contact the instructor. You should have received a turnin password by email. If you have not, contact your professor.Instructions are available on the class website. For this particular assignment, you must submit a single HW2.py file that conforms to the template that has been made available. A detailed point-by-point rubric is available. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download