Page 1 of 1

Launch NetLogo and go to the Code tab. Make sure that you still have declared a global variable x at the top of the Code

Posted: Tue Jul 05, 2022 10:25 am
by answerhappygod
Launch NetLogo and go to the Code tab. Make sure thatyou still have declared a global variable x at the top ofthe Code tab.
We will implement the simple identity function (x↦x) in the codetab and name it id. To do so, add the following reporterprocedure to your Code tab. (As always, usethe Check button to check your work.)
Note the use to-report, report, and end, whichyou will find in every reporter procedure. Add a comment to explainwhat this very simple function does.
A good rule of thumb is to assume untested code is broken, soalways test your function definitions. Here is a simple test to tryat the NetLogo command line: enter print (id 1). You shouldsee the value 1 printed in the Command Center. Nextenter print (id 99). You should see thevalue 99 printed in the Command Center. (We will explorebetter approaches to testing later on.)
In the Code tab, implement the squaring function(x↦x∗x) as a reporter procedure, giving it the name sq. Thisfunction takes one parameter and simply reports its square.
Hopefully, when creating the sq function, youremembered to name its parameter #x. If you instead namedit x, what happens when press the Checkbutton?(Temporarily change your code to try it.) What have you learnedabout the names of global variables and parameters? (This isNetlogo specific.)
A good rule of thumb is to assume untested code is broken, soalways test your function definitions. Here is a simple test to tryat the NetLogo command line: enter print (sq 5). You shouldsee the value 25 printed in the Command Center. Test yourfunction again at the NetLogo command line: enter print (1 +sq 0). You should see the value 1 printed in the CommandCenter.
Make sure that you have declared a globalvariable x at the top of the Code tab. Then,add the following reporter procedure to your Code tab.(As always, use the Check button to check your work.)
Enter the following lines at the command line, and add a commentexplaining the results.
Note that xpsq does not change the value of the globalvariable x. This reporter procedure has no side effects. Basedon discussion in the Glossary, does xpsq implementa pure function? Why or why not? (Explain in the Infotab.)
You should now be comfortable creating reporter procedures inorder to implement mathematical functions. Implement the bivariatereal function x↦x+0.03∗x∗(1−x). Call thisfunction nextVP.