コード例 #1
0
ファイル: Equation.cs プロジェクト: Pimpeczek/PiwotToolsLib
        /// <summary>
        /// Calculates aproximation of a integral over a given variable on a given interval.
        /// </summary>
        /// <param name="variableName">Variable to integrate over.</param>
        /// <param name="lowerBound">Lower inclusive boundry.</param>
        /// <param name="upperBound">Upper exclusive boundry.</param>
        /// <param name="step">The step of the integration.</param>
        /// <param name="mode">The mode of integration.</param>
        /// <returns></returns>
        public double Integrate(string variableName, double lowerBound, double upperBound, double step, IntegrationMode mode)
        {
            if (lowerBound > upperBound)
            {
                upperBound = lowerBound;
            }
            double           result   = 0;
            EquationVariable variable = GetVariable(variableName);

            variable.Value = lowerBound;
            if (mode == IntegrationMode.Rectangle)
            {
                while (variable.Value < upperBound)
                {
                    result         += Calculate() * step;
                    variable.Value += step;
                }
            }
            else
            {
                double val;
                while (variable.Value < upperBound)
                {
                    val             = Calculate();
                    variable.Value += step;
                    result         += (Calculate() + val) / 2 * step;
                }
            }
            return(result);
        }
コード例 #2
0
ファイル: Equation.cs プロジェクト: Pimpeczek/PiwotToolsLib
 /// <summary>
 /// Adds a new variable to variable list. Returns false if variable with the same name exists.
 /// </summary>
 /// <param name="variable">The new variable.</param>
 /// <returns></returns>
 public bool AddVariable(EquationVariable variable)
 {
     for (int i = 0; i < variables.Count; i++)
     {
         if (variables[i].Name == variable.Name)
         {
             return(false);
         }
     }
     variables.Add(variable);
     return(true);
 }