area
Calculate the area under a curve between two points using numerical integration. Input a mathematical expression and integration bounds to compute the enclosed region.
Instructions
Calculate the area under a curve between two points
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | ||
| start | Yes | ||
| end | Yes | ||
| n | No | Number of subintervals (default: 1000) |
Implementation Reference
- index.js:143-145 (handler)Handler function for the 'area' tool. It invokes the riemannSum helper with the trapezoid method to approximate the area under the curve (definite integral).async ({ expression, start, end, n = 1000 }) => { return riemannSum(expression, 'x', start, end, n, 'trapezoid'); }
- index.js:135-141 (schema)Input and output schema for the 'area' tool. Defines parameters: expression (string), start/end (numbers), optional n (subintervals). Outputs a number.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:131-146 (registration)Registration of the 'area' tool via ai.defineTool, including name, description, schema, and inline handler.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:43-76 (helper)riemannSum helper function that performs numerical integration using left, right, midpoint, or trapezoid Riemann sums. Used by the 'area' handler with 'trapezoid' method.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}`; } };