Graph of linear function

The aim of the lesson is to learn the characteristics of the linear function and the way of plotting and it graphs using robots. For that reason, there is a correlation between mathematics and programming (in Python). Students are asked to think analytically, using previous knowledge of geometry.

Prerequisite

What students should know before

  • Linear Functions:  f (x) = k x + n, where k is not equal to 0

The graph of f is a line with slope k and y- intercept n.

  • trigonometric functions, finding an angle when the trigonometric function value is known

  • definition and application of complementary and supplementary angles

  • Cartesian coordinate system- characteristics

  • geometric analysis for angle more than 90 degrees, and less than 90 degrees

Time constraints:

  • starting from 45 min - single lesson

Preparing For This Tutorial:

  • The LEGO Robot that coincides with this tutorial comes from building specific sections found in the LEGO® MINDSTORMS® Education EV3 Core Set building instructions. You will need to build the main body for the robot (I’ll refer to the Base Unit), plus a gyro sensor ,two servo motors and a construction adapted to plot with a pen.

Effects

Mathematics - Following this lesson, students are able to transform knowledge from mathematical theory into a practical way ,related to the term graph of linear function.

Constructively will think about plotting the line at a given angle into a coordinate system, made through appropriate programs in Python.

Computer science - This lesson is about how to create a program in Python that will make a robot to plot a graph of a linear function, using a gyro sensor and two servomotors.

Exercise

  1. Create a program that will plot a graph of function

  1. f(x)=kx first for k>0 then k<0

  2. f(x)=kx+n for n>0 then n<0

  3. special case when k=0

  4. any kind f(x)=kx+n

  1. Test the program in two ways by making transformations

  • Vertical shifts f(x)=kx+n

(give k constant ,value k = 1 for the beginning, and change the values for n)

  • Vertical stretch or compression f(x)=kx+n

(give n constant value,n=0 for the beginning, and change values for k)

Example solution

Part 1

Mathematics

А coordinate system is indicated on a white sheet of paper or some white board.

Creating a program that plots a graph of the function f(x)=kx+n . We are considering all cases of values ​​for k and n.

In coordinate systems plot graphs of linear functions of different types

а) f(x)=kx for k>0 then k<0

  1. f(x)=kx+n for n>0 then n<0

  2. f(x)=kx+n for arbitrary k and n

In the plotings, mark the angles that occupy the graph with the x-axis and calculate their sizes.

Then determine the sizes of their respective complementary angles.

Computer science

Step 1: Configuring gyro sensor and two servomotors

Program

#!/usr/bin/env python3

from ev3dev.ev3 import \* #Ev3 crucial lib

from time import sleep #Importing the sleep only from the library time

import math #Import math lib for math functions

gyro_sensor = GyroSensor() #Defining Gyro Sensor

left_motor = LargeMotor("outB") #Defining the LargeMotor which is connected to the port B as left motor

right_motor = LargeMotor("outC") #Defining the LargeMotor which is connected to the port C as right motor

assert gyro_sensor.connected #Ensuring that

assert left_motor.connected #everything is

assert right_motor.connected #connected!

gyro_sensor.mode = 'GYRO-ANG' #Setting the mode to gyro sense to measure angles

Step 2:

Create program code for entering values for k and n, and number of graphs to be plot

#!/usr/bin/env python3

numofAtts=int(input("Input the number of graphs you want to plot:"))

while(numofAtts>0):

  k=float(input("Input k from y=k*x+n:")) #Input value for k

  print("\n") #New line

  n=float(input("Input n from y=k*x+n:")) #Input value for n

Step 3:

Create program code that move robot up and down by y-axes depending on the value of n

#!/usr/bin/env python3

if(n > 0): #If N is larger than 0

  left_motor.run_timed(time_sp=1000*n,speed_sp=100)

  right_motor.run_timed(time_sp=1000*n,speed_sp=100)

if (n < 0): #If N is less than 0

  left_motor.run_timed(time_sp=1000*abs(n),speed_sp=-100)

  right_motor.run_timed(time_sp=1000*abs(n),speed_sp=-100)

time.sleep(3)

Step 4:

Calculate k and prepare robot for the rotation

#!/usr/bin/env python3

gyro_sensor.mode = 'GYRO-RATE'

gyro_sensor.mode = 'GYRO-ANG'

gcal=abs(gyro_sensor.value()) #Storing value from gyro sensor (-33,33)

time.sleep(2) #Rest a bit with functioning

angle=0 #At the start the angle that robot and y-axis take should be 0

angleneeded=abs(math.degrees(math.atan(k)))

Step 5:

Do the rotation of the robot based on the calculations and sensor informations

#!/usr/bin/env python3

if k<0: #If k is less than 0, the angle is > 90

  while abs(angle-gcal)<90-angleneeded: #While we don't achieve the needed complementary angle

    left_motor.run_forever(speed_sp=-10) #Move the robot to the

    right_motor.run_forever(speed_sp=10) #Right

    angle=abs(gyro_sensor.value()) #Constantly update the angle

    print(angle)

    print(gcal)

elif k > 0: #If k is large than 0 then the angle is < 90

  gcal=abs(gyro_sensor.value())

  angle=abs(gyro_sensor.value())

#if gyro_sensor.value()<0:

  # angleneeded+=abs(gyro_sensor.value())

while angle-gcal<abs(90-angleneeded): #While we don't achieve the needed complementary angle

  left_motor.run_forever(speed_sp=10) #Move the robot to the

  right_motor.run_forever(speed_sp=-10) #Left

  angle=abs(gyro_sensor.value()) #While we don't achieve the needed complementary angle

  print(angle)

  print(gcal)

Step 6:

