Skip to main content
Glama

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
NameRequiredDescriptionDefault
aYesLower limit of integration
bYesUpper limit of integration
expressionYesFunction to integrate
methodNoMethod: left, right, midpoint, or trapezoidmidpoint
nYesNumber of subintervals
variableYesVariable 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}`; } };
  • 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); } );

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/nbiish/mcp-calc-tools'

If you have feedback or need assistance with the MCP directory API, please join our Discord server