Complete Mathematical Reference with Proofs

Calculus

Limits · Derivatives · Integrals · Series · ODEs · Cryptography · GNU Octave

Limits & Continuity Derivatives Trig Functions Integration FTC Proofs Taylor Series ODEs Multivariable Complex Analysis Cryptography GNU Octave

§01 Limits & Continuity

The limit is the single foundational idea on which all of calculus rests. Every derivative is a limit. Every definite integral is a limit. Understanding limits rigorously means understanding calculus at its core.

Formal ε-δ Definition

limx→a f(x) = L   ⟺   ∀ε > 0, ∃δ > 0 : 0 < |x − a| < δ  ⟹  |f(x) − L| < ε

Example proof (ε-δ): Show limx→3 (2x − 1) = 5.

We need |f(x) − 5| = |(2x−1) − 5| = |2x − 6| = 2|x − 3| < ε.
Choose δ = ε/2. Then 0 < |x − 3| < δ implies 2|x−3| < 2·(ε/2) = ε. ✓

Limit Laws

Sumlim[f + g] = lim f + lim g
Productlim[f · g] = (lim f)·(lim g)
Quotientlim[f/g] = lim f / lim g,  lim g ≠ 0
Powerlim [f(x)]ⁿ = [lim f(x)]ⁿ
Compositelim f(g(x)) = f(lim g(x)),  f cont.
Squeezeg ≤ f ≤ h, lim g = lim h = L  ⟹  lim f = L

The Squeeze Theorem — limx→0 (sin x)/x = 1

This limit is used in every proof about derivatives of trigonometric functions. We prove it geometrically.

Proof via area comparison (0 < x < π/2):

Let O=(0,0), A=(1,0), P=(cos x, sin x) on the unit circle, T=(1, tan x).
Compare areas: Area(△OAP) ≤ Area(sector OAP) ≤ Area(△OAT).
½·1·sin x  ≤  ½·x  ≤  ½·1·tan x.
Divide through by ½ sin x (positive):   1 ≤ x/sin x ≤ 1/cos x.
Invert (reversing inequalities):   cos x ≤ (sin x)/x ≤ 1.
As x → 0⁺: cos x → 1. By Squeeze Theorem: lim (sin x)/x = 1.
For x → 0⁻: sin(−x)/(−x) = sin x/x, so the two-sided limit is 1.
limx→0 sin x / x = 1
limx→0 (1 − cos x) / x = 0
limx→0 (1 − cos x) / x² = 1/2

L'Hôpital's Rule

When a limit gives the indeterminate form 0/0 or ∞/∞, differentiate numerator and denominator separately.

limx→a f(x)/g(x)  [0/0 or ∞/∞]  = limx→a f′(x)/g′(x)

