C#最小二乘法拟合曲线成直线的实例
作者:yangzm996 时间:2021-10-23 16:20:47
最小二乘法拟合曲线成直线
效果
拟合前
拟合后
传入X轴和Y轴的数据,得到新的Y轴数据。
将X值数据和拟合后的Y轴数据绑带即可。
/// <summary>
/// 最小二乘法/线性回归,得到新的点
/// </summary>
/// <param name="Points"></param>
/// <param name="err"></param>
/// <returns></returns>
private List<double> LinearRegression(List<double> PointsX, List<double> PointsY)
{
List<double> Result = null;
if (PointsX.Count < 2 || PointsY.Count < 2 || PointsX.Count != PointsY.Count)
{
return Result;
}
double Averagex = 0, Averagey = 0;
for (int i = 0; i < PointsX.Count; i++)
{
Averagex += PointsX[i];
Averagey += PointsY[i];
}
Averagex /= PointsX.Count;
Averagey /= PointsX.Count;
double Numerator = 0, Denominator = 0;
for (int i = 0; i < PointsX.Count; i++)
{
Numerator += (PointsX[i] - Averagex) * (PointsY[i] - Averagey);
Denominator += (PointsX[i] - Averagex) * (PointsX[i] - Averagex);
}
double K = Numerator / Denominator;
double B = Averagey - K * Averagex;
List<double> temp = new List<double>();
foreach (var item in PointsX)
{
temp.Add(K * item + B);
}
Result = temp.ToList();
return Result;
}
来源:https://blog.csdn.net/weixin_39448579/article/details/128582213
标签:C#,最小二乘法,曲线,直线
0
投稿
猜你喜欢
C# 操作符之二 算数操作符
2023-06-19 20:21:13
Android封装实现短信验证码的获取倒计时
2023-06-28 22:57:23
springboot对接支付宝支付接口(详细开发步骤总结)
2023-11-10 23:07:35
查找算法之二分查找的C++实现
2022-05-27 07:24:17
java寻找迷宫路径的简单实现示例
2021-07-06 13:17:50
Java concurrency线程池之线程池原理(四)_动力节点Java学院整理
2023-08-12 21:13:13
Spring Boot2.3 新特性分层JAR的使用
2021-08-03 12:55:50
Ribbon单独使用,配置自动重试,实现负载均衡和高可用方式
2023-05-12 00:49:15
Android自定义控件实现时间轴
2021-07-12 04:13:08
Java实现文件上传到服务器本地并通过url访问的方法步骤
2021-12-01 11:45:20
Eclipse+Webservice简单开发实例
2023-04-10 08:55:29
JDK8中新增的原子性操作类LongAdder详解
2023-06-19 22:02:58
Android实现带节点的进度条
2022-01-16 08:58:02
Java编程调用微信分享功能示例
2022-10-16 06:39:49
示例解析java面向对象编程封装与访问控制
2021-10-18 19:55:19
C# winform 请求http的实现(get,post)
2023-03-20 13:52:01
IntelliJ IDEA中查看文件内所有已声明的方法(类似eclipse的outline)
2021-08-06 00:39:39
Android实现CoverFlow效果控件的实例代码
2023-06-23 13:12:43
springboot+mybatis-plus 两种方式打印sql语句的方法
2022-12-29 13:41:11
详解OAuth2 Token 一定要放在请求头中吗
2022-05-01 09:43:15