FormulaT - Graphing

No preview image

1 collaborator

55 Nathan Holbert (Author)

Tags

formulat 

Tagged by Nathan Holbert almost 15 years ago

graphing 

Tagged by Nathan Holbert almost 15 years ago

physics 

Tagged by Nathan Holbert almost 15 years ago

velocity 

Tagged by Nathan Holbert almost 15 years ago

Model group LS 426-2009 | Visible to everyone | Changeable by everyone
Model was written in NetLogo 4.1RC4 • Viewed 387 times • Downloaded 41 times • Run 4 times
Download the 'FormulaT - Graphing' modelDownload this modelEmbed this model

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


VERSION

$Id: FormulaT-graphing.nlogo 44494 2009-09-27 21:05:35Z nholbert $

WHAT IS IT?

This model is a graphing tool meant to be used with the FormulaT-pitboss model though it could be easily adapted for other uses. In it you can plot points along an x and y axis and then export this data into a .txt file that can then be loaded in other models. The view can also be exported so that the user has a "hard copy" of their graph.

HOW IT WORKS

When the user presses "setup" the model creates a grid complete with a labeled x and y axis (labels are coordinates divided by 2 so that size of the graphing space is adequate.

After pressing the "new-point" button, the user can click on intersection locations in the view to add new points. These new points are actually turtles that create a link to the previously created turtle. Also, as new points are added important data about these points are written into the output area.

Finally, when the user is satisfied with their graph they can export their data into a simple text file that can be loaded into other models, and/or they can export the view as a png file for later viewing. Previously exported data can also be loaded into the view using the "Load Points" button.

HOW TO USE IT

To make a new graph the user simply needs to press "setup" and then "new-point." Clicking in the view will add new points to the graph. Users cannot create points with xcor <= previously created points and they cannot add points so the |slope| > 2 . Make sure to add a point for each "Marker" (xcor intersections) so that the data will be properly compiled.

When satisfied with the graph, the user simply presses the "Export Data" button, chooses the location and name of their data file (these should end in .txt), and then clicks save. The view can also be exported by pressing "Export Graph." Graphs should be saved as .png files.

If the user would like to load the points to a previously created graph, simply press "Load Points," choose the appropriate data file, and press "save" (note, the file is not actually saved but loaded and consequently will not replace your file!).

THINGS TO NOTICE

Once you've run your graph in the FormulaT-pitboss model, try comparing the plot created by your car to the graph you made in this model. Do they look similar? Are they different? Why?

When you plot a new on the graph you'll notice the output area write the marker, velocity, and acceleration. Acceleration is not a label for either axis. Can you figure out how the acceleration is calculated?

THINGS TO TRY

Many different graphs will create successful runs in the FormulaT-pitboss model. Try a variety of graphs to try to get the fastest time possible!

EXTENDING THE MODEL

This graph model could easily be adapted for a variety of uses. Feel free to borrow and steal code to make a graphing tool for your model!

Try changing this graph so that instead of graphing velocity on the y axis, acceleration is plotted. How does this graph compare to the velocity graph?

NETLOGO FEATURES

This model makes use of links, as well as importing and exporting features of Netlogo.

RELATED MODELS

All FormulaT models.

CREDITS AND REFERENCES

Created by Nathan Holbert 6/25/09

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [
  last-turtle
  velocity-data
  old-vel
  old-acc
]
turtles-own [
  mark
  vel-data
  acc-data
]

to setup
  clear-all

;; calculates for every other column and every other row
;; and then sets their color to light brown

  ask patches with
    [(floor((pxcor + max-pxcor - floor(2)) mod 2) = 0) or  
    (floor((pycor + max-pycor) mod 2) = 0)] [              
    set pcolor grey - 4                                    
    ]

;; creates x and y axis lines

  ask patches with [ pxcor = 0 ] [                   
    set pcolor brown
  ]
  ask patches with [ pycor = 0 ] [                   
    set pcolor brown
  ]

;; sets the labels for every other line to be half their
;; coordinate values on the axis lines

  ask patches with [ (pxcor = 0) and (floor((pycor + max-pycor) mod 2) = 0) ] [   
    set plabel ( pycor / 2)
  ]
  ask patches with [ (pycor = 0) and (floor((pxcor + max-pxcor) mod 2) = 0) ] [
    set plabel ( pxcor / 2)
  ]

;; Creates a hidden turtle that stands in for the starting line data.
;; This is also important so the graph "starts" at zero  

  create-turtles 1 [
    set color yellow
    set shape "circle"
    setxy 0 0
    hide-turtle
  ]
  
  set last-turtle -1  ;; presets last-turtle variable for use in new-point code
end 

;; To make points the user must press the Interact with Graph" button
;; and then click on intersection locations in the view which sprouts a new turtle.
;; Users cannot click on non-intersection locations and may not
;; create a point behind points they've already made.
;; When a new turtle is made, a link is made to the previous turtle
;; and it's x & y data (divided by two) is set as it's mark and
;; velocity and the "rise over run" is calculated as it's acceleration.
;; This data is "set" (so it can be written easily into a file) and then
;; it is written into the output area.
;; Users can also edit points by clicking on previously made turtles and moving
;; them to a new location.

to new-point-check 
  if mouse-down? [
    let active-patch patch mouse-xcor mouse-ycor
    ifelse not any? turtles-on active-patch 
      [new-point]
      [edit-point]
  ]
end 

to new-point
  ask patch mouse-xcor mouse-ycor [
    if not any? turtles-here [
      if (floor((pycor + max-pycor) mod 2) = 0) and (floor((pxcor + max-pxcor) mod 2) = 0) [  ;; click must be in intersection point
        if not any? turtles with [ pxcor >= round (mouse-xcor) ] [ 
          if abs(pycor - [pycor] of turtle (last-turtle + 1)) <= 4 [
            sprout 1 [  ;; make a new turtle at the clicking location
              set color yellow
              set shape "circle"
              set last-turtle who - 1  ;; sets last-turtle to previously made turtle
              set mark (pxcor / 2)     ;; things are divided by two since the graph is twice as big as it needs to be so that it looks better
              set vel-data (pycor / 2)
              set acc-data (precision( (pycor - ([pycor] of turtle (last-turtle))) / (pxcor - ([pxcor] of turtle (last-turtle))) )2)
              create-link-with turtle (last-turtle)  ;; link to last turtle so a nice line graph is made
              output-type "Marker " output-type mark output-type "  "  ;; write data in output area
              output-type "Velocity " output-type vel-data output-type "  "
              output-type "Acceleration " output-print acc-data
            ]
          ]
        ] 
      ]
    ]
  ]
end 

to edit-point
  let candidate min-one-of turtles [distancexy mouse-xcor mouse-ycor]  ;; looks for turtle under mouse
  set old-vel ([vel-data] of candidate)                                ;; saves old vel and acc data into a variable
  set old-acc ([acc-data] of candidate)
  if [distancexy mouse-xcor mouse-ycor] of candidate < 1 [
    watch candidate                                                    ;; halo turtle
    while [mouse-down?] [
      if (floor((round(mouse-ycor) + max-pycor) mod 2) = 0) and        
      (floor((round(mouse-xcor) + max-pxcor) mod 2) = 0) [             ;; turtle can only move to intersection points
        ask subject [
          ifelse turtle (who + 1) = nobody [                           ;; still building graph..
            if abs(mouse-ycor - [ycor] of turtle (who - 1)) <= 4 [     ;; |acceleration| < 2
              set ycor round(mouse-ycor)                               ;; move and update info
              set mark (pxcor / 2)
              set vel-data (pycor / 2)
              set acc-data (precision( (pycor - ([pycor] of turtle (who - 1))) / (pxcor - ([pxcor] of turtle (who - 1))) )2)
            ]
          ] [
            if abs(mouse-ycor - [ycor] of turtle (who + 1)) <= 4 and   ;; same stuff as above, but for editing already made graphs
            abs(mouse-ycor - [ycor] of turtle (who - 1)) <= 4 [
              set ycor round(mouse-ycor)
              set mark (pxcor / 2)
              set vel-data (pycor / 2)
              set acc-data (precision( (pycor - ([pycor] of turtle (who - 1))) / (pxcor - ([pxcor] of turtle (who - 1))) )2)
              ask turtle ([who] of subject + 1) [
                set acc-data (precision( (pycor - ([pycor] of turtle (who - 1))) / (pxcor - ([pxcor] of turtle (who - 1))) )2)
              ]
            ]
          ]
        ] 
      ]
    ]
    ask subject [
      if vel-data != old-vel [                                        ;; checks to make sure you actually changed stuff
        output-type "Marker " output-print mark                       ;; if you did, update output area
        output-type "Velocity changed from " output-type old-vel
        output-type " to " output-print vel-data
        output-type "Acceleration changed from " output-type old-acc
        output-type " to " output-print acc-data
      ]
    ]
    reset-perspective                                                 ;; turns off halo
  ]
end 

;; Saves the turtle data into a text file that can be
;; read by the FormulaT-pitboss model

to save-graph-data
  let file user-new-file

  if ( file != false )
  [
    if file-exists? file [  ;; overwrites files of the same name
      file-delete file
    ]
    file-open file
    ask turtles
    [
      file-write mark
      file-write vel-data
      file-write acc-data
    ]
    file-close
  ]
end 

to save-graph-image
  let file user-new-file

  if ( file != false )
  [
    if file-exists? file [  ;; overwrites files of the same name
      file-delete file
    ]
    export-view file
  ]
end 

;; Loads previously made graph points.  Note: The links and the output
;; area are not recreated, this just recreates the turtles and moves
;; them to the correct spot.

to load-data
  create-turtles 20 [
    set shape "circle"
    set color yellow
    ]
  let file user-file

  if ( file != false )
  [
    set velocity-data []
    file-open file

    while [ not file-at-end? ]
      [ set velocity-data sentence velocity-data (list (list file-read file-read file-read)) ]

    user-message "File loading complete!"
    file-close
  ]
end 

;; Moves the turtles to the correct location outlined in the loaded file.

to train-turtles
  ifelse ( is-list? velocity-data )
    [ 
      foreach velocity-data [ ask turtle first ? [ setxy (first ? * 2) (item 1 ? * 2)
        set mark first ? set vel-data item 1 ? set acc-data last ? ] ] ]
    [ user-message "You need to load in patch data first!" ]
  ask turtles [
    if who != 0 [
      create-link-with turtle (who - 1)
    ]
  ]
end 

to next-race
  run "__magic-open \\"FormulaT-pitboss\\""              ;; should open the next race...
end 

There are 2 versions of this model.

Uploaded by When Description Download
Nathan Holbert over 14 years ago (Default description) Download this version
Nathan Holbert over 14 years ago Initial upload Download this version

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.