Proof of the 0/0 case (via Cauchy's Mean Value Theorem):

Assume f(a) = g(a) = 0, both differentiable near a, g′ ≠ 0 near a.
Cauchy's MVT on [a, x]: ∃c between a and x with [f(x)−f(a)] / [g(x)−g(a)] = f′(c)/g′(c).
Since f(a)=g(a)=0: f(x)/g(x) = f′(c)/g′(c).
As x → a, c → a (c is trapped between a and x), so f(x)/g(x) → f′(a)/g′(a).

Continuity

f is continuous at a when three conditions all hold: (1) f(a) is defined, (2) limx→a f(x) exists, (3) limx→a f(x) = f(a). A function continuous on a closed interval satisfies both the Extreme Value Theorem and the Intermediate Value Theorem.

Intermediate Value Theorem (IVT)

If f is continuous on [a,b] and N is strictly between f(a) and f(b), then there exists c ∈ (a,b) with f(c) = N.

Proof by nested interval bisection:

WLOG f(a) < N < f(b). Set a₀=a, b₀=b. Let m = (aₙ+bₙ)/2.
If f(m) = N we're done. If f(m) < N set aₙ₊₁=m, bₙ₊₁=bₙ. Otherwise aₙ₊₁=aₙ, bₙ₊₁=m.
The nested intervals [aₙ,bₙ] have bₙ−aₙ = (b−a)/2ⁿ → 0 and converge to some c.
By continuity: f(c) = lim f(aₙ) ≥ N and f(c) = lim f(bₙ) ≤ N, so f(c) = N.

GNU Octave — Limits

GNU Octave
% ─── Numerical exploration of lim sin(x)/x as x→0 ───────────────
x_vals = [0.5, 0.1, 0.01, 1e-4, 1e-8];
printf("x           sin(x)/x\n");
for x = x_vals
  printf("%.2e    %.15f\n", x, sin(x)/x);
endfor
% Catastrophic cancellation demo — do NOT use tiny h for this limit
printf("\nh = 1e-16: sin(h)/h = %.15f  (numerical noise!)\n", sin(1e-16)/1e-16);

% ─── ε-δ verification: lim_{x→3}(2x−1) = 5 ─────────────────────
a = 3;  L = 5;  epsilon = 0.01;  delta = epsilon/2;
xs = a + delta*[-0.99, -0.5, 0.5, 0.99];
printf("\nε=%.4f δ=%.4f verification:\n", epsilon, delta);
for x = xs
  fx = 2*x - 1;
  printf("  x=%.4f  f=%.4f  |f-L|=%.4f  within_ε=%d\n", ...
         x, fx, abs(fx-L), abs(fx-L) < epsilon);
endfor

% ─── L'Hôpital: lim sin(x)/x via differentiation ─────────────────
% Numerator limit: d/dx sin(x) at 0 = cos(0) = 1
% Denominator lim: d/dx x at 0 = 1  →  ratio = 1
printf("\nL'Hopital result: cos(0)/1 = %.15f\n", cos(0));

% ─── IVT: bisection root of f(x)=x³−x−2 on [1,2] ────────────────
function c = bisect(f, a, b, tol)
  while (b - a)/2 > tol
    m = (a + b)/2;
    if f(m) == 0; c = m; return; endif
    if sign(f(m)) == sign(f(a)); a = m; else; b = m; endif
  endwhile
  c = (a + b)/2;
endfunction

f    = @(x) x^3 - x - 2;
root = bisect(f, 1, 2, 1e-12);
printf("\nBisection root of x³−x−2 = %.14f  f(root)=%.2e\n", root, f(root));

% ─── Continuity check: is |x|/x continuous at 0? ─────────────────
for x = [-0.01, -1e-10, 0, 1e-10, 0.01]
  if x == 0; val = 'undef'; else; val = num2str(x/abs(x)); endif
  printf("  sign(%.2e) = %s\n", x, val);
endfor
printf("→ Limits from left/right disagree: NOT continuous at 0.\n");

§02 Derivatives — Definition & Rules

The derivative formalises the idea of instantaneous rate of change. Geometrically it is the slope of the tangent line; physically it is velocity, acceleration, or any rate.

The Derivative from First Principles

f′(x) = limh→0 [f(x+h) − f(x)] / h

Proof: d/dx (xⁿ) = nxⁿ⁻¹ (Power Rule, integer n ≥ 1)

f(x+h) − f(x) = (x+h)ⁿ − xⁿ.
Binomial theorem: (x+h)ⁿ = xⁿ + nxⁿ⁻¹h + C(n,2)xⁿ⁻²h² + … + hⁿ.
f(x+h) − f(x) = nxⁿ⁻¹·h + terms of order h² or higher.
[f(x+h)−f(x)]/h = nxⁿ⁻¹ + C(n,2)xⁿ⁻²h + … + hⁿ⁻¹.
As h → 0, every term containing h vanishes, leaving nxⁿ⁻¹.

Proof: Product Rule — (fg)′ = f′g + fg′

[(fg)(x+h) − (fg)(x)]/h = [f(x+h)g(x+h) − f(x)g(x)]/h.
Add and subtract the bridge term f(x)g(x+h):
= [f(x+h)−f(x)]/h · g(x+h)  +  f(x) · [g(x+h)−g(x)]/h.
As h → 0: first factor → f′(x), g(x+h) → g(x) (continuity); second → g′(x).
Result: f′(x)g(x) + f(x)g′(x).

Proof: Quotient Rule — (f/g)′ = (f′g − fg′)/g²

Let h = f/g, so f = gh. Differentiate both sides: f′ = g′h + gh′.
Solve for h′: h′ = (f′ − g′h)/g = [f′ − g′(f/g)]/g = (f′g − fg′)/g².

Proof: Chain Rule — [f(g(x))]′ = f′(g(x))·g′(x)

The naive proof divides by Δg, which may be zero. The rigorous version uses an auxiliary ε-function.

Let u = g(x). Define ε(k) = [f(u+k)−f(u)]/k − f′(u) for k ≠ 0, and ε(0) = 0.
By differentiability of f at u, ε is continuous at 0. Also: f(u+k)−f(u) = [f′(u)+ε(k)]·k.
Set k = g(x+h)−g(x). Then [f(g(x+h))−f(g(x))]/h = [f′(g(x))+ε(k)] · [g(x+h)−g(x)]/h.
As h → 0: k → 0, so ε(k) → 0; and [g(x+h)−g(x)]/h → g′(x).
Limit = f′(g(x)) · g′(x).

Proof: d/dx eˣ = eˣ

d/dx eˣ = eˣ · limh→0 (eʰ − 1)/h.
From the Taylor expansion eʰ = 1 + h + h²/2! + …: (eʰ−1)/h = 1 + h/2! + h²/3! + … → 1.
Therefore d/dx eˣ = eˣ · 1 = eˣ. (eˣ is its own derivative — uniquely!)

Complete Differentiation Table

d/dx c0
d/dx xⁿnxⁿ⁻¹
d/dx eˣ
d/dx aˣaˣ ln a
d/dx ln x1/x
d/dx logₐ x1/(x ln a)
d/dx sin xcos x
d/dx cos x−sin x
d/dx tan xsec²x
d/dx arcsin x1/√(1−x²)
d/dx arccos x−1/√(1−x²)
d/dx arctan x1/(1+x²)
Product(fg)′ = f′g + fg′
Quotient(f/g)′ = (f′g−fg′)/g²
Chain[f(g)]′ = f′(g)·g′

Implicit Differentiation

Differentiate both sides with respect to x, treating y as an unknown function y(x). Then solve for dy/dx.

Example: Unit circle x² + y² = 1

Differentiate: 2x + 2y(dy/dx) = 0  ⟹  dy/dx = −x/y. The tangent at (x₀,y₀) has slope −x₀/y₀, perpendicular to the radius.

Example: Elliptic curve y² = x³ − x + 1

2y(dy/dx) = 3x² − 1  ⟹  dy/dx = (3x²−1)/(2y). This formula is the group law of elliptic curve cryptography (see §12).

tangent line to y = x³ − 2x  |  move mouse to slide the point

GNU Octave — Derivatives

GNU Octave
% ─── Complex-step derivative (machine-precision, no cancellation) ─
%   f′(x) ≈ Im[f(x+ih)]/h  for tiny h — provably exact to h²
function d = csd(f, x)
  h = 1e-200;
  d = imag(f(x + 1i*h)) / h;
endfunction

printf("Complex-step derivative verification:\n");
tests = {'xⁿ',  @(x) x^5,         @(x) 5*x^4;
         'eˣ',   @exp,            @exp;
         'sin x',@sin,            @cos;
         'ln x', @log,            @(x)1/x;
         'xln x',@(x)x*log(x),   @(x)log(x)+1};
x0 = 1.7;
for i = 1:rows(tests)
  ex = tests{i,3}(x0);
  nu = csd(tests{i,2}, x0);
  printf("  d/dx %-8s exact=%+.10f  csd=%+.10f  err=%.1e\n", ...
         tests{i,1}, ex, nu, abs(ex-nu));
endfor

% ─── Central difference vs complex step accuracy comparison ───────
f  = @(x) sin(x.^2) .* exp(-x);
fp = @(x) (2*x.*cos(x^2) - sin(x^2)) .* exp(-x);
x0 = pi/3;  exact = fp(x0);
printf("\nError vs step size h:\n");
for h = [1e-1,1e-3,1e-6,1e-9,1e-12]
  cd_err = abs((f(x0+h)-f(x0-h))/(2*h) - exact);
  cs_err = abs(csd(@(x)f(x), x0) - exact);  % always ~1e-16 regardless of h
  printf("  h=%.0e  central_diff err=%.1e\n", h, cd_err);
endfor
printf("  complex-step err = %.1e (independent of h)\n", csd(@(x)f(x),x0)-exact);

% ─── Chain rule: d/dx sin(x²) = 2x·cos(x²) ──────────────────────
g  = @(x) sin(x.^2);
gp = @(x) 2*x.*cos(x.^2);
for x = [0.5, 1, pi/4]
  printf("  d/dx sin(x²)|x=%.3f: exact=%+.10f  csd=%+.10f\n", x, gp(x), csd(g,x));
endfor

% ─── Implicit diff: slope of ellipse 3x²+2y²=5 at (1,1) ──────────
% dy/dx = −3x/(2y)
x0=1; y0=1;
slope = -3*x0/(2*y0);
printf("\nImplicit: slope of 3x²+2y²=5 at (1,1): dy/dx = %.6f\n", slope);

§03 Derivatives of Trigonometric Functions

Proof: d/dx (sin x) = cos x

d/dx sin x = limh→0 [sin(x+h) − sin x]/h.
Apply angle addition: sin(x+h) = sin x cos h + cos x sin h.
= lim sin x (cos h − 1)/h + cos x (sin h / h).
Use the two key limits: lim(cos h−1)/h = 0 and lim(sin h)/h = 1 (proved in §1).
= sin x · 0 + cos x · 1 = cos x.

Proof: d/dx (cos x) = −sin x

cos(x+h) = cos x cos h − sin x sin h.
lim [cos(x+h)−cos x]/h = lim cos x (cos h−1)/h − sin x (sin h/h).
= cos x · 0 − sin x · 1 = −sin x.

Proof: d/dx (tan x) = sec²x

tan x = sin x / cos x. Apply the Quotient Rule:
d/dx(sin x / cos x) = (cos x · cos x − sin x · (−sin x)) / cos²x
= (cos²x + sin²x) / cos²x = 1/cos²x = sec²x.  (Used: sin²+cos²=1.)

Proof: d/dx (sec x) = sec x tan x

sec x = (cos x)⁻¹. By chain rule: d/dx (cos x)⁻¹ = −(cos x)⁻² · (−sin x).
= sin x / cos²x = (1/cos x)(sin x/cos x) = sec x tan x.

All Six Trigonometric Derivatives

d/dx sin xcos x
d/dx cos x−sin x
d/dx tan xsec²x
d/dx csc x−csc x cot x
d/dx sec xsec x tan x
d/dx cot x−csc²x

Inverse Trigonometric Derivatives — all from Implicit Differentiation

d/dx arcsin x1/√(1−x²)
d/dx arccos x−1/√(1−x²)
d/dx arctan x1/(1+x²)
d/dx arccsc x−1/(|x|√(x²−1))
d/dx arcsec x1/(|x|√(x²−1))
d/dx arccot x−1/(1+x²)

Proof: d/dx (arctan x) = 1/(1+x²)

Let y = arctan x, so tan y = x. Differentiate implicitly w.r.t. x:
sec²y · dy/dx = 1  ⟹  dy/dx = cos²y.
Since tan y = x: sec²y = 1 + tan²y = 1 + x², so cos²y = 1/(1+x²).
Therefore d/dx arctan x = 1/(1+x²).

Proof: d/dx (arcsin x) = 1/√(1−x²)

y = arcsin x ⟹ sin y = x. Differentiate: cos y · dy/dx = 1.
dy/dx = 1/cos y. Since sin y = x, cos y = √(1−x²) (positive on (−π/2,π/2)).
Therefore d/dx arcsin x = 1/√(1−x²).

The d/dx Cycle: Higher Derivatives of Sine

dn/dxn (sin x) = sin(x + nπ/2)

The pattern: sin → cos → −sin → −cos → sin → … cycles with period 4. In compact form: the nth derivative of sin x equals sin(x + nπ/2), verifiable by noting that differentiation multiplies the Fourier transform by iω, i.e., by e^{iπ/2} = i.

GNU Octave — Trig Derivatives

GNU Octave
% ─── Verify all 6 trig derivatives at x = π/5 ────────────────────
function d = csd(f, x); d = imag(f(x+1e-200i))*1e200; endfunction

x = pi/5;
rules = {'sin x',  @sin,  @cos;
         'cos x',  @cos,  @(x)-sin(x);
         'tan x',  @tan,  @(x)1/cos(x)^2;
         'csc x',  @csc,  @(x)-csc(x)*cot(x);
         'sec x',  @sec,  @(x)sec(x)*tan(x);
         'cot x',  @cot,  @(x)-csc(x)^2};
printf("Trig derivative verification at x=π/5:\n");
for i=1:rows(rules)
  ex = rules{i,3}(x);
  nu = csd(rules{i,2}, x);
  printf("  d/dx %-7s exact=%+.10f  csd=%+.10f  err=%.1e\n", ...
         rules{i,1}, ex, nu, abs(ex-nu));
endfor

% ─── Verify inverse trig derivatives ──────────────────────────────
printf("\nInverse trig derivatives:\n");
iv = {'arcsin', @asin, @(x)1/sqrt(1-x^2);
      'arccos', @acos, @(x)-1/sqrt(1-x^2);
      'arctan', @atan, @(x)1/(1+x^2)};
x2 = 0.4;
for i=1:3
  ex = iv{i,3}(x2);
  nu = csd(iv{i,2}, x2);
  printf("  d/dx %-8s at 0.4: exact=%+.10f  csd=%+.10f  err=%.1e\n", ...
         iv{i,1}, ex, nu, abs(ex-nu));
endfor

% ─── nth derivative of sin: d^n/dx^n sin(x) = sin(x + n*π/2) ─────
x0 = pi/3;
printf("\nnth derivative of sin x at x=π/3:\n");
for n = 0:7
  val = sin(x0 + n*pi/2);
  printf("  n=%d: sin(x+nπ/2) = %+.8f\n", n, val);
endfor

% ─── Tricky chain rule: d/dx sin(cos(x)) ─────────────────────────
f  = @(x)sin(cos(x));
fp = @(x)cos(cos(x)).*(-sin(x));
x_grid = linspace(0,2*pi,5);
printf("\nd/dx sin(cos(x)):\n");
for xv = x_grid
  printf("  x=%.4f  exact=%+.8f  csd=%+.8f\n", xv, fp(xv), csd(f,xv));
endfor

% ─── Plot: sin, cos and their derivatives ─────────────────────────
x = linspace(-2*pi, 2*pi, 1000);
figure('Name','Trig Derivatives');
plot(x,sin(x),'b-','lw',2, x,cos(x),'r--','lw',2);
legend('sin x',"d/dx sin x = cos x");
grid on; title('sin x and its derivative');

§04 Applications of Derivatives

Mean Value Theorem (MVT)

If f is continuous on [a,b] and differentiable on (a,b), then there exists c ∈ (a,b) such that:

f′(c) = [f(b) − f(a)] / (b − a)

Interpretation: the instantaneous rate of change equals the average rate of change at some interior point.

Proof via Rolle's Theorem (which is MVT when f(a)=f(b)):

Let g(x) = f(x) − f(a) − [(f(b)−f(a))/(b−a)] · (x−a). This subtracts the secant line.
Then g(a) = 0 and g(b) = f(b)−f(a)−(f(b)−f(a)) = 0.
By Rolle's Theorem: ∃c ∈ (a,b) with g′(c) = 0.
g′(c) = f′(c) − (f(b)−f(a))/(b−a) = 0, giving the MVT formula.

Critical Points, Extrema, Concavity

First Derivative Test

At critical point c (f′(c)=0 or undefined):

  • f′ changes + to − : local max
  • f′ changes − to + : local min
  • No sign change: saddle point

Second Derivative Test

  • f″(c) > 0 : local minimum (concave up ∪)
  • f″(c) < 0 : local maximum (concave down ∩)
  • f″(c) = 0 : inconclusive

Inflection Points

Points where f″ changes sign — where concavity flips. Test: solve f″(x) = 0 and check sign change.

Newton-Raphson Method

Use the tangent line to iteratively improve a root estimate. Each step replaces the curve with its tangent:

xn+1 = xn − f(xn) / f′(xn)

Converges quadratically near a simple root: the error squares at each step, so correct digits roughly double per iteration.

Related Rates

Classic: Sliding Ladder

A 10-m ladder leans against a wall. The foot slides outward at 2 m/s. How fast does the top slide down when the foot is 6 m from the wall?

Constraint: x² + y² = 100. Differentiate w.r.t. t: 2x(dx/dt) + 2y(dy/dt) = 0.
At x=6: y = √64 = 8. So 2·6·2 + 2·8·(dy/dt) = 0 ⟹ dy/dt = −24/16 = −1.5 m/s.

Expanding Oil Slick (circle area)

A = πr². Differentiate: dA/dt = 2πr · (dr/dt). If r=5 m and dr/dt=0.3 m/min, then dA/dt = 3π ≈ 9.42 m²/min.

Linearisation & Differentials

f(x) ≈ f(a) + f′(a)(x−a)    (tangent line approximation)

The differential df = f′(x) dx quantifies the approximate change in output for a small change dx in input. This is the basis of error propagation in physics and engineering.

GNU Octave — Optimization & Newton-Raphson

GNU Octave
% ─── Newton-Raphson with convergence tracking ─────────────────────
function [root,iters,errs] = newton(f, fp, x0, tol)
  x = x0; errs = [];
  for k = 1:50
    fx = f(x);  errs(end+1) = abs(fx);
    if abs(fx) < tol; root=x; iters=k; return; endif
    x = x - fx/fp(x);
  endfor
  root=x; iters=50;
endfunction

% Find √2: solve x²−2=0
f = @(x)x^2-2;  fp = @(x)2*x;
[r,k,e] = newton(f, fp, 1.5, 1e-15);
printf("√2 by Newton-Raphson:\n");
for i=1:length(e)
  printf("  iter %d: |f|=%.3e  (digits ≈ %.1f)\n", i, e(i), -log10(e(i)+1e-16));
endfor
printf("  Result: %.15f  error=%.2e\n", r, abs(r-sqrt(2)));

% Quadratic convergence: each step squares the error
printf("\nConvergence ratio |eₙ₊₁|/|eₙ|²:\n");
for i=1:length(e)-2
  printf("  step %d→%d: %.4f (should→constant)\n", i, i+1, e(i+1)/e(i)^2);
endfor

% ─── Optimize f(x) = x⁴−4x²+x using Newton on f′=0 ──────────────
f2  = @(x) x^4 - 4*x^2 + x;
fp2 = @(x) 4*x^3 - 8*x + 1;
fpp = @(x) 12*x^2 - 8;
printf("\nCritical points of x⁴−4x²+x:\n");
for x0 = [-1.5, 0.1, 1.6]
  [r,~,~] = newton(fp2, fpp, x0, 1e-13);
  typ = '?'; if fpp(r)>0; typ='min'; elseif fpp(r)<0; typ='max'; endif
  printf("  x₀=%.2f → x*=%.8f  f=%.6f  f''=%.4f → %s\n", x0,r,f2(r),fpp(r),typ);
endfor

% ─── Linearisation: approximate sin(0.1) near a=0 ────────────────
a = 0;
printf("\nLinearisation of sin(x) near x=0:\n");
for h = [0.1, 0.3, 0.5, 1.0]
  lin   = sin(a) + cos(a)*(h - a);   % = h (since sin(0)=0, cos(0)=1)
  exact = sin(h);
  printf("  x=%.1f: linear=%.6f  exact=%.6f  err=%.2e\n", h, lin, exact, abs(lin-exact));
endfor

§05 Integration & the Fundamental Theorem

The definite integral is defined as the limit of Riemann sums. The Fundamental Theorem of Calculus (FTC) connects differentiation and integration — arguably the most important result in all of mathematics.

The Riemann Integral

Partition [a,b] into n subintervals of width Δx = (b−a)/n. Choose sample points xi* in each subinterval:

ab f(x) dx = limn→∞ Σi=1n f(xi*) Δx
riemann sum for f(x) = sin(x)+1.2 on [0, π] — click to toggle rule

Fundamental Theorem of Calculus — Part 1

If F(x) = ∫ax f(t) dt  then  F′(x) = f(x)

Proof of FTC Part 1:

F(x+h) − F(x) = ∫xx+h f(t) dt.
By the Extreme Value Theorem, f attains a minimum m and maximum M on [x, x+h].
m·h ≤ ∫xx+h f(t) dt ≤ M·h (for h > 0).
Dividing by h: m ≤ [F(x+h)−F(x)]/h ≤ M.
As h → 0, both m and M → f(x) by continuity. By the Squeeze Theorem: F′(x) = f(x).

Fundamental Theorem of Calculus — Part 2

ab f(x) dx = F(b) − F(a)    where F′ = f

Proof of FTC Part 2:

Let G(x) = ∫ax f(t) dt. By Part 1, G′ = f.
Any two antiderivatives differ by a constant: F(x) = G(x) + C.
F(b) − F(a) = [G(b)+C] − [G(a)+C] = G(b) − G(a) = ∫ab f dx − 0.

Standard Antiderivatives

∫ xⁿ dx (n≠−1)xⁿ⁺¹/(n+1) + C
∫ 1/x dxln|x| + C
∫ eˣ dxeˣ + C
∫ aˣ dxaˣ/ln a + C
∫ sin x dx−cos x + C
∫ cos x dxsin x + C

Properties of Definite Integrals

Reversalab = −∫ba
Additivityac = ∫ab + ∫bc
Linearity∫(αf+βg) = α∫f + β∫g
Even function−aa f dx = 2∫0a f dx
Odd function−aa f dx = 0
Comparisonf ≤ g ⟹ ∫f ≤ ∫g

Numerical Integration: Trapezoid & Simpson's Rules

Trapezoid: h/2 · [f(a) + 2Σf(xᵢ) + f(b)]  error O(h²)
Simpson's: h/3 · [f(a)+4f(x₁)+2f(x₂)+…+f(b)]  error O(h⁴)

GNU Octave — Integration

GNU Octave
% ─── Riemann sums: left, midpoint, right ─────────────────────────
function s = riemann(f, a, b, n, rule)
  h  = (b-a)/n;
  switch rule
    case 'left';  xs = a + (0:n-1)*h;
    case 'right'; xs = a + (1:n)*h;
    case 'mid';   xs = a + (0.5:1:n-0.5)*h;
  endswitch
  s = h * sum(f(xs));
endfunction

f     = @(x) sin(x) + 1.2;
a=0; b=pi;
exact = quadgk(f, a, b);
printf("∫₀^π (sin x + 1.2) dx = %.15f\n", exact);
printf("\nRiemann sum convergence:\n");
for n = [5, 20, 100, 1000]
  L = riemann(f,a,b,n,'left');
  M = riemann(f,a,b,n,'mid');
  printf("  n=%4d: left=%.8f  mid=%.8f  mid_err=%.2e\n", n,L,M,abs(M-exact));
endfor

% ─── Composite trapezoid and Simpson's ───────────────────────────
function s = trapz_rule(f,a,b,n)
  x = linspace(a,b,n+1); h=(b-a)/n;
  s = h/2*(f(x(1))+2*sum(f(x(2:end-1)))+f(x(end)));
endfunction
function s = simpsons(f,a,b,n)  % n must be even
  x = linspace(a,b,n+1); h=(b-a)/n;
  s = h/3*(f(x(1)) + 4*sum(f(x(2:2:end-1))) + 2*sum(f(x(3:2:end-2))) + f(x(end)));
endfunction

g = @(x) exp(-x.^2/2) / sqrt(2*pi);   % Gaussian PDF
exact_g = quadgk(g, -3, 3);
printf("\nGaussian CDF P(|X|<3) via quadrature:\n");
printf("  exact = %.15f\n", exact_g);
printf("  trapz (n=100) err = %.2e\n", abs(trapz_rule(g,-3,3,100)-exact_g));
printf("  Simp  (n=100) err = %.2e\n", abs(simpsons(g,-3,3,100)-exact_g));

% ─── FTC Part 1 demo: d/dx ∫₀ˣ sin(t²) dt ≈ sin(x²) ─────────────
F  = @(x) quadgk(@(t)sin(t.^2), 0, x);
Fp = @(x) sin(x^2);   % FTC Part 1 prediction
h  = 1e-5;
x0 = 1.3;
numerical_deriv = (F(x0+h) - F(x0-h))/(2*h);
printf("\nFTC Part 1: d/dx ∫₀ˣ sin(t²)dt at x=1.3:\n");
printf("  FTC prediction sin(x²) = %.10f\n", Fp(x0));
printf("  Numerical deriv of F(x) = %.10f\n", numerical_deriv);

§06 Integration of Trigonometric Functions

Basic Trig Integrals

∫ sin x dx−cos x + C
∫ cos x dxsin x + C
∫ tan x dx−ln|cos x| + C
∫ cot x dxln|sin x| + C
∫ sec x dxln|sec x + tan x| + C
∫ csc x dx−ln|csc x + cot x| + C
∫ sec²x dxtan x + C
∫ csc²x dx−cot x + C
∫ sec x tan x dxsec x + C
∫ csc x cot x dx−csc x + C

Proof: ∫ tan x dx = −ln|cos x| + C

∫ tan x dx = ∫ (sin x / cos x) dx. Let u = cos x, du = −sin x dx.
= ∫ −du/u = −ln|u| + C = −ln|cos x| + C.

Proof: ∫ sec x dx = ln|sec x + tan x| + C

Multiply numerator and denominator by (sec x + tan x):
∫ sec x · (sec x + tan x)/(sec x + tan x) dx = ∫ (sec²x + sec x tan x)/(sec x + tan x) dx.
Let u = sec x + tan x; then du = (sec x tan x + sec²x) dx — exactly the numerator.
= ∫ du/u = ln|u| + C = ln|sec x + tan x| + C.

Power Reduction: Even Powers of Sine and Cosine

sin²x = (1 − cos 2x)/2
cos²x = (1 + cos 2x)/2
sin²x cos²x = (1 − cos 4x)/8

Proof: ∫₀ sin²x dx = π

∫₀ sin²x dx = ∫₀ (1 − cos 2x)/2 dx = [x/2 − sin(2x)/4]₀ = π − 0 = π.

Trig Substitution

Integral containsSubstitutionIdentity usedRange
√(a²−x²)x = a sin θ1 − sin²θ = cos²θθ ∈ [−π/2, π/2]
√(a²+x²)x = a tan θ1 + tan²θ = sec²θθ ∈ (−π/2, π/2)
√(x²−a²)x = a sec θsec²θ − 1 = tan²θθ ∈ [0, π/2)

The Gaussian Integral — Proof via Polar Coordinates

−∞ e−x² dx = √π

Proof — the most elegant computation in analysis (uses trig via polar coords):

Let I = ∫−∞ e−x² dx. Consider I² as a double integral:
I² = (∫−∞ e−x²dx)(∫−∞ e−y²dy) = ∫∫ℝ² e−(x²+y²) dx dy.
Switch to polar: x=r cosθ, y=r sinθ, and dx dy = r dr dθ:
I² = ∫₀ ∫₀ e−r² r dr dθ = 2π · ∫₀ r e−r² dr.
Let u = r²: ∫₀ r e−r² dr = ½ ∫₀ e−u du = ½.
So I² = 2π · ½ = π, therefore I = √π.  (The sign is + since e−x² > 0.)

Fourier Orthogonality — Foundation of Signal Processing & Cryptography

∫₀ sin(mx) sin(nx) dx = π · δmn     (Kronecker delta)

Proof (m ≠ n):

Use product-to-sum: sin(mx)sin(nx) = ½[cos((m−n)x) − cos((m+n)x)].
∫₀ cos(kx) dx = [sin(kx)/k]₀ = 0 for integer k ≠ 0.
So ∫₀ sin(mx)sin(nx) dx = ½(0 − 0) = 0 for m ≠ n. ✓
For m = n: ∫₀ sin²(nx) dx = ∫₀ (1−cos(2nx))/2 dx = π. ✓

GNU Octave — Trig Integration

GNU Octave
% ─── Verify all basic trig integrals ─────────────────────────────
tests = {'∫ sin',     @sin,           @(x)-cos(x);
         '∫ cos',     @cos,           @sin;
         '∫ sec²',    @(x)sec(x).^2,  @tan;
         '∫ sec·tan', @(x)sec(x).*tan(x), @sec};
a=0.3; b=1.1;
printf("Trig integral verification [%.1f, %.1f]:\n", a, b);
for i=1:rows(tests)
  ftc  = tests{i,3}(b) - tests{i,3}(a);
  num  = quadgk(tests{i,2}, a, b);
  printf("  %-12s FTC=%.10f  quad=%.10f  err=%.1e\n", ...
         tests{i,1}, ftc, num, abs(ftc-num));
endfor

% ─── Fourier orthogonality: ∫₀^2π sin(mx)sin(nx) dx ─────────────
printf("\nFourier inner products (should be π on diagonal, 0 off):\n");
for m=1:4
  for n=1:4
    v = quadgk(@(x)sin(m*x).*sin(n*x), 0, 2*pi);
    printf("%6.3f", v/pi);
  endfor; printf("\n");
endfor

% ─── Gaussian integral: ∫_{-∞}^{∞} e^{-x²} dx = √π ─────────────
I = quadgk(@(x)exp(-x.^2), -30, 30);
printf("\n∫e^(-x²)dx = %.15f  √π = %.15f\n", I, sqrt(pi));

% ─── Power reduction: ∫₀^π sin⁴(x)dx = 3π/8 ────────────────────
I4 = quadgk(@(x)sin(x).^4, 0, pi);
printf("∫₀^π sin⁴x dx = %.12f  (3π/8=%.12f)\n", I4, 3*pi/8);

% ─── Trig substitution: ∫₀^1 √(1−x²)dx = π/4 (unit semicircle) ─
Iq = quadgk(@(x)sqrt(1-x.^2), 0, 1);
printf("∫₀^1 √(1−x²)dx = %.15f  (π/4=%.15f)\n", Iq, pi/4);

% ─── Wallis product: ∫₀^{π/2} sinⁿ dx via reduction formula ──────
function W = wallis(n)
  if n==0; W=pi/2; elseif n==1; W=1; else; W=(n-1)/n*wallis(n-2); endif
endfunction
printf("\nWallis integrals ∫₀^{π/2} sinⁿ(x) dx:\n");
for n=0:6
  printf("  n=%d: formula=%.8f  quad=%.8f\n", n, wallis(n), ...
         quadgk(@(x)sin(x).^n, 0, pi/2));
endfor

§07 Integration Techniques

u-Substitution (Reverse Chain Rule)

∫ f(g(x)) g′(x) dx = ∫ f(u) du     where u = g(x)

Integration by Parts (IBP)

∫ u dv = uv − ∫ v du

Proof from the Product Rule:

d/dx(uv) = u(dv/dx) + v(du/dx). Integrate both sides over [a,b]:
uv|ab = ∫ u dv/dx dx + ∫ v du/dx dx = ∫ u dv + ∫ v du.
Rearranging: ∫ u dv = uv|ab − ∫ v du.

LIATE priority for choosing u:

Logarithm → Inverse trig → Algebraic (polynomial) → Trig → Exponential

Example: ∫ x ex dx — choose u=x (Algebraic), dv=exdx → ∫ x eˣ dx = xeˣ − eˣ + C.

Reduction Formula for ∫ sinⁿx dx

∫ sinⁿx dx = −sinⁿ⁻¹x cosx / n + (n−1)/n · ∫ sinⁿ⁻²x dx

Proof via integration by parts:

Let u = sinⁿ⁻¹x,   dv = sin x dx. Then du = (n−1)sinⁿ⁻²x cosx dx,   v = −cos x.
∫ sinⁿx dx = −sinⁿ⁻¹x cosx + (n−1) ∫ sinⁿ⁻²x cos²x dx.
Replace cos²x = 1 − sin²x:
= −sinⁿ⁻¹x cosx + (n−1) ∫ sinⁿ⁻²x dx − (n−1) ∫ sinⁿx dx.
Collect: n ∫ sinⁿx dx = −sinⁿ⁻¹x cosx + (n−1) ∫ sinⁿ⁻²x dx. Divide by n.

Partial Fractions

For rational P(x)/Q(x) with deg P < deg Q, decompose Q into factors:

Linear factor (x−a)

A/(x−a)

Repeated (x−a)ⁿ

A₁/(x−a) + … + Aₙ/(x−a)ⁿ

Irreducible quadratic

(Ax+B)/(x²+bx+c)

Worked Example: ∫ x sin x dx

IBP with u = x, dv = sin x dx. Then du = dx, v = −cos x.
∫ x sin x dx = −x cos x − ∫ (−cos x) dx = −x cos x + sin x + C.
Check: d/dx(−x cos x + sin x) = −cos x + x sin x + cos x = x sin x. ✓

Worked Example: ∫ ln x dx

Write ∫ ln x dx = ∫ ln x · 1 dx. IBP: u = ln x, dv = dx. Then du = dx/x, v = x.
= x ln x − ∫ x · (1/x) dx = x ln x − ∫ 1 dx = x ln x − x + C.

GNU Octave — Integration Techniques

GNU Octave
% ─── IBP: ∫ x·sin(x) dx = −x·cos(x) + sin(x) ────────────────────
antideriv = @(x) -x.*cos(x) + sin(x);
printf("∫₀^π x·sin(x) dx:\n");
printf("  FTC: %.15f  (exact: π=%.15f)\n", antideriv(pi)-antideriv(0), pi);
printf("  quad: %.15f\n", quadgk(@(x)x.*sin(x), 0, pi));

% ─── IBP: ∫ x²·eˣ dx (needs IBP twice) ──────────────────────────
% Antideriv: eˣ(x²−2x+2)
A2 = @(x) exp(x).*(x.^2 - 2*x + 2);
printf("\n∫₀^1 x²·eˣ dx:\n");
printf("  FTC: %.15f\n", A2(1)-A2(0));
printf("  quad: %.15f\n", quadgk(@(x)x.^2.*exp(x), 0, 1));

% ─── Reduction formula verification ──────────────────────────────
function W = wallis(n)
  if n==0; W=pi/2; elseif n==1; W=1; else; W=(n-1)/n*wallis(n-2); endif
endfunction
printf("\nReduction formula ∫₀^{π/2} sinⁿx dx:\n");
for n = [2,4,6,8]
  w = wallis(n);
  q = quadgk(@(x)sin(x).^n, 0, pi/2);
  printf("  n=%d: formula=%.10f  quad=%.10f  err=%.1e\n", n, w, q, abs(w-q));
endfor

% ─── Partial fractions: ∫ 1/(x²−1) dx ───────────────────────────
% 1/(x²−1) = (1/2)/(x−1) − (1/2)/(x+1)
% Antideriv = (1/2)·ln|(x−1)/(x+1)|
A3 = @(x) 0.5*log(abs((x-1)./(x+1)));
I_pf  = quadgk(@(x)1./(x.^2-1), 2, 5);
I_ftc = A3(5) - A3(2);
printf("\n∫₂^5 1/(x²−1) dx: quad=%.12f  FTC=%.12f  err=%.1e\n", I_pf, I_ftc, abs(I_pf-I_ftc));

% ─── u-sub: ∫ x·√(x²+1) dx = (x²+1)^{3/2}/3 ────────────────────
A4 = @(x) (x.^2+1).^(3/2)/3;
printf("∫₀^2 x√(x²+1) dx: FTC=%.10f  quad=%.10f\n", ...
       A4(2)-A4(0), quadgk(@(x)x.*sqrt(x.^2+1), 0, 2));

§08 Taylor & Maclaurin Series

Taylor series let us represent smooth functions as infinite polynomials. They connect differentiation, integration, and exponential/trig functions through a single algebraic framework.

Taylor's Theorem with Remainder

f(x) = Σk=0n f(k)(a)/k! · (x−a)k  +  Rn(x)
Lagrange remainder: Rn(x) = f(n+1)(c)/(n+1)! · (x−a)n+1   for some c between a and x

Proof via repeated Integration by Parts:

Start from FTC: f(x) − f(a) = ∫ax f′(t) dt.
Integrate by parts with u=f′(t), dv=1·dt, but using v = t−x (not t) to ensure the boundary term vanishes at t=x:
ax f′(t) dt = [(t−x)f′(t)]ax − ∫ax (t−x)f″(t) dt = (x−a)f′(a) + ∫ax (x−t)f″(t) dt.
Repeat with v = −(x−t)²/2: each step produces one more Taylor term.
After n steps: f(x) = Σk=0n f(k)(a)(x−a)k/k! + ∫ax f(n+1)(t)(x−t)n/n! dt.
The integral remainder equals f(n+1)(c)(x−a)n+1/(n+1)! by the MVT for integrals.

Essential Maclaurin Series (a = 0)

Σ xⁿ/n!  (all x)
sin xΣ (−1)ⁿ x^{2n+1}/(2n+1)!  (all x)
cos xΣ (−1)ⁿ x^{2n}/(2n)!  (all x)
ln(1+x)Σ (−1)ⁿ⁺¹ xⁿ/n  (|x| ≤ 1)
1/(1−x)Σ xⁿ  (|x| < 1)
arctan xΣ (−1)ⁿ x^{2n+1}/(2n+1)  (|x| ≤ 1)
(1+x)ᵅΣ C(α,n) xⁿ  (|x| < 1, binomial)
sinh xΣ x^{2n+1}/(2n+1)!  (all x)
cosh xΣ x^{2n}/(2n)!  (all x)

Proof: sin x = x − x³/3! + x⁵/5! − …

Compute derivatives of f=sin x at a=0: f(0)=0, f′(0)=1, f″(0)=0, f‴(0)=−1, f⁽⁴⁾(0)=0, …
Pattern: even-order derivatives are 0; odd-order alternate +1, −1.
Taylor formula: sin x = 0 + x/1! + 0 − x³/3! + 0 + x⁵/5! − … = Σ(−1)ⁿ x^{2n+1}/(2n+1)!
The series converges for all x: |R_{2n+1}| ≤ |x|^{2n+2}/(2n+2)! → 0.

Euler's Formula — Proved by Taylor Series

e = cos θ + i sin θ
Expand e = Σ (iθ)ⁿ/n! and separate real (even) and imaginary (odd) terms.
Real part: Σ (−1)ⁿ θ^{2n}/(2n)! = cos θ.
Imaginary part: i · Σ (−1)ⁿ θ^{2n+1}/(2n+1)! = i sin θ.
At θ = π: e = −1 + 0i, giving the identity e + 1 = 0.

Leibniz Formula for π

π/4 = 1 − 1/3 + 1/5 − 1/7 + … = Σn=0 (−1)ⁿ/(2n+1)
Arctan series: arctan x = x − x³/3 + x⁵/5 − … for |x| ≤ 1.
Set x=1: arctan(1) = π/4 = Σ(−1)ⁿ/(2n+1). Convergence at x=1 follows from alternating series test.

GNU Octave — Taylor Series & Euler

GNU Octave
% ─── Taylor series for sin x: convergence ────────────────────────
function s = taylor_sin(x, N)
  s = 0;
  for n = 0:N-1
    s = s + (-1)^n * x^(2*n+1) / factorial(2*n+1);
  endfor
endfunction

x = pi/3;
printf("Taylor sin(π/3) convergence:\n");
for N = 1:9
  ap = taylor_sin(x, N);
  printf("  N=%d: %.15f  err=%.2e\n", N, ap, abs(ap-sin(x)));
endfor

% ─── Euler's formula: e^{iθ} = cos θ + i sin θ ───────────────────
theta = pi/5;
lhs = exp(1i*theta);
rhs = cos(theta) + 1i*sin(theta);
printf("\nEuler e^{iπ/5}:  LHS=%.10f%+.10fi\n", real(lhs), imag(lhs));
printf("               RHS=%.10f%+.10fi\n", real(rhs), imag(rhs));
printf("  e^{iπ}+1 = %.2e + %.2ei (should be 0)\n", ...
       real(exp(1i*pi)+1), imag(exp(1i*pi)+1));

% ─── Leibniz series and faster Machin formula for π ──────────────
printf("\nLeibniz π/4 partial sums:\n");
s = 0;
for n=0:9
  s = s + (-1)^n/(2*n+1);
  printf("  n=%d: 4s=%.10f  err=%.2e\n", n, 4*s, abs(4*s-pi));
endfor
% Machin: π/4 = 4·atan(1/5) − atan(1/239) — converges far faster
pi_machin = 4*(4*atan(1/5) - atan(1/239));
printf("Machin π=%.15f  err=%.2e\n", pi_machin, abs(pi_machin-pi));

% ─── Taylor coefficients via FFT (Cauchy's coefficient formula) ──
function c = taylor_fft(f, a, n, r)
  % c(k) = f^(k)(a)/k! via |f(a+r·e^{2πij/N})|/N
  N  = max(256, 2^ceil(log2(n+1)));
  th = 2*pi*(0:N-1)/N;
  z  = a + r*exp(1i*th);
  fv = arrayfun(f, z);
  cf = real(ifft(fv));
  c  = cf(1:n+1) ./ (r.^(0:n));
endfunction

c = taylor_fft(@sin, 0, 8, 1);
printf("\nTaylor coefficients of sin(x) at 0 (via FFT):\n");
for k=0:8
  if abs(c(k+1))>1e-10
    printf("  k=%d: %.10f  (1/k!=%.10f)\n", k, c(k+1), (-1)^((k-1)/2)/factorial(k));
  endif
endfor

§09 Ordinary Differential Equations

Separation of Variables

dy/dx = f(x)g(y)  ⟹  ∫ dy/g(y) = ∫ f(x) dx

Proof: General solution of y′ = ky (exponential growth/decay)

Separate: dy/y = k dx.
Integrate: ln|y| = kx + C₁.
Exponentiate: y = Aekx where A = ±eC₁ (absorbs the sign; A=0 also works trivially).
IVP with y(0) = y₀: A = y₀, so y(x) = y₀ ekx.

First-Order Linear ODE: Integrating Factor Method

y′ + P(x)y = Q(x)     Integrating factor: μ(x) = e∫P(x)dx

Derivation:

Multiply both sides by μ: μy′ + μP(x)y = μQ(x).
The left side is exactly (μy)′, since (μy)′ = μ′y + μy′ = μPy + μy′. (using μ′ = μP)
Integrate: μy = ∫ μQ dx + C. Solve: y = [∫μQ dx + C] / μ.

Second-Order Linear ODEs: Characteristic Equation

ay″ + by′ + cy = 0     Try y = erx:   ar² + br + c = 0

Two distinct real roots r₁ ≠ r₂

y = C₁er₁x + C₂er₂x

Repeated root r = r₁ = r₂

y = (C₁ + C₂x)erx

Complex roots r = α ± βi

y = eαx[C₁cos(βx) + C₂sin(βx)]

Simple Harmonic Oscillator (SHO)

y″ + ω²y = 0     Solution: y = A cos(ωt) + B sin(ωt) = C sin(ωt + φ)

Proof that y = sin(ωt) satisfies y″ + ω²y = 0:

y = sin(ωt), y′ = ω cos(ωt), y″ = −ω² sin(ωt).
y″ + ω²y = −ω² sin(ωt) + ω² sin(ωt) = 0. ✓
This is why sine and cosine ARE the solutions to the SHO — the relationship runs both ways.

Damped Oscillator

my″ + cy′ + ky = F₀cos(ωt)

Underdamped (c² < 4mk)

Oscillates with decaying envelope: e−αt[C₁cos(βt)+C₂sin(βt)]

Critically damped (c²=4mk)

Fastest return: (C₁+C₂t)e−αt

Overdamped (c²>4mk)

No oscillation: C₁er₁t+C₂er₂t, r₁,r₂ < 0

The Logistic Equation

dP/dt = rP(1 − P/K)     Solution: P(t) = K / [1 + ((K−P₀)/P₀) e−rt]

Models population growth with carrying capacity K. Separable ODE; its S-shaped solution curve is a fundamental shape in biology, epidemiology, and machine learning (the sigmoid function).

GNU Octave — ODEs

GNU Octave
% ─── Exponential decay: y'=−0.5y, y(0)=3 ────────────────────────
[t,y] = ode45(@(t,y)-0.5*y, [0 10], 3);
exact = @(t) 3*exp(-0.5*t);
err   = max(abs(y - exact(t)));
printf("Decay y'=−0.5y: max ode45 err = %.2e\n", err);

% ─── SHO: y''+4y=0, y(0)=1, y'(0)=0 → y=cos(2t) ─────────────────
sho = @(t,y)[y(2); -4*y(1)];
[ts,ys] = ode45(sho, [0 4*pi], [1;0]);
err_sho = max(abs(ys(:,1) - cos(2*ts)));
printf("SHO y''+4y=0: max err vs cos(2t) = %.2e\n", err_sho);

% ─── Damped oscillator: y''+2cy'+4y=0 for c=0.3,2,4 ─────────────
printf("\nDamped oscillator at t=10:\n");
for c = [0.3, 2, 4]
  ode = @(t,y)[y(2); -2*c*y(2)-4*y(1)];
  [td,yd] = ode45(ode,[0 10],[1;0]);
  disc = (2*c)^2 - 16;
  if disc<0; typ='under'; elseif disc==0; typ='crit'; else; typ='over'; endif
  printf("  c=%.1f (%sdamped): y(10)=%.6f\n", c, typ, interp1(td,yd(:,1),10));
endfor

% ─── Logistic equation ────────────────────────────────────────────
r=1.5; K=100; P0=5;
logistic_exact = @(t) K ./ (1 + (K-P0)/P0 * exp(-r*t));
[tl,Pl] = ode45(@(t,P)r*P*(1-P/K), [0 8], P0);
err_log = max(abs(Pl - logistic_exact(tl)));
printf("\nLogistic: P(8)=%.4f  exact=%.4f  err=%.2e\n", ...
       Pl(end), logistic_exact(8), err_log);

% ─── Lotka-Volterra (predator-prey) ──────────────────────────────
a=1.5; b=1; d=3; g=1;
lv = @(t,y)[a*y(1)-b*y(1)*y(2); d*y(1)*y(2)-g*y(2)];
[tlv,ylv] = ode45(lv,[0 15],[1;0.5]);
figure('Name','Predator-Prey');
plot(tlv,ylv(:,1),'b-','lw',2, tlv,ylv(:,2),'r-','lw',2);
legend('Prey','Predator'); grid on; title('Lotka-Volterra equations');

§10 Multivariable Calculus

Partial Derivatives

∂f/∂x = limh→0 [f(x+h, y) − f(x,y)] / h    (treat y as constant)

Clairaut's Theorem: Symmetry of Mixed Partials

fxy(a,b) = fyx(a,b)    if both are continuous near (a,b)

Proof sketch:

Define Δ(h,k) = f(a+h,b+k) − f(a+h,b) − f(a,b+k) + f(a,b).
Let φ(x) = f(x,b+k) − f(x,b). By MVT: Δ = h·φ′(c₁) = h·[f_x(c₁,b+k)−f_x(c₁,b)].
Apply MVT again: = h·k·f_{xy}(c₁,c₂) for c₁ near a, c₂ near b.
Doing this in reversed order gives h·k·f_{yx}(d₁,d₂). Dividing by hk and taking h,k→0: f_{xy}=f_{yx}.

The Gradient, Divergence, Curl, Laplacian

Gradient ∇f(∂f/∂x, ∂f/∂y, ∂f/∂z) — direction of steepest ascent
Divergence ∇·F∂Fx/∂x + ∂Fy/∂y + ∂Fz/∂z — net outward flux
Curl ∇×F(∂Fz/∂y−∂Fy/∂z, ∂Fx/∂z−∂Fz/∂x, ∂Fy/∂x−∂Fx/∂y)
Laplacian ∇²f∂²f/∂x² + ∂²f/∂y² + ∂²f/∂z² = div(grad f)
Chain ruledf/dt = ∇f · dr/dt (directional rate of change along path)
DirectionalD_u f = ∇f · û  (|û|=1)

Critical Points in 2D — Hessian Test

D = fxxfyy − (fxy)²     (Hessian determinant)

D > 0, fxx > 0

Local minimum

D > 0, fxx < 0

Local maximum

D < 0

Saddle point

D = 0

Test inconclusive

Green's and Stokes' Theorems

Green's: ∮C (P dx + Q dy) = ∬D (∂Q/∂x − ∂P/∂y) dA
Stokes': ∮C F·dr = ∬S (∇×F)·dS

Green's theorem is a 2D special case of Stokes'. Both connect a line integral around a boundary to a flux integral over the enclosed surface — a profound connection between local and global behaviour.

Lagrange Multipliers

Optimise f(x,y) subject to g(x,y) = c:    ∇f = λ ∇g  and  g = c

Proof intuition:

At a constrained extremum, moving along the constraint g=c cannot increase/decrease f.
So ∇f must be orthogonal to the constraint curve, i.e., parallel to ∇g (which is always orthogonal to g=c).
Therefore ∇f = λ∇g for some scalar λ (the "Lagrange multiplier").

GNU Octave — Multivariable Calculus

GNU Octave
% ─── Partial derivatives via complex step ────────────────────────
f  = @(x,y) sin(x.*y) + x.^2.*cos(y);
h  = 1e-200;
x0 = pi/4;  y0 = pi/3;
dfdx_num = imag(f(x0+1i*h, y0))/h;
dfdy_num = imag(f(x0, y0+1i*h))/h;
% Exact: ∂f/∂x = y·cos(xy)+2x·cos(y),  ∂f/∂y = x·cos(xy)−x²·sin(y)
dfdx_ex = y0*cos(x0*y0)+2*x0*cos(y0);
dfdy_ex = x0*cos(x0*y0)-x0^2*sin(y0);
printf("∂f/∂x: exact=%.12f  cs=%.12f  err=%.1e\n", dfdx_ex, dfdx_num, abs(dfdx_ex-dfdx_num));
printf("∂f/∂y: exact=%.12f  cs=%.12f  err=%.1e\n", dfdy_ex, dfdy_num, abs(dfdy_ex-dfdy_num));

% ─── Gradient descent on f(x,y) = x²+2y²−xy+x ───────────────────
f2   = @(v) v(1)^2 + 2*v(2)^2 - v(1)*v(2) + v(1);
grad = @(v) [2*v(1)-v(2)+1; 4*v(2)-v(1)];
v = [4;3];  lr = 0.08;
printf("\nGradient descent (lr=0.08):\n");
for k=1:40
  v = v - lr*grad(v);
  if mod(k,8)==0
    printf("  k=%2d: v=[%.6f,%.6f]  f=%.8f  |∇|=%.2e\n",...
           k, v(1),v(2), f2(v), norm(grad(v)));
  endif
endfor
% Exact: ∇f=0 → 2x−y+1=0, 4y−x=0 → x=−4/7, y=−1/7
printf("  Exact min: x=%.8f, y=%.8f\n", -4/7, -1/7);

% ─── Hessian test for f(x,y)=x³−3x+y³−3y ────────────────────────
% Critical pts: 3x²−3=0 → x=±1; 3y²−3=0 → y=±1
printf("\nHessian classification of x³−3x+y³−3y:\n");
for cx = [-1,1]
  for cy = [-1,1]
    fxx=6*cx; fyy=6*cy; fxy=0;
    D = fxx*fyy - fxy^2;
    if     D>0&&fxx>0; t='min';
    elseif D>0&&fxx<0; t='max';
    else;               t='saddle'; endif
    fval = cx^3-3*cx+cy^3-3*cy;
    printf("  (%+d,%+d): D=%.1f → %s  f=%.1f\n", cx,cy,D,t,fval);
  endfor
endfor

% ─── Laplace's equation solver (finite differences) ──────────────
N=25; u=zeros(N);
u(end,:) = sin(linspace(0,pi,N));  % top BC: u=sin(πx)
for iter=1:3000
  u(2:end-1,2:end-1) = ...
    (u(1:end-2,2:end-1)+u(3:end,2:end-1)+ ...
     u(2:end-1,1:end-2)+u(2:end-1,3:end))/4;
endfor
% Exact: u(x,y) = sin(πx)·sinh(πy)/sinh(π)
exact_u = @(x,y) sin(pi*x).*sinh(pi*y)/sinh(pi);
x_pts = linspace(0,1,N); y_pts = x_pts;
[Xg,Yg] = meshgrid(x_pts, y_pts);
err_laplace = max(abs(u(:) - exact_u(Xg(:),Yg(:))));
printf("\nLaplace solver (25×25 grid) max error: %.3e\n", err_laplace);

§11 Complex Analysis & Calculus

11.1 — Complex Differentiation & the Cauchy–Riemann Equations

Let f(z) = u(x,y) + iv(x,y) where z = x + iy. The function f is complex-differentiable (holomorphic) at z₀ if the limit

f′(z₀) = limh→0 [f(z₀+h) − f(z₀)] / h

exists regardless of the direction h approaches 0 in the complex plane.

PROOF — Cauchy–Riemann Equations
Approach along the real axis (h = Δx): f′ = ∂u/∂x + i·∂v/∂x.
Approach along the imaginary axis (h = iΔy): f′ = ∂v/∂y − i·∂u/∂y.
Equating real and imaginary parts:
∂u/∂x = ∂v/∂y   &   ∂u/∂y = −∂v/∂x
These are the Cauchy–Riemann equations. When satisfied (with continuous partials), f is holomorphic.

11.2 — Harmonic Functions

PROOF — Real & imaginary parts of holomorphic functions are harmonic
From C-R: ∂u/∂x = ∂v/∂y and ∂u/∂y = −∂v/∂x.
Differentiate: ∂²u/∂x² = ∂²v/(∂x∂y) and ∂²u/∂y² = −∂²v/(∂y∂x).
By Clairaut's theorem (§10), the mixed partials are equal, so:
∂²u/∂x² + ∂²u/∂y² = 0   (Laplace's equation ∇²u = 0)
Identically for v. Thus both u and v are harmonic.

Harmonic functions model steady-state heat, electrostatics, and fluid flow — the Laplace PDE solver in §13 computes them numerically.

11.3 — Cauchy's Integral Theorem & Formula

THEOREM — Cauchy's Integral Theorem
If f is holomorphic on and inside a simple closed contour C, then:
C f(z) dz = 0
Sketch: By Green's theorem, ∮C f dz = ∬D (−∂v/∂x − ∂u/∂y) + i(∂u/∂x − ∂v/∂y) dA. The Cauchy–Riemann equations make both integrands vanish.
THEOREM — Cauchy's Integral Formula
If f is holomorphic inside and on C, and a is inside C:
f(a) = (1/2πi) ∮C f(z)/(z−a) dz
Differentiating n times under the integral:
f(n)(a) = (n!/2πi) ∮C f(z)/(z−a)n+1 dz
This reveals that holomorphic ⟹ infinitely differentiable — a stunning consequence absent in real analysis.

11.4 — Laurent Series & the Residue Theorem

Near an isolated singularity z₀, f has a Laurent expansion:

f(z) = Σn=−∞ aₙ(z−z₀)n

The coefficient a₋₁ is the residue of f at z₀:

Res(f, z₀) = a₋₁ = (1/2πi) ∮ f(z) dz
THEOREM — Residue Theorem
If f is holomorphic inside C except at isolated singularities z₁,…,zₙ:
C f(z) dz = 2πi · Σk=1n Res(f, zk)
Each singularity contributes its residue; the holomorphic remainder integrates to zero by Cauchy's theorem.

11.5 — Evaluating Real Integrals via Residues

EXAMPLE — ∫0 dx/(1+x²) = π/2
Let f(z) = 1/(1+z²) = 1/[(z+i)(z−i)]. Integrate over a semicircular contour in the upper half-plane.
The only pole inside is z = i with Res(f,i) = 1/(2i).
The semicircle integral → 0 as R → ∞ (by the ML inequality: |f| ≤ 1/R² on the arc, arc length = πR).
So ∫−∞ dx/(1+x²) = 2πi · 1/(2i) = π, giving ∫0 = π/2.

11.6 — Conformal Mappings

A holomorphic function with f′(z₀) ≠ 0 preserves angles at z₀ — it is conformal. Key maps:

MapFormulaEffect
Möbiusw = (az+b)/(cz+d)Maps circles/lines to circles/lines
Joukowskiw = z + 1/zCircle → airfoil shape (aerodynamics)
Exponentialw = ezHorizontal strip → wedge/sector
Schwarz–Christoffeldw/dz = C·∏(z−xk)αk−1Upper half-plane → polygon

Cryptographic note: conformal maps on elliptic curves (§12) transform the group law while preserving algebraic structure — the basis of isogeny-based post-quantum cryptography.

§12 Cryptography — The Calculus of Security

12.1 — Information Entropy & Lagrange Multipliers

Shannon entropy measures uncertainty in a probability distribution {p₁,…,pₙ}:

H = −Σi=1n pi ln pi
PROOF — Maximum entropy is achieved by the uniform distribution
Maximize H = −Σ pᵢ ln pᵢ subject to constraint g = Σ pᵢ − 1 = 0.
Lagrangian: ℒ = −Σ pᵢ ln pᵢ − λ(Σ pᵢ − 1).
∂ℒ/∂pᵢ = −ln pᵢ − 1 − λ = 0  ⟹  pᵢ = e−1−λ = constant for all i.
From Σ pᵢ = 1: pᵢ = 1/n. So Hmax = ln n.
The Hessian ∂²ℒ/∂pᵢ∂pⱼ = −δᵢⱼ/pᵢ is negative-definite, confirming a maximum.

Cryptographic meaning: A truly random key of n symbols carries H = ln n bits of entropy — any non-uniform distribution leaks information an attacker can exploit.

12.2 — Continuous Probability & the CDF

For a continuous random variable X with PDF f(x):

P(a ≤ X ≤ b) = ∫ab f(x) dx     CDF: F(x) = ∫−∞x f(t) dt     F′(x) = f(x) by FTC

The normal distribution N(μ,σ²) has PDF (1/σ√(2π)) e−(x−μ)²/(2σ²). Its integral has no closed form — the error function erf(x) = (2/√π)∫₀ˣ e−t² dt is computed numerically.

12.3 — Elliptic Curve Cryptography: Group Law via Implicit Differentiation

An elliptic curve E: y² = x³ + ax + b defines a group. The tangent-and-chord addition rule uses calculus directly.

PROOF — Point doubling formula via implicit differentiation
Given P = (x₁, y₁) on E, differentiate y² = x³ + ax + b implicitly:
2y · dy/dx = 3x² + a  ⟹  m = (3x₁² + a) / (2y₁)
The tangent line at P: y = m(x − x₁) + y₁. Substitute into E:
[m(x−x₁)+y₁]² = x³+ax+b. Expand and use y₁² = x₁³+ax₁+b:
x³ − m²x² + … = 0. This cubic has a double root at x₁, so by Vieta's:
x₃ = m² − 2x₁,   y₃ = m(x₁ − x₃) − y₁.
The result 2P = (x₃, −y₃) is the group doubling operation at the heart of ECDSA, ECDH, and every ECC system.

Point addition (P ≠ Q): slope m = (y₂−y₁)/(x₂−x₁), then same formulas. The scalar multiplication kP = P+P+⋯+P (k times) via double-and-add is the trapdoor: computing kP is fast (O(log k) doublings), but recovering k from kP is the Elliptic Curve Discrete Logarithm Problem (ECDLP) — believed intractable.

12.4 — RSA & Euler's Theorem

PROOF — Euler's theorem: aφ(n) ≡ 1 (mod n)
Let Z*ₙ = {r₁,…,rφ(n)} be the units mod n. For gcd(a,n)=1, the map rᵢ ↦ a·rᵢ mod n permutes Z*ₙ.
Therefore ∏ rᵢ ≡ ∏ (a·rᵢ) = aφ(n) · ∏ rᵢ (mod n).
Since ∏ rᵢ is invertible mod n, divide both sides: aφ(n) ≡ 1.

RSA construction: Choose primes p, q; n = pq; φ(n) = (p−1)(q−1). Pick e with gcd(e,φ(n))=1; compute d ≡ e⁻¹ mod φ(n). Encrypt: c = mᵉ mod n. Decrypt: m = cd = med = m1+kφ(n) = m · (mφ(n))k ≡ m · 1k = m (mod n).

12.5 — Differential Privacy: The Laplace Mechanism

To publish a numeric query f(D) on a dataset D without revealing any individual, add Laplace noise:

Output = f(D) + Lap(Δf / ε)

where Δf = maxD,D′ |f(D) − f(D′)| is the sensitivity (over neighbouring datasets) and ε is the privacy budget.

PROOF — ε-differential privacy of Laplace mechanism
The Laplace PDF is p(x) = (ε/2Δf) e−ε|x|/Δf. For datasets D, D′ differing in one record:
P(output = y | D) / P(output = y | D′) = exp(−ε|y−f(D)|/Δf) / exp(−ε|y−f(D′)|/Δf)
= exp(ε(|y−f(D′)| − |y−f(D)|) / Δf)
By the triangle inequality: |y−f(D′)| − |y−f(D)| ≤ |f(D)−f(D′)| ≤ Δf.
Therefore the ratio ≤ eε, which is the definition of ε-differential privacy.

12.6 — Hash Functions & Sensitivity to Initial Conditions

Cryptographic hash functions (SHA-256, BLAKE3) are designed so that changing one input bit cascades unpredictably — the avalanche effect. This mirrors sensitivity to initial conditions in dynamical systems:

|δ(t)| ≈ |δ₀| · eλt

where λ > 0 is the Lyapunov exponent. The exponential function (whose derivative equals itself: (eˣ)′ = eˣ from §2) is the engine of both chaotic divergence and cryptographic diffusion.

12.7 — Lattice Cryptography & Gaussian Distributions

Post-quantum lattice schemes (CRYSTALS-Kyber, CRYSTALS-Dilithium) sample error vectors from a discrete Gaussian over a lattice Λ:

DΛ,σ(x) = ρσ(x) / ρσ(Λ)    where ρσ(x) = exp(−π‖x‖² / σ²)

The smoothing parameter ηε(Λ) — the σ at which the Gaussian "smears" over Λ so that its distribution is nearly uniform modulo the fundamental domain — is computed via the Poisson summation formula, which itself relies on the Fourier transform (integration from §5–§7).

12.8 — Zero-Knowledge Proofs & the Schwartz–Zippel Lemma

A ZK-SNARK proves knowledge of a secret without revealing it. At its core lies polynomial identity testing:

LEMMA — Schwartz–Zippel
If p(x₁,…,xₙ) is a nonzero polynomial of total degree d over a field 𝔽, and S ⊆ 𝔽:
P(p(r₁,…,rₙ) = 0) ≤ d / |S|    for random rᵢ ∈ S
Proof sketch (n=1): A degree-d polynomial has at most d roots (Fundamental Theorem of Algebra, proved via complex analysis §11). So the probability of hitting a root is ≤ d/|S|. The multivariate case follows by induction on n.

In a ZK-SNARK, the prover commits to a polynomial encoding the computation. The verifier picks a random evaluation point r. If the polynomial is wrong, it will disagree with probability ≥ 1 − d/|𝔽| — overwhelming for a large field.

GNU Octave — Cryptographic Calculus
% --- 12A: Shannon Entropy ---
p = [0.5 0.25 0.125 0.125];
H = -sum(p .* log2(p));
printf("Shannon entropy: %.4f bits (max = %.4f for n=%d)\n", H, log2(length(p)), length(p));

% --- 12B: ECC Point Doubling on y²=x³+2x+3 (mod 97) ---
a = 2; p_mod = 97;
P = [3, 6]; % verify: 6²=36 ≡ 3³+2·3+3=36 (mod 97) ✓
m = mod((3*P(1)^2 + a) * power_mod(2*P(2), p_mod-2, p_mod), p_mod);
function r = power_mod(base, exp, m)
  r = 1; base = mod(base, m);
  while exp > 0
    if mod(exp,2) == 1, r = mod(r*base, m); end
    exp = floor(exp/2); base = mod(base*base, m);
  end
end
x3 = mod(m^2 - 2*P(1), p_mod);
y3 = mod(m*(P(1)-x3) - P(2), p_mod);
printf("ECC double: 2·(%d,%d) = (%d,%d) on y²=x³+2x+3 mod 97\n", P(1),P(2),x3,y3);

% --- 12C: Differential Privacy — Laplace Mechanism ---
true_mean = 50; sensitivity = 1; epsilon = 0.5;
n_releases = 10000;
noise = -sensitivity/epsilon .* sign(rand(n_releases,1)-0.5) .* log(1-2*abs(rand(n_releases,1)-0.5));
dp_vals = true_mean + noise;
printf("DP mechanism (ε=%.1f): mean=%.3f, std=%.3f\n", epsilon, mean(dp_vals), std(dp_vals));

% --- 12D: Lyapunov Exponent of Logistic Map ---
r = 3.9; x = 0.1; N = 10000; lyap = 0;
for k = 1:N
  lyap = lyap + log(abs(r - 2*r*x));  % derivative of rx(1-x) is r-2rx
  x = r*x*(1-x);
end
lyap = lyap / N;
printf("Logistic map (r=%.1f) Lyapunov exponent: %.4f (chaotic if >0)\n", r, lyap);

§13 GNU Octave — Calculus Laboratory

13.1 — Numerical Differentiation: Complex-Step Method

The classical finite difference f′(x) ≈ [f(x+h)−f(x)]/h suffers from cancellation error for small h. The complex-step derivative avoids this entirely:

f′(x) = Im[f(x + ih)] / h + O(h²)    (no subtraction!)
GNU Octave — Complex-Step Derivative
% Complex-step: machine-precision derivatives with one function eval
function d = complex_step(f, x, h)
  if nargin < 3, h = 1e-20; end
  d = imag(f(x + 1i*h)) / h;
end

f = @(x) sin(x).^3 .* exp(-x.^2);
x0 = 1.0;
d_cs = complex_step(f, x0);
d_exact = 3*sin(x0)^2*cos(x0)*exp(-x0^2) - 2*x0*sin(x0)^3*exp(-x0^2);
printf("Complex-step: %.15f\nExact:        %.15f\nError:        %.2e\n", d_cs, d_exact, abs(d_cs-d_exact));

13.2 — Taylor Coefficients via FFT

GNU Octave — Taylor Coefficients from FFT
% Extract Taylor coefficients of f(z) around z₀ using Cauchy's formula + FFT
function c = taylor_fft(f, z0, r, N)
  % f = function handle, z0 = center, r = radius, N = number of terms
  theta = 2*pi*(0:N-1)/N;
  z = z0 + r*exp(1i*theta);
  c = fft(f(z)) / N;
  c = c ./ (r.^(0:N-1));  % scale by r^(-n)
  c = real(c);  % clean roundoff for real-analytic functions
end

% Test: Taylor of e^x around 0 (should give 1/n!)
c = taylor_fft(@exp, 0, 1, 10);
exact = 1 ./ factorial(0:9);
printf("Taylor of exp(x): FFT vs exact:\n");
for k = 0:9
  printf("  a_%d = %12.8f  (exact %12.8f)\n", k, c(k+1), exact(k+1));
end

13.3 — Gradient Descent: Minimizing a Function

GNU Octave — Gradient Descent with Line Search
% Gradient descent on Rosenbrock: f(x,y) = (1-x)² + 100(y-x²)²
f  = @(v) (1-v(1))^2 + 100*(v(2)-v(1)^2)^2;
gf = @(v) [-2*(1-v(1)) - 400*v(1)*(v(2)-v(1)^2);
            200*(v(2)-v(1)^2)];

x = [-1; 1]; path = x';
for iter = 1:20000
  g = gf(x);
  if norm(g) < 1e-8, break; end
  % Backtracking line search
  alpha = 1; rho = 0.5; c1 = 1e-4;
  while f(x - alpha*g) > f(x) - c1*alpha*(g'*g)
    alpha = rho * alpha;
  end
  x = x - alpha*g;
  if mod(iter,2000)==0, path = [path; x']; end
end
printf("Rosenbrock min at (%.6f, %.6f), f=%.2e after %d iters\n", x(1), x(2), f(x), iter);

13.4 — Spectral Differentiation (Chebyshev)

GNU Octave — Chebyshev Spectral Derivative
% Spectral differentiation matrix on Chebyshev points
N = 20;
j = (0:N)'; x = cos(pi*j/N);  % Chebyshev-Lobatto points
c = [2; ones(N-1,1); 2] .* (-1).^j;
X = repmat(x, 1, N+1); dX = X - X';
D = (c * (1./c)') ./ (dX + eye(N+1));
D = D - diag(sum(D, 2));

% Test on f(x) = sin(πx): f'(x) = π cos(πx)
fv = sin(pi*x);
df_spectral = D * fv;
df_exact = pi * cos(pi*x);
printf("Chebyshev spectral diff max error: %.3e\n", max(abs(df_spectral - df_exact)));

13.5 — ODE Solvers: Runge-Kutta 4

GNU Octave — RK4 for Damped Pendulum
% θ'' + γθ' + (g/L)sin(θ) = 0  →  system: y=[θ, θ']
g = 9.81; L = 1.0; gamma = 0.5;
dydt = @(t,y) [y(2); -gamma*y(2) - (g/L)*sin(y(1))];

h = 0.01; T = 15; t = 0:h:T;
y = zeros(length(t), 2); y(1,:) = [pi/2, 0]; % initial: 90° release

for n = 1:length(t)-1
  k1 = dydt(t(n),       y(n,:)');
  k2 = dydt(t(n)+h/2,   y(n,:)' + h/2*k1);
  k3 = dydt(t(n)+h/2,   y(n,:)' + h/2*k2);
  k4 = dydt(t(n)+h,     y(n,:)' + h*k3);
  y(n+1,:) = y(n,:) + (h/6)*(k1 + 2*k2 + 2*k3 + k4)';
end

printf("Damped pendulum (γ=%.1f): final θ=%.4f rad at t=%.0f s\n", gamma, y(end,1), T);
printf("Energy dissipated: initial KE+PE = %.4f J\n", g*L*(1-cos(y(1,1))));

13.6 — Monte Carlo Integration

GNU Octave — Monte Carlo π & Gaussian Integral
% --- Monte Carlo π via quarter-circle ---
N = 1e6;
xy = rand(N, 2);
inside = sum(xy(:,1).^2 + xy(:,2).^2 <= 1);
pi_est = 4 * inside / N;
printf("Monte Carlo π (%d samples): %.6f (error: %.2e)\n", N, pi_est, abs(pi_est-pi));

% --- Gaussian integral ∫exp(-x²)dx from -∞ to ∞ = √π ---
% Using importance sampling with N(0,1) proposal
x = randn(N, 1); % samples from N(0,1)
% f(x)/g(x) where g is N(0,1) PDF and f = exp(-x²)
w = exp(-x.^2) ./ ((1/sqrt(2*pi))*exp(-x.^2/2));
I_est = mean(w);
printf("∫exp(-x²)dx ≈ %.6f (exact √π = %.6f)\n", I_est, sqrt(pi));

13.7 — Numerical PDE: Heat Equation

GNU Octave — Heat Equation (Forward Euler)
% u_t = α·u_xx on [0,1], u(0,t)=u(1,t)=0, u(x,0)=sin(πx)
alpha = 0.01; Nx = 50; dx = 1/Nx;
dt = 0.4*dx^2/alpha; % stability: dt ≤ dx²/(2α)
Nt = round(1.0/dt);
x = (1:Nx-1)'*dx; u = sin(pi*x);
r = alpha*dt/dx^2;

for n = 1:Nt
  u_new = u;
  u_new(2:end-1) = u(2:end-1) + r*(u(3:end) - 2*u(2:end-1) + u(1:end-2));
  u_new(1) = u(1) + r*(u(2) - 2*u(1));
  u_new(end) = u(end) + r*(-2*u(end) + u(end-1));
  u = u_new;
end

u_exact = exp(-alpha*pi^2*Nt*dt) * sin(pi*x);
printf("Heat eq at t=%.2f: max error = %.3e (CFL ratio r=%.3f)\n", Nt*dt, max(abs(u-u_exact)), r);

13.8 — Hilbert Transform & Analytic Signal

GNU Octave — Hilbert Transform
% Hilbert transform via FFT: H{f}(t) = (1/π) PV∫ f(τ)/(t-τ) dτ
N = 1024; t = linspace(0, 1, N);
sig = sin(2*pi*5*t) + 0.5*sin(2*pi*12*t);

F = fft(sig);
h = zeros(1, N);
h(1) = 1; h(2:floor(N/2)) = 2; h(floor(N/2)+1) = 1;
analytic = ifft(F .* h);
envelope = abs(analytic);
inst_freq = diff(unwrap(angle(analytic))) / (2*pi*(t(2)-t(1)));

printf("Analytic signal: envelope range [%.3f, %.3f]\n", min(envelope), max(envelope));
printf("Instantaneous freq range: [%.1f, %.1f] Hz\n", min(inst_freq), max(inst_freq));

13.9 — Bessel Functions & Fresnel Integrals

GNU Octave — Bessel & Fresnel via Quadrature
% Bessel J₀(x) = (1/π)∫₀^π cos(x sin θ) dθ
function val = bessel_j0_quad(x)
  val = quad(@(th) cos(x*sin(th)), 0, pi) / pi;
end

x_test = [0, 1, 2.4048, 5, 10]; % 2.4048 is first zero of J₀
printf("Bessel J₀ via quadrature:\n");
for xv = x_test
  printf("  J₀(%.4f) = %10.7f  (builtin: %10.7f)\n", xv, bessel_j0_quad(xv), besselj(0,xv));
end

% Fresnel integrals: C(x)=∫₀ˣ cos(πt²/2)dt, S(x)=∫₀ˣ sin(πt²/2)dt
function [C, S] = fresnel(x)
  C = quad(@(t) cos(pi*t.^2/2), 0, x);
  S = quad(@(t) sin(pi*t.^2/2), 0, x);
end

[C5,S5] = fresnel(5);
printf("Fresnel at x=5: C=%.6f, S=%.6f (both → 0.5 as x→∞)\n", C5, S5);

13.10 — Symbolic-Numeric Verification

GNU Octave — Verify Key Results Numerically
% Comprehensive verification of calculus identities
printf("=== CALCULUS IDENTITY VERIFICATION ===\n\n");

% 1. FTC: ∫₀^π sin(x)dx = [-cos(x)]₀^π = 2
I1 = quad(@sin, 0, pi);
printf("FTC: ∫₀^π sin(x)dx = %.10f (exact 2)\n", I1);

% 2. Euler: e^(iπ) + 1 = 0
euler = exp(1i*pi) + 1;
printf("Euler: |e^(iπ)+1| = %.2e\n", abs(euler));

% 3. Gaussian: ∫exp(-x²)dx = √π
I3 = quad(@(x)exp(-x.^2), -20, 20);
printf("Gaussian: ∫exp(-x²) = %.10f (exact √π=%.10f)\n", I3, sqrt(pi));

% 4. Basel: Σ1/n² = π²/6
n = 1:1e6; basel = sum(1./n.^2);
printf("Basel: Σ1/n² = %.10f (exact π²/6=%.10f)\n", basel, pi^2/6);

% 5. Leibniz: π/4 = 1 - 1/3 + 1/5 - 1/7 + ...
n = 0:1e6; leibniz = sum((-1).^n ./ (2*n+1));
printf("Leibniz: series = %.10f (exact π/4=%.10f)\n", leibniz, pi/4);

% 6. Stirling: n! ≈ √(2πn)(n/e)^n
n = 20;
stirling = sqrt(2*pi*n) * (n/exp(1))^n;
printf("Stirling: 20! = %.4e, approx = %.4e, ratio = %.8f\n", factorial(n), stirling, factorial(n)/stirling);

% 7. Residue: ∫₀^∞ 1/(1+x²) = π/2
I7 = quad(@(x) 1./(1+x.^2), 0, 1000);
printf("Residue check: ∫₀^∞ 1/(1+x²) = %.10f (exact π/2=%.10f)\n", I7, pi/2);

% 8. Machin's formula: π/4 = 4·arctan(1/5) - arctan(1/239)
machin = 4*4*atan(1/5) - 4*atan(1/239);
printf("Machin: 4(4·atan(1/5)-atan(1/239)) = %.15f\n        π                          = %.15f\n", machin, pi);

⚡ Quick Reference Card

Derivative Rules

RuleFormula
Powerd/dx [xⁿ] = nxⁿ⁻¹
Product(fg)′ = f′g + fg′
Quotient(f/g)′ = (f′g − fg′)/g²
Chaind/dx [f(g(x))] = f′(g(x))·g′(x)
Exponentiald/dx [eˣ] = eˣ  |  d/dx [aˣ] = aˣ ln a
Logarithmd/dx [ln x] = 1/x  |  d/dx [loga x] = 1/(x ln a)

Trig Derivatives

f(x)f′(x)f(x)f′(x)
sin xcos xarcsin x1/√(1−x²)
cos x−sin xarccos x−1/√(1−x²)
tan xsec² xarctan x1/(1+x²)
csc x−csc x cot xarcsec x1/(|x|√(x²−1))
sec xsec x tan xarccsc x−1/(|x|√(x²−1))
cot x−csc² xarccot x−1/(1+x²)

Essential Integrals

∫ f(x) dxResult + C
∫ xⁿ dxxⁿ⁺¹/(n+1)  (n≠−1)
∫ 1/x dxln|x|
∫ eˣ dx
∫ sin x dx−cos x
∫ cos x dxsin x
∫ sec² x dxtan x
∫ sec x tan x dxsec x
∫ 1/(1+x²) dxarctan x
∫ 1/√(1−x²) dxarcsin x
∫ tan x dx−ln|cos x|
∫ sec x dxln|sec x + tan x|

Key Series

FunctionMaclaurin SeriesRadius
Σ xⁿ/n!
sin xΣ (−1)ⁿ x²ⁿ⁺¹/(2n+1)!
cos xΣ (−1)ⁿ x²ⁿ/(2n)!
1/(1−x)Σ xⁿ1
ln(1+x)Σ (−1)ⁿ⁺¹ xⁿ/n1
arctan xΣ (−1)ⁿ x²ⁿ⁺¹/(2n+1)1

Fundamental Theorems

TheoremStatement
FTC Part 1d/dx ∫ₐˣ f(t)dt = f(x)
FTC Part 2∫ₐᵇ f(x)dx = F(b)−F(a) where F′=f
MVT∃c∈(a,b): f′(c) = [f(b)−f(a)]/(b−a)
Taylorf(x) = Σ f⁽ⁿ⁾(a)(x−a)ⁿ/n!
Green's∮ P dx+Q dy = ∬ (∂Q/∂x−∂P/∂y) dA
Stokes'∮ F·dr = ∬ (∇×F)·dS
Divergence∯ F·dS = ∭ ∇·F dV
Cauchyf(a) = (1/2πi) ∮ f(z)/(z−a) dz
Residue∮ f dz = 2πi Σ Res(f, zₖ)