Anylogic中Integral函数原型及积分上下限设置方法咨询
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 likecalculateRate())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.,0for the start of the simulation, ortime + 5to 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
startTimeparameter (either in theintegral()function or the Integral object's settings). For example, settingstartTime = 10means 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:
(This keeps the integral value fixed at 50 once simulation time passes 20.)time <= 20 ? integral(myVariable, 0, 10) : 50
- Use an Event that triggers at your desired end time, then call
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
xfrom 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
xto increase from your lower to upper limit over a time interval, then integratef(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

