PLOT

In review

This page runs a BASIC program that graphs the accumulation function

\[ A(t) = \int_0^t \cos(s^2)\, ds \]

from \(t = 0\) to \(t = 4\). The program is rewritten in Python and runs right here in your browser. Edit the code and press Run to redraw the graph; Reset brings back the original program.

The calculation is the same as in TABLE, now with 400 steps instead of 8, and instead of printing each value the program draws it. On each step, deltay = fnf(t) * deltat is how much the accumulation changes, and LINE (t, accumulation)-(t + deltat, accumulation + deltay) draws a segment from the current point to the next one. So the slope of each segment is fnf(t), the rate \(\cos(t^2)\). WINDOW (0, 0)-(4, 2) sets t from 0 to 4 across and the accumulation from 0 to 2 up.

Look at the shape: the graph rises steeply at first, where \(\cos(t^2)\) is near 1, reaches a high point near \(t \approx 1.25\) (where \(t^2 = \pi/2\) and the rate turns negative), and then wiggles faster and faster as \(\cos(t^2)\) oscillates more and more quickly, settling down around 0.6. Try numberofsteps = 8 to see the rough version from TABLE drawn as a graph, or change fnf to graph a different accumulation.

QBasic lets a FUNCTION be written at the end of the program, after the code that uses it. Python needs a function to be defined before it is called, so in the Python the def comes first, and return gives back the value that BASIC sets with fnf = .... COS becomes math.cos, and ^ becomes **.

Notes from the textbook

Add notes here.

 1  DEFDBL A-Z
 2  SCREEN 12
 3  WINDOW (0, 0)-(4, 2)
 4  tinitial = 0
 5  tfinal = 4
 6  t = tinitial
 7  accumulation = 0
 8  numberofsteps = 400
 9  deltat = (tfinal - tinitial) / numberofsteps
10  FOR k = 1 TO numberofsteps
11          deltay = fnf(t) * deltat
12          LINE (t, accumulation)-(t + deltat, accumulation + deltay)
13          accumulation = accumulation + deltay
14          t = t + deltat
15  NEXT k
16  FUNCTION fnf (t)
17          fnf = COS(t ^ 2)
18  END FUNCTION

Graph

Each line(...) draws one straight segment, like BASIC's LINE, and window(...) sets the visible range, like WINDOW. Hover over the graph to read coordinates.

Loading Python. The first run takes a few seconds.

Python code

Press Run (or Ctrl/⌘ + Enter) to redraw the graph. Press Esc, then Tab, to leave the editor.

Loading Python…