integralset.java

来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 90 行

JAVA
90
字号
package net.sf.dz.controller;import java.util.Iterator;import java.util.NoSuchElementException;/** * Data set supporting the integration calculation. * * <p> * * The {@link DataSet#record record()} method from {@link DataSet DataSet} * class is used, however, make sure you record the right values. If this * class is used for the {@link PID_Controller PID controller}, it must be * fed with controller error, and anti-windup action must be programmed * outside of this class. * * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org"> Vadim Tkachenko</a> 2001-2002 * @version $Id: IntegralSet.java,v 1.3 2004/06/28 20:35:46 vtt Exp $ */public class IntegralSet extends DataSet {    /**     * Create the instance.     *     * @param integrationTime Integration time, milliseconds. Data elements     * older than this are expired.     */    public IntegralSet(final long integrationTime) {        super(integrationTime);    }    /**     * Get the integral starting with the first data element available and     * ending with the last data element available.     *     * <p>     *     * Integration time must have been taken care of by {@link     * DataSet#expire expiration}.     *     * @return An integral value.     */    public final double getIntegral() {        double result = 0;        long startTime = System.currentTimeMillis();        try {            // The following line will throw the NoSuchElementException if            // the dataSet is empty. It is possible to check the size, but            // handling the exception is more efficient because happens just            // once - at the startup, or when there was an interruption in            // the data sequence longer than the expiration interval and all            // the data elements were expired.            Iterator i = iterator();            Long trailerKey = (Long) i.next();            while (i.hasNext()) {                double trailerValue = get(trailerKey.longValue());                Long currentKey = (Long) i.next();                double currentValue = get(currentKey.longValue());                double diff = ((currentValue + trailerValue) / 2) * (currentKey.longValue() - trailerKey.longValue());                result += diff;                trailerKey = currentKey;            }            return result;        } catch (NoSuchElementException nseex) {            System.err.println("Ignored:");            nseex.printStackTrace();            return result;        } finally {            //System.err.println("getIntegral@" + hashCode() + "," + dataSet.size() + ": " + (System.currentTimeMillis() - startTime));        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?