Homework 1, Computing Sine and Cosine with Infinite Series due via D2L dropbox by Thursday, September 13, at 11:55 pm 10 points In…
due via D2L dropbox by Thursday, September 13, at 11:55 pm
In homework 1, we’re going to compute an infinite series (although we won’t go to infinity). Consider
the infinite series for sin(x), with x in radians. (The interval 0 to 2π radians is the same as 0 to 360
We can use a loop to compute this to varying degrees of accuracy. You’ll ask the user for the number of
terms to handle. I have posted a version of this as hw1-unsolved.txt. You will need to copy it and
rename your copy as [removed].[removed].py. (Sorry, D2L won’t let me post .py files.)
This file won’t run for you right off the bat, because the code for computing the terms is missing. But
everything else is there. You must add the comments at the top for your name, lecture, and recitation,
and you must add unique, original comments to the code that computes the terms to explain your logic.
Comments in Python begin with a #.
You are free to use either the factorial I showed in class, or math.factorial(x).
Part 1 (7 points): write a Python function called infinite_sin_1. Here’s the first line:
First, you will need a variable for the total of these terms. Start that at 0.
Second, you will ask the user how many terms to use. The command for this is a little ugly:
iterations = int(raw_input(‘Enter the number of cosine terms to add up: ‘))
Here, the raw_input() command prompts the user for a string. But we need that string to
become an integer. So we use the int() command to change the string we get back from raw_input into
Third, you will want to write a for-loop that uses the range command to make a list of numbers,
[0, 1, 2, 3, 4]. The for-loop also sets a counter variable called i to each of these numbers in turn. (Take a
look at the python examples from class to remember how for-loops work.)
Fourth, inside this for-loop, you’ll figure out what the next term is and add it to the total.
Getting the right exponent/factorial numbers.
When i=0, the exponent and factorial for that term is 1.
When i=1, the exponent and factorial for that term is 3.
When i=2, the exponent and factorial for that term is 5.
Use i to calculate the exponent and factorial for each term.Getting the right sign for each term.
This series has alternating positive and negative terms.
When i=0, 2, or any even number, the term is positive.
When i=1, 3, or any odd number, the term is negative.
Use i to figure this out.
Adding the term to the total.
When you are getting the right terms, add each one to the total inside the loop.
At the end of the function, the last line should return the total. (Else, you will have no answer!)
Fifth, use Python’s math.sin(x) to check your answer. You should be surprisingly close to the
right number after adding a few terms.
Part 2 (3 points). Write a similar method for infinite_cos_1(x). You will want to Google around for the
infinite series for cosine, which should look similar to the one you know for sine. This should require
only small changes inside the for-loop.
When I run this code and it works, I see output like this.
Enter the number of sine terms to add up: 3
Enter the number of cosine terms to add up: 6
real sine: 0.8660254037844385965883021
my sine 1: 0.8662952837868347355509968
my sine is accurate to: 0.0002698800023961389626947
real cosine: 0.5000000000000001110223025
my cosine 1: 0.4999999963909432243447384
my cosine is accurate to: 0.0000000036090568866775641
Extra credit (1 point): if you have this all working and you are bored, send me email about how bored
you are, and we’ll discuss a more efficient way to compute these series.
Save your time - order a paper!
Get your paper written from scratch within the tight deadline. Our service is a reliable solution to all your troubles. Place an order on any task and we will take care of it. You won’t have to worry about the quality and deadlinesOrder Paper Now
Computer Science homework help