R7 — Drawing and sampling
Goal: draw a curve in the browser, read the numbers underneath it, and meet the two cases where a plot would quietly mislead you.
Why there is no picture on this page
plot is the one command these lessons cannot run for you. Every other transcript here is produced by executing a real session when the site is built — but drawing needs a page to draw on, so plot lives in the browser front end rather than in the shared command set.
That is a deliberate choice rather than an omission: a terminal cannot draw, and a command that cannot do anything is worse than an absent one. The same reasoning shapes R10.
So the plotting lines below are the one thing on this page you type rather than read — and you do not have to set anything up to try them. Every transcript on these pages carries a run this session link underneath it, which opens the browser REPL with that session already loaded, so you can click the one nearest the example you are reading and type the plotting line straight into it. The rest of the page is executed here as usual.
Plotting
> plot sin(x) x 0 6.28
(plotted 200 points)
The arguments are the shape you already know from R4 — what, which variable, from, to — with an optional count on the end, defaulting to 200. Three siblings share it exactly:
pointsdraws markers instead of a joined line;bodeandnyquistdraw the two standard frequency-response diagrams.
Sharing one argument syntax means there is nothing new to learn for any of them.
The same grid, as numbers
samples takes those same arguments and prints the table the plotter would have drawn — and it works everywhere, including here:
> samples sin(x) x 0 3.14159 5
0.0 0.0
0.7854 0.70711
1.5708 1.0
2.35619 0.70711
3.14159 0.0
Five points from 0 to π, as x and y in two columns. This is the command to reach for when a figure looks wrong: the numbers are the same ones the picture was built from, and numbers are easier to argue with than pixels.
When points go missing, you are told
A sweep drops any point where the expression has no finite value — a pole, or an argument outside the function’s domain. It counts what it dropped:
> samples 1/x x -1 1 11
-1.0 -1.0
-0.8 -1.25
-0.6 -1.66667
-0.4 -2.5
-0.2 -5.0
0.2 5.0
0.4 2.5
0.6 1.66667
0.8 1.25
1.0 1.0
note: only 10 of 11 samples had a finite value
Ten rows, not eleven: the grid landed on x = 0, where 1/x has no value.
That closing note is the whole point of this section. A plot of ten points where you asked for eleven looks exactly like a plot of eleven — the curve simply passes through — so without the count the pole would be invisible. Reporting N of M costs a line and turns a silent gap into a fact you can act on.
When a grid is wrong in principle, it is refused
Some expressions have values only at whole numbers. Anything built with sum, product or tabulate over a range that depends on the variable you are sweeping is one:
> samples sum(k, k, 1, n) n 0 100
samples: sum, product and tabulate need whole bounds, so sum(k, k, 1, n) has a value only where n is a whole number
note: one point per integer plots it — try: sum(k, k, 1, n) n 0 100 101
Leonardo refused, and named the grid that works.
It could have drawn instead. Over 0 … 100 with the default 200 points the step is 100/199, so exactly one sample lands on a whole number — and a chart of that one point would be a true picture of nothing anyone asked for, its spacing an artefact of where the step happened to fall rather than anything about the function. Following the advice:
> samples sum(k, k, 1, n) n 0 10 11
0.0 0.0
1.0 1.0
2.0 3.0
3.0 6.0
4.0 10.0
5.0 15.0
6.0 21.0
7.0 28.0
8.0 36.0
9.0 45.0
10.0 55.0
One point per integer — the triangular numbers, which is what the function actually has. Swap samples for plot and the same line draws it.
Note the two conditions: the expression must be whole-numbered and the grid must actually be missing points. A grid already landing on the integers is never refused, so the advice above works rather than being an obstacle.
Try it yourself
x^3 - 2x crosses zero somewhere between 1 and 2. Find roughly where, then get it exactly.
Answers
```text > samples x^3 - 2x x 1 2 6 1.0 -1.0 1.2 -0.672 1.4 -0.056 1.6 0.896 1.8 2.232 2.0 4.0 ``` *[run this session](https://lycogrypho.github.io/Leonardo/app/#s=precision%205%0Acolors%20dark%0Apretty%20off%0Alatex%20off%0Anames%20off%0Alogic%20minmax%0Alogic%20symmetric%20off%0Aexact%20precision%2030%0Aexact%20off)* The sign changes between 1.4 and 1.6. Exactly: ```text > solve(x^3 - 2x = 0, x) x_1 := -1.41421 x_2 := 0.0 x_3 := 1.41421 > unset x_1 x_1 unset > unset x_2 x_2 unset > unset x_3 x_3 unset ``` *[run this session](https://lycogrypho.github.io/Leonardo/app/#s=precision%205%0Acolors%20dark%0Apretty%20off%0Alatex%20off%0Anames%20off%0Alogic%20minmax%0Alogic%20symmetric%20off%0Aexact%20precision%2030%0Aexact%20off)* Three roots — `0` and `±√2` — of which `1.41421` is the one bracketed above.What you learned
plot,points,bodeandnyquistare browser-only, and share one argument syntax withsamples, which works everywhere.- A sweep reports what it dropped (
N of M), so a pole cannot hide inside a smooth-looking curve. - A whole-numbered expression is refused with a working recipe rather than drawn on a grid that misses it — and only when points are genuinely being lost.
- When a figure looks wrong, print the numbers.
Previous: R6 — Matrices · Next: R8 — Exact arithmetic · All lessons