Graphing Algorithm

quadratic-equationIn this challenge we will write several procedures to graph different mathematical equations on screen using Python Turtle.

We will focus on:

  • Linear equations: y = ax + b
  • Quadratic equations: y = ax2 + bx + c
  • Trigonometric functions: e.g. y = cos(x)

(x,y) coordinates using Python Turtle?

xy-coordinatesOn the Python trinket used below the turtle screen is 400 pixels wide by 400 pixels high.

Look at the canvas below to understand how (x,y) coordinates work:

Linear Equation

y = ax + b

A linear equation produces a straight line when graphed on screen.

Your task:
Change the parameters used when calling the drawLine() procedure to graph the following equations:

  • y = 2x + 60
  • y = -x + 10
  • y = x
  • y = 1/2x

Quadratic Equation

y = ax2 + bx + c

Add another procedure called drawQuadraticEquation() that will take three parameters: a, b and c and be used to graph and display the matching quadratic equation.

Use this new procedure to graph the following equations:

  • y = x2 + 2x + 60
  • y = 1/2x2 – 3x + 20
  • y = x2
  • y = -x2 + 100

Trigonometric Functions

You can now add further procedures to draw the main trigonometric functions. To do so you will need to import the math library using an import statement.

Here are the three main functions you will need to implement:

  • cosine: y = cos(x)
  • sine: y = sin(x)
  • tangent: y = tan(x)
Share Button
Posted in Computer Science, Python - Advanced, Python Challenges Tagged with: , , ,

Our Latest Book