Standard Library - Calculus

Numerical differentiation and integration in Romasm

Overview

Romasm's calculus library provides numerical differentiation and integration functions. All values use fixed-point scaling by 1000 (e.g., 5000 = 5.000).

Derivative Functions

derivative_x_squared

Calculates f'(x) = 2x for f(x) = x² using the central difference method.

Method: f'(x) ≈ (f(x+h) - f(x-h)) / (2h)
Input Output
R0 = x (scaled by 1000) R0 = 2x (scaled by 1000)
Example:
; Calculate f'(5) = 2*5 = 10
LOAD R0, 5000    ; x = 5.000
CALL derivative_x_squared
PRINT R0         ; Outputs ~10000 (10.000)

derivative_x_cubed

Calculates f'(x) = 3x² for f(x) = x³.

Input Output
R0 = x (scaled by 1000) R0 = 3x² (scaled by 1000)

derivative_sin

Calculates the derivative of sin(x) where x is in degrees: d/dx sin(x) = cos(x) × (π/180).

derivative_cos

Calculates the derivative of cos(x): d/dx cos(x) = -sin(x) × (π/180).

derivative_exp

Calculates the derivative of e^x: d/dx e^x = e^x (the derivative of e^x is itself!).

derivative_ln

Calculates the derivative of ln(x): d/dx ln(x) = 1/x.

Integral Functions

integral_x_squared

Calculates the definite integral ∫[a,b] x² dx analytically using the formula: (b³ - a³) / 3

Input Output
R0 = a (lower bound, scaled)
R1 = b (upper bound, scaled)
R0 = ∫[a,b] x² dx (scaled by 1000)
Example:
; Calculate ∫[0,5] x² dx = 125/3 ≈ 41.667
LOAD R0, 0       ; a = 0
LOAD R1, 5000    ; b = 5.000
CALL integral_x_squared
PRINT R0         ; Outputs ~41667 (41.667)

integral_trapezoidal_x_squared

Numerical integration using the trapezoidal rule (more accurate than rectangular rule).

Trapezoidal Rule: ∫[a,b] f(x) dx ≈ (b-a)/2 × [f(a) + f(b)] + Σ f(x_i)

Input: R0 = a, R1 = b, R2 = Δx (all scaled by 1000)

integral_simpson_x_squared

Numerical integration using Simpson's rule (most accurate).

Simpson's Rule: ∫[a,b] f(x) dx ≈ (b-a)/6 × [f(a) + 4f((a+b)/2) + f(b)]

Input: R0 = a, R1 = b, R2 = Δx (all scaled by 1000)

integral_sin

Definite integral of sin(x) from a to b (x in degrees).

integral_cos

Definite integral of cos(x) from a to b (x in degrees).

integral_exp

Definite integral of e^x: ∫[a,b] e^x dx = e^b - e^a

integral_ln

Definite integral of 1/x: ∫[a,b] 1/x dx = ln(b) - ln(a)

Numerical Methods

Romasm uses several numerical methods for calculus operations:

Central Difference (Derivatives)

f'(x) ≈ (f(x+h) - f(x-h)) / (2h)

More accurate than forward difference, uses values on both sides of x.

Rectangular Rule (Integration)

∫[a,b] f(x) dx ≈ Σ f(x_i) × Δx

Simplest method, approximates area as rectangles.

Trapezoidal Rule (Integration)

More accurate than rectangular rule, uses trapezoids instead of rectangles.

Simpson's Rule (Integration)

Most accurate numerical integration method, uses parabolic approximations.

Related Documentation