TABLE
This page runs a BASIC program that makes a table for the accumulation function
\[ A(t) = \int_0^t \cos(s^2)\, ds, \]
the solution of \(y' = \cos(t^2)\) with \(y(0) = 0\). The program is rewritten in Python and runs right here in your browser. Edit the code and press Run to run it again; Reset brings back the original program.
There is no formula for \(A(t)\) in terms of familiar functions, but Euler’s method doesn’t need one. The interval from 0 to 4 is split into numberofsteps pieces of width deltat. On each piece the program computes deltay = fnf(t) * deltat, the rate at the start of the step times the width, adds it to accumulation, and moves t ahead. Each line of output shows three numbers: deltay, the running total accumulation, and the new value of t. Watch deltay change sign as \(\cos(t^2)\) swings between positive and negative; when it is negative, the accumulation goes down.
With only \(2^3 = 8\) steps the table is rough. Change numberofsteps to 2 ** 6 or 2 ** 10 and compare the last value of accumulation with the 1.1330 you get here.
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 tinitial = 0
3 tfinal = 4
4 t = tinitial
5 numberofsteps = 2 ^ 3
6 deltat = (tfinal - tinitial) / numberofsteps
7 accumulation = 0
8 FOR k = 1 TO numberofsteps
9 deltay = fnf(t) * deltat
10 accumulation = accumulation + deltay
11 t = t + deltat
12 PRINT deltay, accumulation, t
13 NEXT k
14 FUNCTION fnf (t)
15 fnf = COS(t ^ 2)
16 END FUNCTION
Output
Everything the program prints appears here, one line for each print(...),
like BASIC's PRINT. Numbers printed on the same line are lined up in columns.
Python code
Press Run (or Ctrl/⌘ + Enter) to run the program again. Press Esc, then Tab, to leave the editor.