You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Anylogic中Integral函数原型及积分上下限设置方法咨询

Integral Function in AnyLogic: Prototype & Limit Setup

Hey there! Let's break down everything you need to know about the Integral function in AnyLogic, from its prototype to setting upper/lower limits:

1. Integral Function/Object Prototype

AnyLogic offers two main ways to work with integrals—either via a direct expression function or a dedicated Integral object. Here's the lowdown on both:

Expression-Based integral() Function

If you want to compute an integral inline in an expression, use this built-in function with the following prototype:

double integral(double expression, double startValue, double startTime)

Let's break down each parameter:

  • expression: The mathematical expression you want to integrate (e.g., myVariable, sin(time), or a custom function like calculateRate())
  • startValue: The initial value of the integral at the start time (e.g., 0 if you're starting from scratch)
  • startTime: The simulation time when the integration should begin (e.g., 0 for the start of the simulation, or time + 5 to start 5 time units from now)

Dedicated Integral Object (Agent Library)

For more control (like pausing/resuming integration or adjusting precision), drag an Integral object from the Agent Library into your model. Its core API methods (prototype-style) include:

// Set the expression to integrate
void setExpression(double expression)

// Set the initial value of the integral
void setInitialValue(double initialValue)

// Set the time when integration starts
void setStartTime(double startTime)

// Retrieve the current integral result
double getValue()

You can also configure all these settings directly in the object's Properties panel without writing code—super handy for quick setups!

2. Setting Integral Upper & Lower Limits

How you set limits depends on whether you're integrating over time or a custom variable:

Case 1: Integrating Over Simulation Time (Most Common)

  • Lower Limit: This is controlled by the startTime parameter (either in the integral() function or the Integral object's settings). For example, setting startTime = 10 means integration starts at simulation time 10.
  • Upper Limit: To stop integration at a specific time, you have a few options:
    • Use an Event that triggers at your desired end time, then call myIntegral.stop() to halt calculations.
    • For inline expressions, wrap the integral() function in a conditional to lock the value once the upper limit is reached:
      time <= 20 ? integral(myVariable, 0, 10) : 50
      
      (This keeps the integral value fixed at 50 once simulation time passes 20.)

Case 2: Integrating Over a Non-Time Variable (Definite Integral)

AnyLogic doesn't have a direct function for definite integrals over non-time variables (e.g., integrating f(x) from x=2 to x=10), but you can work around it with numerical integration:

  • Use a loop or event to iterate your variable x from the lower limit to upper limit, accumulating the integral using methods like the trapezoidal rule:
    double integralResult = 0;
    double xStart = 2;
    double xEnd = 10;
    double step = 0.1; // Smaller step = higher precision
    for(double x = xStart; x < xEnd; x += step) {
        integralResult += f(x) * step;
    }
    
  • Alternatively, map your variable to simulation time: set x to increase from your lower to upper limit over a time interval, then integrate f(x) * dx/dt (the derivative of x with respect to time) over that time range.

Quick Pro Tip

If you're using the Integral object, you can boost integration precision by changing the solver method in its Properties panel—switch from the default Euler method to Runge-Kutta for more accurate results with complex expressions.

内容的提问来源于stack exchange,提问作者Niloofar Akbarian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 20:17:44