CPK、平均值、标准差、极差、最值的代码计算
时间: 2023-09-05 21:09:18 浏览: 80
下面是在C#中计算CPK、平均值、标准差、极差和最值的示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
public class DataPoint
{
public double Value { get; set; }
}
public class StatisticsCalculator
{
public double CalculateAverage(List<DataPoint> dataPoints)
{
return dataPoints.Average(dp => dp.Value);
}
public double CalculateStandardDeviation(List<DataPoint> dataPoints)
{
double average = CalculateAverage(dataPoints);
double sumOfSquares = dataPoints.Sum(dp => Math.Pow(dp.Value - average, 2));
double variance = sumOfSquares / dataPoints.Count;
return Math.Sqrt(variance);
}
public double CalculateRange(List<DataPoint> dataPoints)
{
double minValue = dataPoints.Min(dp => dp.Value);
double maxValue = dataPoints.Max(dp => dp.Value);
return maxValue - minValue;
}
public double CalculateCPK(List<DataPoint> dataPoints, double tolerance)
{
double average = CalculateAverage(dataPoints);
double standardDeviation = CalculateStandardDeviation(dataPoints);
double upperSpecLimit = average + tolerance;
double lowerSpecLimit = average - tolerance;
double cpk = Math.Min((upperSpecLimit - average) / (3 * standardDeviation),
(average - lowerSpecLimit) / (3 * standardDeviation));
return cpk;
}
}
public class Program
{
public static void Main(string[] args)
{
List<DataPoint> dataPoints = new List<DataPoint>
{
new DataPoint { Value = 10 },
new DataPoint { Value = 12 },
new DataPoint { Value = 15 },
new DataPoint { Value = 11 },
new DataPoint { Value = 13 },
new DataPoint { Value = 9 },
new DataPoint { Value = 14 },
new DataPoint { Value = 10 }
};
StatisticsCalculator calculator = new StatisticsCalculator();
double average = calculator.CalculateAverage(dataPoints);
double standardDeviation = calculator.CalculateStandardDeviation(dataPoints);
double range = calculator.CalculateRange(dataPoints);
double cpk = calculator.CalculateCPK(dataPoints, 1.5);
Console.WriteLine("Average: " + average);
Console.WriteLine("Standard Deviation: " + standardDeviation);
Console.WriteLine("Range: " + range);
Console.WriteLine("CPK: " + cpk);
}
}
```
这段代码定义了一个`DataPoint`类来表示数据点,以及一个`StatisticsCalculator`类来计算平均值、标准差、极差和CPK。在`Main`方法中,我们创建了一个包含一些示例数据点的列表,并使用`StatisticsCalculator`类来计算这些统计量的值。你可以根据自己的需要修改数据点列表和公差(tolerance)的值。运行代码后,将输出计算得到的平均值、标准差、极差和CPK的结果。
请注意,这只是一个简单的示例代码,实际应用中可能需要根据具体情况进行更复杂的处理和计算。希望对你有所帮助!