拟合二元多次曲线
来源:互联网 发布:GOOgle keep软件下载 编辑:程序博客网 时间:2024/04/28 23:57
数学库MathNet.Iridium
///<summary>
///用最小二乘法拟合二元多次曲线///例如:y=a0+a1*x 返回值则为a0 a1
///例如:y=a0+a1*x+a2*x*x 返回值则为a0 a1 a2
///</summary>
///<param name="arrX">已知点的x坐标集合</param>
///<param name="arrY">已知点的y坐标集合</param>
///<param name="length">已知点的个数</param>
///<param name="dimension">方程的最高次数</param>
public static double[] MultiLine(double[] arrX, double[] arrY, int length, int dimension)//二元多次线性方程拟合曲线
{
int n = dimension + 1; //dimension次方程需要求 dimension+1个 系数
double[,] Guass = new double[n, n + 1]; //高斯矩阵 例如:y=a0+a1*x+a2*x*x
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < n; j++)
{
Guass[i, j] = SumArr(arrX, j + i, length);
}
Guass[i, j] = SumArr(arrX, i, arrY, 1, length);
}
return ComputGauss(Guass, n);
}
public static double SumArr(double[] arr, int n, int length) //求数组的元素的n次方的和
{
double s = 0;
for (int i = 0; i < length; i++)
{
if (arr[i] != 0 || n != 0)
s = s + Math.Pow(arr[i], n);
else
s = s + 1;
}
return s;
}
public static double SumArr(double[] arr1, int n1, double[] arr2, int n2, int length)
{
double s = 0;
for (int i = 0; i < length; i++)
{
if ((arr1[i] != 0 || n1 != 0) && (arr2[i] != 0 || n2 != 0))
s = s + Math.Pow(arr1[i], n1) * Math.Pow(arr2[i], n2);
else
s = s + 1;
}
return s;
}
public static double[] ComputGauss(double[,] Guass, int n)
{
int i, j;
int k, m;
double temp;
double max;
double s;
double[] x = new double[n];
for (i = 0; i < n; i++) x[i] = 0.0;//初始化
for (j = 0; j < n; j++)
{
max = 0;
k = j;
for (i = j; i < n; i++)
{
if (Math.Abs(Guass[i, j]) > max)
{
max = Guass[i, j];
k = i;
}
}
if (k != j)
{
for (m = j; m < n + 1; m++)
{
temp = Guass[j, m];
Guass[j, m] = Guass[k, m];
Guass[k, m] = temp;
}
}
if (0 == max)
{
// "此线性方程为奇异线性方程"
return x;
}
for (i = j + 1; i < n; i++)
{
s = Guass[i, j];
for (m = j; m < n + 1; m++)
{
Guass[i, m] = Guass[i, m] - Guass[j, m] * s / (Guass[j, j]);
}
}
}//结束for (j=0;j<n;j++)
for (i = n - 1; i >= 0; i--)
{
s = 0;
for (j = i + 1; j < n; j++)
{
s = s + Guass[i, j] * x[j];
}
x[i] = (Guass[i, n] - s) / Guass[i, i];
}
return x;
}//返回值是函数的系数
static void Main(string[] args)
{
double[] x = { 1, 2, 3, 4, 5 };
double[] y={2,4,6,8,10};
MultiLine(x,y,5,1);
}
0 0
- 拟合二元多次曲线
- 最小二乘法拟合二元多次曲线
- 实现曲线的光滑拟合,多次趋近直线
- 拟合曲线
- 最小二乘法拟合曲线
- MATLAB拟合曲线
- 多项式拟合曲线
- 使用G2O拟合曲线
- tensorflow拟合曲线
- g2o拟合曲线
- TensorFlow拟合曲线
- Matlab拟合曲线
- python拟合二元一次函数
- 用最小二乘法构造拟合曲线
- 最小二乘法求多项式拟合曲线
- 【matlab】拟合曲线 polyfit set
- Matlab拟合曲线小例子
- 最小二乘法拟合多项式曲线原理
- mac python链接数据库
- 551nod-【1138 连续整数的和】
- 补充:MongoDB的理论知识
- js作业--自己写
- UVAlive6911 思维题set
- 拟合二元多次曲线
- 面向对象(一)
- SQL 数据库 学习 030 查询-13 --- 查询语句的顺序
- JUnit java 单元测试基于白盒在 Eclipse 内的使用
- 聚簇索引与非聚簇索引的区别以及SQL Server查询优化技术
- Andriod 学习 VideoView + MediaController 来实现播放视频
- Service 启动流程源码解析
- Mysql数据库表分区存储到指定磁盘路径
- Unity3d - Awake()、OnEnable()、Start()等默认函数的执行顺序和生命周期