Create a program code in case where k=0(special case when the linear function is getting constant function and the graph is line parallel with x-axis)

#!/usr/bin/env python3

elif k == 0: #If k is equal to 0 than the angle is 0 with x axis

  gcal=abs(gyro_sensor.value())

  angle=abs(gyro_sensor.value())

  #if gyro_sensor.value()<0:

   # angleneeded+=abs(gyro_sensor.value())

  while angle-gcal<abs(90-angleneeded): #While we don't achieve the needed complementary angle

    left_motor.run_forever(speed_sp=10) #Move the robot to the

    right_motor.run_forever(speed_sp=-10) #Left

    angle=abs(gyro_sensor.value()) #While we don't achieve the needed complementary angle

    print(angle)

    print(gcal)

Step 7:

Finalizing the program:

  • plotting the line of graph;

  • checking the remaining number of attempts or

  • call for closing the application.

#!/usr/bin/env python3

left_motor.stop() #When we get out from the loop

right_motor.stop()# we make the robot to stop rotating since it achieved
the desired angle

left_motor.run_timed(time_sp=3250,speed_sp=100,stop_action="brake")
#Moving the robot to some point backwards \*Both motors

right_motor.run_timed(time_sp=3250,speed_sp=100,stop_action="brake")
#Moving the robot to some point backwards \*Both motors

time.sleep(8) #The robot is tired,and calculates ;)

left_motor.run_timed(time_sp=6500,speed_sp=-100,stop_action="brake")
#Moving the robot to some point backwards \*Both motors

right_motor.run_timed(time_sp=6500,speed_sp=-100,stop_action="brake")
#Moving the robot to some point backwards \*Both motors

time.sleep(5)

numofAtts-=1

raise SystemExit

Summary

Students have just learned to configure a servomotor and gyro sensor. They learned how to calculate the angle (angle occupying the graph of the function with the positive direction of the x-axis) from a given value of k in the function using trigonometric function tangent. We have just created a program that makes robot to plot a graph of the linear function f(x)=kx+n .

The lesson above can be realized in different ways.

  1. For students with good programming skills.

Give students only auxiliary questions.

We expect from the student:

  • independent and creative programming skills,

  • using and applying well-known mathematical formulas,

  • solving algorithmic problems and creating the full program code.

  1. For other students

Give some instructions to solve the problem, part of the program code or entire program code just to test it.

We expect the student to test the program by identifying special cases and transformations.

  1. Vertical shifts

f(x)=kx+n (give k a constant value, change the values for n)

  1. Vertical stretch or compression

f(x)=kx+n (give n a constant value, change the values for k)

Sketch the graphs of the functions that are obtained with the corresponding transformations.

#!/usr/bin/env python3

from ev3dev.ev3 import \*

from time import sleep

import math

gyro_sensor = GyroSensor()

left_motor = LargeMotor("outB")

right_motor = LargeMotor("outC")



assert gyro_sensor.connected

assert left_motor.connected

assert right_motor.connected



gyro_sensor.mode = 'GYRO-ANG'

numofAtts=int(input("Input the number of graphs you want to plot:"))

while(numofAtts>0):

 k=float(input("Input k from y=k*x+n:"))



 print("\n")



 n=float(input("Input n from y=k*x+n:"))





 if(n > 0):

   left_motor.run_timed(time_sp=1000*n,speed_sp=100)

   right_motor.run_timed(time_sp=1000*n,speed_sp=100)

 if (n < 0):

  left_motor.run_timed(time_sp=1000*abs(n),speed_sp=-100)

  right_motor.run_timed(time_sp=1000*abs(n),speed_sp=-100)

 time.sleep(3)

 gyro_sensor.mode = 'GYRO-RATE'

 gyro_sensor.mode = 'GYRO-ANG'

 gcal=abs(gyro_sensor.value())

 time.sleep(2)

 angle=0

 angleneeded=abs(math.degrees(math.atan(k)))

 print(angleneeded)

 angle=gcal #33

 if k<0:



   while abs(angle-gcal)<90-angleneeded:



    left_motor.run_forever(speed_sp=-10)

    right_motor.run_forever(speed_sp=10)

    angle=abs(gyro_sensor.value())

    print(angle)

    print(gcal)



 elif k > 0:

  gcal=abs(gyro_sensor.value())

  angle=abs(gyro_sensor.value())

  #if gyro_sensor.value()<0:

   # angleneeded+=abs(gyro_sensor.value())

  while angle-gcal<abs(90-angleneeded):

    left_motor.run_forever(speed_sp=10)

    right_motor.run_forever(speed_sp=-10)

    angle=abs(gyro_sensor.value())

    print(angle)

    print(gcal)



 elif k == 0:

  gcal=abs(gyro_sensor.value())

  angle=abs(gyro_sensor.value())

  #if gyro_sensor.value()<0:

   # angleneeded+=abs(gyro_sensor.value())

  while angle-gcal<abs(90-angleneeded):

    left_motor.run_forever(speed_sp=10)

    right_motor.run_forever(speed_sp=-10)

    angle=abs(gyro_sensor.value())

    print(angle)

    print(gcal)



  #Assuring that we didn't made motors to move @ all

  left_motor.stop()

  right_motor.stop()

 left_motor.run_timed(time_sp=3250,speed_sp=100,stop_action="brake")

 right_motor.run_timed(time_sp=3250,speed_sp=100,stop_action="brake")



 time.sleep(8) #The robot is tired,and calculates ;)



 left_motor.run_timed(time_sp=6500,speed_sp=-100,stop_action="brake")
 right_motor.run_timed(time_sp=6500,speed_sp=-100,stop_action="brake")

 time.sleep(5)



 numofAtts-=1

raise SystemExit
Next Section - The Magnitude of Static Friction