Lesson3 Individual Assignment

Individual means that you do it yourself. You won't learn to code if you don't struggle for yourself and write your own code. Remember that while you can discuss the general (algorithmic) way to solve a problem, you should not even be looking at anyone else's code or showing anyone else your code for an individual assignment. Ask an instructor if you are really stuck and having very specific code problems.
Review the Group Work guidelines on Cavas and/or ask an instructor if you have any questions.

Background info

During their work scientists are often calculating and converting units and coordinate systems, in this case converting between polar and rectangular/cartesian coordinate systems.

From a programming point of view, in this assignment, you will continue to write functions that take parameters. We will also start increasing the modularity of our code by defining functions that do useful pieces of calcualtions then bringing them together into a final function that calls several other earlier functions. This will make more sense when you get into the activity. One thing to watch out for though, is that you want to make sure that you are using return and not printing stuff. Really none of the functions in this assignment need to print anything.

Look back at the information on the Fundamental Principles of Polar Coordinates, the diagram, and equations in Lesson 2 Pre-Activity. You will need that information for this assignment.

Programming Practice

Be sure to spell all function names correctly - misspelled functions will lose points (and often break anyway since no one is sure what to type to call it). If you prefer showing your earlier, scratch work as you figure out what you are doing, please be sure that you make a final, complete, correct last function in its own cell that you then call several times to test. In other words, separate your thought process/working versions from the final one (a comment that tells us which is the final version would be lovely).

Every function should have at least a docstring at the start that states generally what it does (see Lesson3 team if you need a reminder). Make other comments as necessary.

Copy and run your final version of degrees2radians function from the Lesson2 Individual assignment into this notebook. Double check that this function takes an angle in degrees as a parameter and returns only the value in radians (it should not print anything at all!)

(Also, remember how meaningful white space is in Python, pasting can sometimes mess up the spaces/tabs)


In [ ]:

Test degrees2radians with the values below (in degrees) it to make sure that it still works! (feel free to add tests)

  • 45°
  • 57.296°
  • 90°
  • 180°

when you are done testing, write a brief interpretation of the test results.


In [ ]:

Now define a hypotenuse function that takes two parameters (two sides of a right triangle) and returns the radius according to Equation 3 from Lesson 2 Pre-Activity.


In [ ]:

Test hypotenuse with the values below to make sure that it works! (feel free to add tests)

  • 1, 1
  • 3, 4
  • 5, 12

when you are done testing, write a brief interpretation of the test results.


In [ ]:

Next, write a function called polar2x that takes two parameters, r and theta and returns the rectangular coordinate x.

The value of theta corresponds to the angle in radians.


In [ ]:

Test polar2x with the values (r, theta) below to make sure that it works! (feel free to add tests)

  • 1, 1
  • 3, 3.1416

when you are done testing, write a brief interpretation of the test results.


In [ ]:

Suppose you want the parameter theta to represent an angle in degrees. Since a function can use (or call) any previously defined function, modify your polar2x function so that it takes its angle parameter, theta, in degrees and uses the degrees2radians function to convert it.


In [ ]:

Test polar2x with the values (r, theta) below to make sure that it works! (feel free to add tests)

  • 1, 57.296°
  • 3, 180°
  • at least one additional of your choice

when you are done testing, write a brief interpretation of the test results.


In [ ]:

Now define a function called polar2y that takes two parameters, r and theta, and returns the rectangular coordinate y.

The value of the parameter theta corresponds to the angle in degrees (remember that functions can call other functions).


In [ ]:

Test polar2y with the values (r, theta) below to make sure that it works! (feel free to add tests)

  • 1, 57.296°
  • 3, 180° (this one is strange, make sure you interpret this result)
  • 1.4142, 45
  • at least one additional of your choice

when you are done testing, write a brief interpretation of the test results.


In [ ]:

Finally define a new function called polar2rect that takes two parameters, r and theta (in degrees), and returns the rectangular coordinates x and y. Since functions can call functions, you should be using your already defined functions inside polar2rect.


In [ ]:

Test polar2rect with the values (r, theta) below to make sure that it works! (feel free to add tests)

  • 1, 57.296°
  • 5, 45°
  • 10.4, 106.7°
  • 8, 207°
  • 9.5, 304°
  • at least one additional of your choice

when you are done testing, write a brief interpretation of the test results.


In [ ]: