Open In App

How to make a cosine wave graph in Python turtle?

Last Updated : 19 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to draw a Cosine wave and the inverse of a cosine wave using a turtle in Python.

What is Cosine?

The Cosine function, also written as cos or cos(x), reduces the hypotenuse of a right triangle to the projection onto the x-axis. cosine signal waveform with a shape identical to that of a sine wave it’s occurring exactly before one by four(1/4) cycle of the sine wave.

Cos θ = Adjacent side/Hypotenuse

The Cosine graph, and their degree

degree convert to radians Cos x
0 0 1
30 π/6 √3/2
45 π/4 √1/2
60 π/3 1/2
90 π/2 0

Cos Waveform:

How to make a cosine wave graph in python turtle?

 

Example 1: Generating Cosine wave

In this example, we will import the required module and set the coordination, after that we will draw vertical and horizontal lines to draw our cosine wave.

Python3




import math
import turtle
 
win = turtle.Screen()
win.bgcolor("white")
 
# coordinate setting
win.setworldcoordinates(0, -2, 3600, 2)
t = turtle.Turtle()
 
# Draw a vertical line
t.goto(0, 2)
t.goto(0, -2)
t.goto(0, 0)
 
# Draw a Horizontal line
t.goto(3600, 0)
t.penup()
t.goto(0, 1)
t.pendown()
 
t.pencolor("blue")
t.pensize(4)
 
# Generate wave form
for x in range(3600):
    y = math.cos(math.radians(x))
    t.goto(x, y)


Output:

How to make a cosine wave graph in python turtle?

 

What is Inverse Cosine Wave?

Inverse cosine is also known as arccosine. It is reciprocal of the Cosine wave. Cosine inverse of the same ratio will give the measure of the angle, y= cos -1(x) <=> cos y = x. Here, the cosine function is equal to the Adjacent side divided by the hypotenuse, and Each range value between -1 to 1 is within the limited domain (0,180).

θ = Cos -1(Adjacent side/hypotenuse)

The Inverse Cosine graph, and their degree:

y 0 π/6 π/3 π/2 2π/3 5π/6 π
x=cos-1 y 1 √3/2 √1/2 0 -√1/2 -√3/2 -1

Cos inverse Waveform:

   

 

Example 2: Inverse Cosine 

In this example, we will import the required module and set the coordination, after that we will draw vertical and horizontal lines to draw our Inverse cosine wave.

Python3




import math
import turtle
win = turtle.Screen()
win.bgcolor("white")
 
# coordinate setting
win.setworldcoordinates(-1, -180, 1, 180)
t = turtle.Turtle()
 
# Draw a Horizontal line
t.goto(1, 0)
t.goto(-1, 0)
t.penup()
t.goto(0, 0)
t.pendown()
 
# Draw a vertical line
t.goto(0, 180)
t.goto(0, -180)
t.penup()
t.goto(1, 0)
t.pendown()
t.pencolor("blue")
t.pensize(4)
 
# Generate wave form
for y in range(0, 180):
    x = math.cos(math.radians(y))
    t.goto(x, y)


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads