# Function

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)

## WHAT IS IT?

In this activity students can explore functions. Each student controls one point in the function. The activity supports several different kinds of exploration, with open ended possibilities for learning.

Students can try to make their points match an equation or description supplied by a teacher or student, or they can try to find an equation that best fits where their points are. The class should be encouraged to find unique or interesting equations that fit the criteria, to enforce the ideas of equivalence and simplification.

## HOW IT WORKS

When students log in they are assigned a character in the coordinate system. Students control the character, which is merely a point placed in the coordinate system. The teacher can send instructions to students.

Teachers can challenge students to follow a rule like, "move until your y value equals your x value". Students can explore the space, pushing the boundaries of this rule, the point (1,1) works, but so does (-1,-1) and (-0.5, -0.5).

Then students can explore alternate ways to phrase the rule, the function y = x defines this space, but so does y = 3 * x - 2 * x.

Teachers can test students' functions, by graphing them over the class' set of points. You can use any valid NetLogo code to define the function in terms of y = f(x). You can also define inequalities in terms of x. See the How To Use It section for examples.

## HOW TO USE IT

To run the activity press the GO button. To start the activity over with the same group of students stop the GO button by pressing it again, press the SETUP button, and press GO again. To run the activity with a new group of students press the RESET button in the Control Center.

Buttons: SETUP - clears the grid, if you have moved the x-maximum and minimum sliders the grid will automatically resize and redraw. GO - starts the activity so students can move their characters around the grid. ENTER-EQUATION - prompts you to enter an equation to graph in the form y = f(x), where any valid NetLogo code can be used in the function defining y, for example: "y = x" or "y = 2 * pi * sin x". You can also define inequalities in terms of x for example: "y >= 3 * x + 1". You can use any math operations supported by NetLogo and you should use correct NetLogo syntax. So instead of 2x you should enter 2 * x. CLEAR-LAST-EQUATION - removes the last equation graphed from the list and erases it from the grid

Sliders: Y-MAXIMUM, Y-MINIMUM, X-MAXIMUM, X-MINIMUM - define the bounds of the grid the students work in. Note that this is different than the size of the world, this allows you to use different size spaces without the world growing too large. You could also increase the size of the world to get a higher resolution.

Monitors: MOUSE POSITION - reports the position of the mouse in the grid.

Switches: GRID? - toggles the grid lines on and off REVEAL-LEGEND? - toggles the legend of equations in the lower right hand corner

Students' Equations: Students can send equations to the teacher, they appear in the STUDENT-EQUATION monitor. The teacher can browse through them using the "<" and ">" buttons. GRAPH-STUDENT-EQUATION - graphs the equation displayed in the STUDENT-EQUATION monitor EDIT-STUDENT-EQUATION - allows the teacher to change the student equation, either to correct the syntax or to give feedback to the student. The edits are sent to the student.

Linear Regression: If LINEAR-REGRESSION? is on all other equations will be erased and a line will be interpreted, using the students as points. The SLOPE, Y-INTERCEPT, and COEFFICIENT OF CORRELATION are calculated and displayed in the corresponding monitors.

Send a Rule: The teacher can also send a rule for students to follow by clicking the SET-RULE button and entering the "verbal" rule. The current rule is displayed in the RULE monitor.

Client Interface: Buttons: Move your character around the grid by pressing the UP, DOWN, LEFT, and RIGHT buttons which move you STEP-SIZE in the specified direction. CHANGE APPEARANCE - changes the way the characters looks in the view

Monitors: RULE - the rule set by the teacher. YOU ARE A: - a word description of your character LOCATED AT: - your coordinates in the grid

Input Boxes: MY-EQUATION - enter your equation to turn into the teacher here

## THINGS TO NOTICE

Turn the LINEAR-REGRESSION? switch to on, to see the linear regression of your set of points.

The legend can be turned on and off in the bottom right hand corner of the interface.

The bounds of the grid are controlled by the corresponding sliders. (They are distinct from the maximum and minimum world coordinates; the grid coordinate system does not depend on the patch grid coordinates.)

## THINGS TO TRY

