Skip to content

⚠ Early release: v0.3.2.sophistry is not ready for production use. Until 1.0, any minor release may break the API; expect missing features and bugs too.Stability and versioning

Function graphs and calculus

Hand-built plots: an Axes, NumberPlane or PolarPlane maps data coordinates to scene units, and graphs, areas and slopes are drawn in it. Start with SinAndCosFunctionPlot; AreaUnderCurve and SecantToTangent are the calculus visuals.

Sine and cosine

Plot two functions on one set of axes and draw them in with draw().

SinAndCosFunctionPlot.tsx
9 collapsed lines
import { draw, RED, TEAL, track } from "@sophistry/core";
import {
Axes,
FunctionGraph,
Stage,
Text,
usePlayer,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
export default function SinAndCosFunctionPlot() {
const { play } = usePlayer();
const drawn = track(0);
onSettled(() => {
void play(draw(drawn, { duration: 2.5 }));
});
return (
<Stage units={14}>
<Axes xRange={[-7, 7, 1]} yRange={[-1.5, 1.5, 0.5]} width={12} height={4}>
<FunctionGraph function={Math.sin} stroke={RED} progress={drawn} />
<FunctionGraph function={Math.cos} stroke={TEAL} progress={drawn} />
</Axes>
<Text text="sin x" x={2} y={2.6} color={RED} />
<Text text="cos x" x={-0.6} y={2.6} color={TEAL} />
</Stage>
);
}

Log-scale axes

A log y axis turns exponential growth into a straight line, with ticks on each decade. The graph plots through the same scale as the axis.

LogScaleAxes.tsx
3 collapsed lines
import { draw, sequence, track } from "@sophistry/core";
import { Axes, FunctionGraph, Stage, usePlayer } from "@sophistry/solidjs";
import { onSettled } from "solid-js";
export default function LogScaleAxes() {
const { play } = usePlayer();
const axes = track(0);
const graphs = track(0);
// The axes first, then the curves over them.
onSettled(() => {
void play(
sequence(draw(axes, { duration: 1 }), draw(graphs, { duration: 1.2 })),
);
});
return (
<Stage units={12}>
<Axes
xRange={[0, 6, 1]}
yRange={[1, 10000]}
width={8}
height={5}
yAxis={{ scale: "log" }}
xLabel="t"
yLabel="N"
progress={axes}
>
<FunctionGraph function={(t) => 10 ** (0.65 * t)} progress={graphs} />
<FunctionGraph
function={(t) => 10 ** (0.4 * t + 1)}
progress={graphs}
/>
</Axes>
</Stage>
);
}

Polar rose

Trace the curve r = 3 cos 5θ on a polar grid by sweeping θ from 0 to π.

PolarRose.tsx
9 collapsed lines
import { draw, track } from "@sophistry/core";
import {
ParametricGraph,
PolarPlane,
Stage,
Text,
usePlayer,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const rose = (theta: number) => {
const r = 3 * Math.cos(5 * theta);
return { x: r * Math.cos(theta), y: r * Math.sin(theta) };
};
export default function PolarRose() {
const { play } = usePlayer();
const drawn = track(0);
onSettled(() => {
void play(draw(drawn, { duration: 4 }));
});
return (
<Stage units={14}>
<PolarPlane size={6} maxRadius={3} radiusStep={1}>
<ParametricGraph
function={rose}
from={0}
to={Math.PI}
progress={drawn}
/>
</PolarPlane>
<Text text="r = 3 cos 5θ" x={5} y={0} />
</Stage>
);
}

Area under a curve

Riemann rectangles are stacked ever more thinly until they fill the area under the curve.

AreaUnderCurve.tsx
11 collapsed lines
import { derive, track, valueTo } from "@sophistry/core";
import {
AreaUnderGraph,
Axes,
FunctionGraph,
RiemannRectangles,
Stage,
Text,
usePlayer,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const f = (x: number) => (x * x) / 6;
export default function AreaUnderCurve() {
const { play } = usePlayer();
const count = track(3);
onSettled(() => {
void play(valueTo(count, 60, { duration: 4 }));
});
return (
<Stage units={14}>
<Axes xRange={[0, 6]} yRange={[0, 6]} width={9} height={6}>
<AreaUnderGraph function={f} from={1} to={5} fillOpacity={0.2} />
<RiemannRectangles
function={f}
from={1}
to={5}
count={count}
sample="midpoint"
/>
<FunctionGraph function={f} />
</Axes>
<Text
text={derive(count, (value) => `${Math.round(value)} rectangles`)}
y={3.5}
/>
</Stage>
);
}

Secant to tangent

Shrink dx toward zero and the secant line through two points turns into the tangent.

SecantToTangent.tsx
10 collapsed lines
import { derive, track, valueTo } from "@sophistry/core";
import {
Axes,
FunctionGraph,
SecantSlopeGroup,
Stage,
Text,
usePlayer,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const f = (x: number) => (x * x) / 6;
export default function SecantToTangent() {
const { play } = usePlayer();
const dx = track(2.5);
onSettled(() => {
void play(valueTo(dx, 0.01, { duration: 4 }));
});
return (
<Stage units={14}>
<Axes xRange={[0, 6]} yRange={[0, 6]} width={9} height={6}>
<FunctionGraph function={f} />
<SecantSlopeGroup function={f} x={3} dx={dx} length={4} />
</Axes>
<Text text={derive(dx, (value) => `dx = ${value.toFixed(2)}`)} y={3.5} />
</Stage>
);
}

Vector field

The phase portrait of a pendulum as arrows colored by their length.

VectorFieldPendulum.tsx
3 collapsed lines
import { draw, track } from "@sophistry/core";
import { Axes, Stage, usePlayer, VectorField } from "@sophistry/solidjs";
import { onSettled } from "solid-js";
export default function VectorFieldPendulum() {
const { play } = usePlayer();
const drawn = track(0);
onSettled(() => {
void play(draw(drawn, { duration: 2 }));
});
return (
<Stage units={14}>
<Axes
xRange={[-5, 5, 1]}
yRange={[-3, 3, 1]}
width={11}
height={6}
xLabel="angle"
yLabel="speed"
>
<VectorField
function={({ x, y }) => ({ x: y, y: -Math.sin(x) })}
progress={drawn}
/>
</Axes>
</Stage>
);
}