area
Compute the area under a curve defined by a mathematical expression between specified start and end points, using numerical integration for accurate results.
Instructions
Calculate the area under a curve between two points
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end | Yes | ||
| expression | Yes | ||
| n | No | Number of subintervals (default: 1000) | |
| start | Yes |
Implementation Reference
- index.js:131-146 (registration)Registration of the 'area' tool using ai.defineTool, which includes the name, description, input/output schemas, and the handler function.ai.defineTool( { name: 'area', description: 'Calculate the area under a curve between two points', inputSchema: z.object({ expression: z.string(), start: z.number(), end: z.number(), n: z.number().optional().describe('Number of subintervals (default: 1000)') }), outputSchema: z.number(), }, async ({ expression, start, end, n = 1000 }) => { return riemannSum(expression, 'x', start, end, n, 'trapezoid'); } );
- index.js:143-145 (handler)The handler function for the 'area' tool, which calls the riemannSum helper with trapezoid method to approximate the definite integral.async ({ expression, start, end, n = 1000 }) => { return riemannSum(expression, 'x', start, end, n, 'trapezoid'); }
- index.js:135-141 (schema)Input schema defines parameters for the mathematical expression, integration limits (start, end), and optional number of subintervals. Output is a number representing the area.inputSchema: z.object({ expression: z.string(), start: z.number(), end: z.number(), n: z.number().optional().describe('Number of subintervals (default: 1000)') }), outputSchema: z.number(),
- index.js:43-76 (helper)riemannSum helper function that implements various Riemann sum approximations (left, right, midpoint, trapezoid), used by the 'area' tool.const riemannSum = (expr, variable, a, b, n, method = 'midpoint') => { try { const deltaX = (b - a) / n; let sum = 0; const node = math.parse(expr); const scope = {}; if (method === 'left' || method === 'right') { const offset = method === 'right' ? 1 : 0; for (let i = 0; i < n; i++) { const x = a + (i + offset) * deltaX; scope[variable] = x; sum += math.evaluate(node, scope) * deltaX; } } else if (method === 'midpoint') { for (let i = 0; i < n; i++) { const x = a + (i + 0.5) * deltaX; scope[variable] = x; sum += math.evaluate(node, scope) * deltaX; } } else if (method === 'trapezoid') { for (let i = 0; i <= n; i++) { const x = a + i * deltaX; scope[variable] = x; const coef = (i === 0 || i === n) ? 0.5 : 1; sum += coef * math.evaluate(node, scope) * deltaX; } } return sum; } catch (e) { return `Error: ${e.message}`; } };