Quadratic Plotter

Quadratic Plotter preview image

1 collaborator

Default-person Mighty Chen (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.1.0 • Viewed 54 times • Downloaded 11 times • Run 0 times
Download the 'Quadratic Plotter' modelDownload this modelEmbed this model

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


Comments and Questions

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

Click to Run Model

turtles-own [off-grid?]

to setup
  clear-all
  ask patches [
    set pcolor white
  ]
  draw-grid
end 

to go
  create-turtles 1 [
    ;; init turtle at leftmost point of the equation
    setxy min-pxcor plot-fn-safe min-pxcor
    set color red
    set pen-size 1.25
    set size 0.5
    set shape "circle"
    pen-down

    while [xcor < max-pxcor] [
      let next-point plot-fn-safe xcor + 0.1
      ifelse off-grid? [
        pen-up
        set xcor xcor + 0.1
        pen-down
      ] [
        setxy xcor + 0.1 next-point
      ]
    ]
    die
  ]
end 

to draw-grid
  create-turtles 1 [
    set size 0.75

    ;; draw grid lines
    setxy min-pxcor min-pycor
    facexy xcor max-pycor
    set color gray
    pen-down

    repeat max-pxcor * 2 + 1 [
      fd max-pycor * 2
      bk max-pycor * 2
      rt 90
      fd 1
      lt 90
    ]

    repeat max-pycor * 2 + 1 [
      fd 1
      lt 90
      fd max-pxcor * 2
      bk max-pxcor * 2
      rt 90
    ]

    pen-up
    set color black
    set pen-size 2

    setxy max-pxcor 0
    pen-down
    setxy min-pxcor 0
    pen-up
    setxy 0 max-pycor
    pen-down
    setxy 0 min-pycor
    die
  ]
end 

to-report solve-fn [x]
  report a * x * x + b * x + c
end 

to-report y-is-in-patches? [y]
  if y >= max-pycor or y <= min-pycor [report false]
  report true
end 

to-report plot-fn-safe [x]
  let y solve-fn x
  if y > max-pycor [setxy x max-pycor set off-grid? true report max-pycor]
  if y < min-pycor [setxy x min-pycor set off-grid? true report min-pycor]

  set off-grid? false
  report y
end 

to-report show-quadratic
  report (word pretty-a a pretty-b b pretty-c c)
end 

to-report pretty-a [n]
  if n = 0 [report ""]
  if n = 1 [report (word "" "x^2")]
  if n = -1 [report (word "-" "x^2")]
  if n < 0 [report (word "-" abs n "x^2")]
  if n > 0 [report (word abs n "x^2")]
end 

to-report pretty-b [n]
  if n = 0 [report ""]
  if n = 1 [report (word " + " "x")]
  if n = -1 [report (word " - " "x")]
  if n < 0 [report (word " - " abs n "x")]
  if n > 0 [report (word " + " abs n "x")]
end 

to-report pretty-c [n]
  if n = 0 [report ""]
  if n = 1 [report (word " + 1")]
  if n = -1 [report (word " - 1")]
  if n < 0 [report (word " - " abs n )]
  if n > 0 [report (word " + " abs n )]
end 

There are 2 versions of this model.

Uploaded by When Description Download
Mighty Chen almost 4 years ago removed animation Download this version
Mighty Chen almost 4 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Quadratic Plotter.png preview Preview for 'Quadratic Plotter' almost 4 years ago, by Mighty Chen Download

This model does not have any ancestors.

This model does not have any descendants.