riemann_sum
Estimate the area under a curve using Riemann sum methods. Specify function, variable, integration limits, subintervals, and method (left, right, midpoint, or trapezoid) to compute the result.
Instructions
Calculate the Riemann sum of a function using different methods
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| a | Yes | Lower limit of integration | |
| b | Yes | Upper limit of integration | |
| expression | Yes | Function to integrate | |
| method | No | Method: left, right, midpoint, or trapezoid | midpoint |
| n | Yes | Number of subintervals | |
| variable | Yes | Variable of integration |
Implementation Reference
- index.js:43-76 (handler)Core implementation of the Riemann sum calculation supporting left, right, midpoint, and trapezoid methods using mathjs for parsing and evaluation.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}`; } };
- index.js:114-123 (schema)Defines the input schema (expression, variable, a, b, n, method) and output schema (number) for the riemann_sum tool.inputSchema: z.object({ expression: z.string().describe('Function to integrate'), variable: z.string().describe('Variable of integration'), a: z.number().describe('Lower limit of integration'), b: z.number().describe('Upper limit of integration'), n: z.number().describe('Number of subintervals'), method: z.enum(['left', 'right', 'midpoint', 'trapezoid']).default('midpoint') .describe('Method: left, right, midpoint, or trapezoid') }), outputSchema: z.number(),
- index.js:110-128 (registration)Registers the 'riemann_sum' tool using ai.defineTool, including name, description, schema, and thin handler wrapper calling the core riemannSum function.ai.defineTool( { name: 'riemann_sum', description: 'Calculate the Riemann sum of a function using different methods', inputSchema: z.object({ expression: z.string().describe('Function to integrate'), variable: z.string().describe('Variable of integration'), a: z.number().describe('Lower limit of integration'), b: z.number().describe('Upper limit of integration'), n: z.number().describe('Number of subintervals'), method: z.enum(['left', 'right', 'midpoint', 'trapezoid']).default('midpoint') .describe('Method: left, right, midpoint, or trapezoid') }), outputSchema: z.number(), }, async ({ expression, variable, a, b, n, method }) => { return riemannSum(expression, variable, a, b, n, method); } );