Ask the students to follow a verbal rule like "make your y coordinate match your x coordinate". Then ask students to come up with an equation that fits the rule. Graph the different equations. Ask students to stretch the bounds of any given equation to come up with unique solutions.

Adjust the range sliders so the center of the world is not in the center of the view and graph the same equations.

## EXTENDING THE MODEL

Students' equations are not checked for syntax errors until the teacher attempts to graph them. Change the code for handling student equations so that it automatically lets students know if what they have submitted does not compile.

## NETLOGO FEATURES

This model uses the runresult primitive to approximate the y value for each x value, thus, functions can use any valid NetLogo code in the function defining y.

## HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

- Wilensky, U. and Stroup, W. (1999). NetLogo HubNet Function model. http://ccl.northwestern.edu/netlogo/models/HubNetFunction. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.

## COPYRIGHT AND LICENSE

Copyright 1999 Uri Wilensky and Walter Stroup.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

This activity and associated models and materials were created as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227.

## Comments and Questions

globals [ margin ;; constant width of border around the world width ;; width of the grid height ;; height of the grid patches-per-grid-x ;; the number of patches per grid units in the x-direction patches-per-grid-y ;; the number of patches per grid units in the y-direction grids-per-patch-x ;; number of grid units per patch in the x-direction grids-per-patch-y ;; number of grid units per patch in the y-direction grid ;; agentset of all the patches that make up the grid area (all patches - border patches) x ;; the current x variable set when evaluating the equation so it can be used by runresult x-axis ;; x-axis in patch coordinates y-axis ;; y-axis in patch coordinates rule ;; the text of a verbal rule, given by the teacher student-index ;; the index of which item in the equation-list is visible to the teacher graphed-equation-list ;; list of equations currently visible in the graph ;; each item consists of a list, first item is equation string ;; the second is the color it is graphed in. all-shapes ;; a list of the combinations of turtle shapes and colors available-shapes ;; to prevent reusing shapes all-colors color-names old-legend? ;; old values so we know if we need to toggle old-grid? old-linear-regression? lin-reg-eq ;; a string of the linear regression equation r-square ;; r^2, from linear regression slope ;; slope of linear regression y-intercept ;; y-intercept of linear regression ] patches-own [ border? ;; whether or not the patch is located in the border of the graph ] breed [students student] students-own [ user-id ;; unique id, input by student when they log in, to identify each point turtle my-x ;; the x value of the student's position on the graph my-y ;; the y value of the student's position on the graph shape-combo ;; the string describing the shape and color of the student's turtle step-size ;; the amount by which each student's turtle moves in a direction my-equation ;; the last equation turned in by the student ] ;;;;;;;;;;; ;; Setup ;; ;;;;;;;;;;; to startup clear-all hubnet-reset set margin 10 set rule "" set graphed-equation-list [] set student-index -1 set old-grid? grid? set old-legend? reveal-legend? set y-intercept "" set slope "" set r-square "" set color-names [ "red" "orange" "yellow" "brown" "green" "blue" "pink" "purple" ] set all-colors [ red orange yellow brown green blue pink violet ] set all-shapes [ "heart" "star" "circle" "diamond" "square" "rhombus" "triangle" "heart" ] setup end to setup clear-equations setup-grid set available-shapes all-shapes ask students [ set my-x random-grid-coord-x set my-y random-grid-coord-y ] end ;;;;;;;;;;;;;;;;;;;;;;;; ;; Runtime Procedures ;; ;;;;;;;;;;;;;;;;;;;;;;;; to go every 0.1 [ if any? turtles [ ask students [ send-info-to-clients ] ] listen-clients ;; keep the display in sync with the ;; switches if old-grid? != grid? [ ifelse grid? [ draw-grid draw-axes ] [ cd ] set old-grid? grid? ] if old-legend? != reveal-legend? [ ask grid [ set plabel "" ] if reveal-legend? [ draw-legend ] set old-legend? reveal-legend? ] display ] end ;; ;; Student Equation Procedures ;; to graph-student-equation let s current-student true if s = nobody [ stop ] let graphed-colors map [ item 1 ? ] graphed-equation-list if member? [color] of s graphed-colors [ set graphed-equation-list remove-item (position [color] of s graphed-colors) graphed-equation-list ask grid [ set pcolor black ] foreach graphed-equation-list [ graph-equation item 0 ? false item 1 ? ] ] graph-equation [my-equation] of s true [color] of s end ;; change the equation submitted by a student to edit-student-equation let student current-student false if student = nobody [ stop ] let eq user-input [my-equation] of student ask student [ set my-equation eq ] end ;; return the student in index, position of the sorted list ;; of the student agentset to-report current-student [ errors? ] if not any? students with [ my-equation != "" ] [ if errors? [ user-message "There are no student equations to graph." ] report nobody ] if student-index < 0 [ if errors? [ user-message "Please select a student equation using the < and > buttons." ] report nobody ] report item student-index sort students with [ my-equation != "" ] end to-report current-student-name let s current-student false ifelse s = nobody [ report s ] [ report [user-id] of s ] end to-report current-student-equation let s current-student false ifelse s = nobody [ report "" ] [ report [my-equation] of s ] end ;; advance to the next student equation to right-direction ifelse (student-index + 1 >= count students with [ my-equation != "" ] ) [ set student-index 0 ] [ set student-index student-index + 1 ] end ;; go back to the previous student equation to left-direction ifelse student-index = 0 [ set student-index (count students with [ my-equation != "" ] - 1) ] [ set student-index student-index - 1 ] end ;; ;; Graph Equations Procedures ;; to enter-equation let phrase "" set phrase user-input "Enter equation in terms of y = f(x)" graph-equation phrase true white end ;; report the operator used in a submitted equation ;; used when we do the graphing to-report operator [phrase] if member? ">=" phrase [ report ">=" ] if member? "<=" phrase [ report "<=" ] if member? ">" phrase [ report ">" ] if member? "<" phrase [ report "<" ] report "=" end ;; strip off the y=, y< etc so we can estimate ;; the equation without using runresult on every ;; patch to-report phrase-without-operator [phrase op] set phrase remove (word "y " op) phrase set phrase remove (word "y" op) phrase set phrase remove (word op " y") phrase set phrase remove (word op "y") phrase report phrase end to graph-equation [full-phrase add-to-list? current-color ] ;; if the equation has "y =" in some permutation strip it off ;; be flexible about it's presence, position, and spacing. let op operator full-phrase let phrase phrase-without-operator full-phrase op let my-message "" let success? false ;; estimate the equation at every value of x ;; going across the grid. let delta-x grids-per-patch-x set x x-minimum ;; if we're not close enough in the y direction from the last ;; point in the function draw we need to detect this and make ;; the lines appear to be connected. let last-py 0 let last-defined? false while [ x <= x-maximum ] [ ;; we do this in carefully for two reasons ;; first, there may be a syntax error in the equation. ;; second, there might be some points for which the ;; equation is undefined. carefully [ ;; estimate y at this value of x let y runresult phrase let py round patch-y y ;; if we're inside the grid ask the patch with the nearest x and y coords to set pcolor ;; to the color of this equation. ifelse y <= y-maximum and y >= y-minimum ;; if we're outside the viewable world don't plot anything [ if member? op "=" ;; plot the actual function [ ask patch round patch-x x py ;; color the patch that is closest to the correct y-value [ ;; note that we have to convert from grid coords to patch coords set pcolor current-color ;; if the y values are too far apart the line will appear broken ;; so fill in between. though if the last y value was not ;; not defined, (outside the range or mathematically undefined ;; don't connect the points. if abs (last-py - py) > 1 and last-defined? [ sprout 1 [ ifelse last-py < py [ set heading 180 ] [ set heading 0 ] repeat abs ( last-py - py ) - 1 [ fd 1 set pcolor current-color ] die ] ] ] ] set last-defined? true ] [ set last-defined? false ] ;; keep the y inside the grid let new-y min list y-maximum (max (list y-minimum y)) ;; if the operator has an inequality portion ;; fill in the rest of the column in the appropriate ;; direction. ifelse member? ">" op [ ask patch (round patch-x x) (round patch-y new-y) [ fill-inequality 0 current-color ] ] [ if member? "<" op [ ask patch (round patch-x x) (round patch-y new-y) [ fill-inequality 180 current-color ] ] ] set last-py py set success? true ] [ ;; this is a bit tricky because we don't know why it failed ;; could be a compiler error could be just undefined at this point. ;; keep track of the message and mark that this point was not ;; defined so we don't try to connect to it if the next point is. set my-message error-message set last-defined? false ] set x x + delta-x ] ;; if at least one point was plotted successfully there was ;; not a compiler error and add the equation to the list. ifelse success? [ ;; manage the graphed equation information ;; keep the legend up to date. if add-to-list? [ ;; each item in the graphed-equation-list is a list, first item is the ;; equation phrase and the second is the color it is displayed in. set graphed-equation-list lput (list (word "y " op phrase) current-color) graphed-equation-list if reveal-legend? [ draw-legend ] ] ] [ ;; if no points were plotted show the user the message so ;; s/he can fix the problem. user-message my-message ] end ;; given a direction and a color ;; fill in the rest of the column to fill-inequality [h c] ;; patch procedure sprout 1 [ set heading h loop [ fd 1 if border? [ die ] set pcolor c ] ] end ;; clear the equations and the legend to clear-equations ask patches [set plabel "" ] set graphed-equation-list [] set old-linear-regression? false end ;; clear only the last equation drawn to clear-last-equation if (length graphed-equation-list > 0) [ set graphed-equation-list butlast graphed-equation-list ask grid [ set pcolor black ] foreach graphed-equation-list [ graph-equation item 0 ? false item 1 ? ] ] if reveal-legend? [ ask grid [ set plabel "" ] draw-legend ] end ;; set the rule to the value that the user enters in an input dialog to set-rule set rule user-input "Enter the Verbal Rule:" end ;; send a list of the position of every student's turtle, to every student to send-points-to-students let point-list "" ask students [ let point-place (word "(" precision my-x 1 "," precision my-y 1 ")") set point-list (word point-list point-place " ") ] set point-list substring point-list 0 (length point-list - 1) ask students [ hubnet-send user-id "points" (point-list) ] end ;; ;; Linear Regression Procedures ;; to linear-regression ;; you need to have at least two points to make a line if (count students > 1) [ setup draw-axes ;; initialize the variables let num-turtles count students let xcors [ xcor * grids-per-patch-x ] of students let ycors [ ycor * grids-per-patch-y ] of students let sum-x sum xcors let sum-y sum ycors let ave-x sum-x / num-turtles let ave-y sum-y / num-turtles let sum-xy sum [ xcor * grids-per-patch-x * ycor * grids-per-patch-y ] of students let sum-x-square sum [ xcor * grids-per-patch-x * xcor * grids-per-patch-x ] of students let sum-y-square sum [ ycor * grids-per-patch-y * ycor * grids-per-patch-y ] of students ;; compute and store the denominator for the slope and y-intercept so we don't have to do it twice let denominator ( sum-x-square - num-turtles * ( ave-x ^ 2 ) ) ;; if the denominator = 0 or the min of xcors = the max of xcors, the turtles are in a vertical line ;; thus, the line of regression will be undefined and have a r^2 value of 1.0 ifelse ( denominator = 0 ) or ( min xcors = max xcors ) [ set slope "Undefined" set y-intercept "Undefined" ;; since all the turtles are in a line r^2 must be 1 set r-square 1 ;; set all the patches in the column to be on the line ask grid with [ pxcor = [pxcor] of one-of turtles ] [ set pcolor red ] ] [ ;; otherwise, we have some other type of line. so find the y-intercept and slope of the line set y-intercept ( ( ave-y * sum-x-square ) - ( ave-x * sum-xy ) ) / denominator set slope ( sum-xy - num-turtles * ave-x * ave-y ) / denominator ;; compute the value of r^2 ifelse ( ( sum-y-square - num-turtles * ( ave-y ^ 2 ) ) = 0 ) or ( min ycors = max ycors ) [ set r-square 1 ] [ set r-square ( ( sum-xy - num-turtles * ave-x * ave-y ) ^ 2 ) / ( ( sum-x-square - num-turtles * ( ave-x ^ 2 ) ) * ( sum-y-square - num-turtles * ( ave-y ^ 2 ) ) ) ] set r-square precision r-square 3 ;; set the equation to the appropriate string set lin-reg-eq (word (precision slope 3) " * x + " (precision y-intercept 3)) graph-equation lin-reg-eq false red ] ] end ;; ;; Grid Procedures ;; to setup-grid cp cd set old-grid? grid? set width x-maximum - x-minimum set height y-maximum - y-minimum set grids-per-patch-x width / (world-width - (2 * margin)) set grids-per-patch-y height / (world-height - (2 * margin)) set patches-per-grid-x (world-width - (2 * margin)) / width set patches-per-grid-y (world-height - (2 * margin)) / height ;; set up border patches ask patches [ ifelse (pxcor < min-pxcor + margin or pxcor > max-pxcor - margin or pycor < min-pycor + margin or pycor > max-pycor - margin) [ set pcolor 2 set border? true ] [ set pcolor black set border? false ] set plabel "" set plabel-color white ] set grid patches with [ not border? ] ;; label x and y , minimums and maximums ask patches with [ pxcor = min-pxcor + margin + 3 and pycor = min-pycor + margin - 4 ] [ set plabel x-minimum ] ask patches with [ pxcor = max-pxcor - margin and pycor = min-pycor + margin - 4 ] [ set plabel x-maximum ] ask patches with [ pxcor = min-pxcor + (margin - 2) and pycor = min-pycor + margin ] [ set plabel y-minimum ] ask patches with [ pxcor = min-pxcor + (margin - 2) and pycor = max-pycor - margin ] [ set plabel y-maximum ] ;; show x and y axes set x-axis round ( abs y-minimum * patches-per-grid-x + min-pycor + margin - 0.5 ) set y-axis round( abs x-minimum * patches-per-grid-y + min-pxcor + margin - 0.5 ) ;; if range is from ;; if range is from -20 to 20 do every 10 ;; from -10 to 10 do every 1 ;; from -1 to 1 do every .1 if grid?[ draw-grid ] draw-axes end to draw-axes ask patch round 0 x-axis [ draw-grid-line blue 90 (world-width - margin * 2) ] ask patch round y-axis 0 [ draw-grid-line blue 0 (world-height - margin * 2) ] end to draw-grid ;; do vertical lines on grid let patch-x-inc patches-per-grid-x ifelse width > 20 [ set patch-x-inc (10 * patch-x-inc) ][ if width <= 2 [ set patch-x-inc (0.1 * patch-x-inc) ] ] ask grid with [ pycor = 0 and (pxcor - round x-axis) mod round patch-x-inc = 0 ] [ draw-grid-line 3 0 (world-height - margin * 2 ) ] ;; do horizontal lines on grid let patch-y-inc patches-per-grid-y ifelse height > 20 [ set patch-y-inc (10 * patch-y-inc) ][ if height <= 2 [ set patch-y-inc (0.1 * patch-y-inc ) ] ] ask grid with [ pxcor = 0 and (pycor - round y-axis) mod round patch-y-inc = 0 ] [ draw-grid-line 3 90 (world-width - margin * 2) ] end to draw-grid-line [ c h s ] ;; patch procedure sprout 1 [ set shape "line" set color c set heading h set size s stamp die ] end to-report patch-x [ the-x ] report the-x * patches-per-grid-x + y-axis end to-report patch-y [ the-y ] report the-y * patches-per-grid-y + x-axis end to-report random-grid-coord-x ifelse width > 10 ;if the width is greater than 10, place students at whole numbers [ report random width + x-minimum ] ;if the range between 10 and 0, place students whole or half numbers [ report (random (width * 2)) / 2 + x-minimum ] end to-report random-grid-coord-y ifelse height > 10 ;if the width is greater than 10, place students at whole numbers [ report random height + y-minimum ] ;if the range between 10 and 0, place students whole or half numbers [ report (random (height * 2)) / 2 + y-minimum ] end ;; report the coordinates of the mouse, with respect to the range and scale of the graph ;; rather than the patch coordinate system to-report mouse-coords report (word "(" ( precision ((mouse-xcor - y-axis) * grids-per-patch-x) 1) "," ( precision ((mouse-ycor - x-axis) * grids-per-patch-y ) 1 ) ")") end to draw-legend let x-coord max-pxcor - (margin * 2) let y-coord min-pycor + margin + 4 let i 0 foreach graphed-equation-list [ ask patch x-coord y-coord [ set plabel item 0 ? set plabel-color item 1 ? ] set y-coord y-coord + 5 set i i + 1 ] set old-legend? reveal-legend? end ;;;;;;;;;;;;;;;;;;;;;;; ;; HubNet Procedures ;; ;;;;;;;;;;;;;;;;;;;;;;; to listen-clients let at-least-one-message? false while [ hubnet-message-waiting? ] [ set at-least-one-message? true hubnet-fetch-message ifelse hubnet-enter-message? [ create-new-student ] [ ifelse hubnet-exit-message? [ remove-student ] [ execute-command hubnet-message-tag ] ] ] if linear-regression? and (at-least-one-message? or not old-linear-regression?) [ linear-regression ] set old-linear-regression? linear-regression? end ;; update the client monitors to send-info-to-clients ;; turtle procedure hubnet-send user-id "Located at:" (word "(" precision (my-x) 2 "," precision (my-y) 2 ")") hubnet-send user-id "You are a:" (shape-combo) hubnet-send user-id "rule" (rule) end to create-new-student create-students 1 [ setup-student-vars send-info-to-clients ] end to remove-student ask students with [user-id = hubnet-message-source] [ set available-shapes sentence available-shapes shape-combo ;; make sure to return the shape to available die ] end to execute-move [new-heading] ask students with [user-id = hubnet-message-source] [ set heading new-heading let dist 0 ifelse heading mod 180 = 0 [ set dist step-size * patches-per-grid-y ] [ set dist step-size * patches-per-grid-x ] if can-move? dist and not [border?] of patch-ahead dist [ fd dist ] set my-x xcor * grids-per-patch-x set my-y ycor * grids-per-patch-y hubnet-send user-id "Located at:" (word "(" precision my-x 2 "," precision my-y 2 ")") ] end to execute-command [command] ifelse command = "Step-Size" [ ask students with [user-id = hubnet-message-source] [ set step-size hubnet-message ] ][ ifelse command = "up" [ execute-move 0 ][ ifelse command = "down" [ execute-move 180 ][ ifelse command = "right" [ execute-move 90 ][ ifelse command = "left" [ execute-move 270 ][ ifelse command = "Change Appearance" [ ask students with [ user-id = hubnet-message-source ] [ change-shape ] ][ if command = "my-equation" [ let eq hubnet-message ask students with [ user-id = hubnet-message-source ] [ set my-equation eq ] ] ] ] ] ] ] ] end to setup-student-vars ;; turtle procedure set user-id hubnet-message-source set my-x random-grid-coord-x set my-y random-grid-coord-y ;translate my-x and my-y into patches setxy patch-x my-x patch-y my-y change-shape face one-of neighbors4 set step-size 1 set size 9 set my-equation "" end to change-shape ;; turtle procedure set shape one-of all-shapes set color one-of all-colors set shape-combo (word (color-name color) " " shape) hubnet-send user-id "You are a:" shape-combo end to-report color-name [c] report item (position c all-colors) color-names end ; Copyright 1999 Uri Wilensky and Walter Stroup. ; See Info tab for full copyright and license.

There are 7 versions of this model.

## Attached files

File | Type | Description | Last updated | |
---|---|---|---|---|

Function.png | preview | Preview for 'Function' | over 11 years ago, by Uri Wilensky | Download |

This model does not have any ancestors.

This model does not have any descendants.