关于最小二乘法的c语言程序#include "stdafx.h"
【c语言实现最小二乘法函数 c语言最小二乘法拟合】#include stdio.h
#include conio.h
#include stdlib.h
#include cmath
#include iostream
using namespace std;
void polyfit(int n, double x[], double y[], int poly_n, double a[])
{
int i, j;
double *tempx, *tempy, *sumxx, *sumxy, *ata;
void gauss_solve(int n, double A[], double x[], double b[]);
tempx = new double[n];
sumxx = new double[poly_n * 21];
tempy = new double[n];
sumxy = new double[poly_n1];
ata = new double[(poly_n1)*(poly_n1)];
for (i = 0; in; i)
{
tempx[i] = 1;
tempy[i] = y[i];
}
for (i = 0; i2 * poly_n1; i)
for (sumxx[i] = 0, j = 0; jn; j)
{
sumxx[i]= tempx[j];
tempx[j] *= x[j];
}
for (i = 0; ipoly_n1; i)
for (sumxy[i] = 0, j = 0; jn; j)
{
sumxy[i]= tempy[j];
tempy[j] *= x[j];
}
for (i = 0; ipoly_n1; i)
for (j = 0; jpoly_n1; j)
ata[i*(poly_n1)j] = sumxx[ij];
gauss_solve(poly_n1, ata, a, sumxy);
delete [] tempx;
tempx = NULL;
delete [] sumxx;
sumxx = NULL;
delete [] tempy;
tempy = NULL;
delete [] sumxy;
sumxy = NULL;
delete [] ata;
ata = NULL;
}
void gauss_solve(int n, double A[], double x[], double b[])
{
int i, j, k, r;
double max;
for (k = 0; kn - 1; k)
{
max = fabs(A[k*nk]); /*find maxmum*/
r = k;
for (i = k1; in - 1; i)
if (maxfabs(A[i*ni]))
{
max = fabs(A[i*ni]);
r = i;
}
if (r != k)
for (i = 0; in; i)/*change array:A[k]A[r] */
{
max = A[k*ni];
A[k*ni] = A[r*ni];
A[r*ni] = max;
}
max = b[k];/*change array:b[k]b[r]*/
b[k] = b[r];
b[r] = max;
for (i = k1; in; i)
{
for (j = k1; jn; j)
A[i*nj] -= A[i*nk] * A[k*nj] / A[k*nk];
b[i] -= A[i*nk] * b[k] / A[k*nk];
}
}
for (i = n - 1; i = 0; x[i] /= A[i*ni], i--)
for (j = i1, x[i] = b[i]; jn; j)
x[i] -= A[i*nj] * x[j];
}
/*==================polyfit(n,x,y,poly_n,a)===================*/
/*=======拟合y=a0 a1*x a2*x^2 …… apoly_n*x^poly_n========*/
/*=====n是数据个数 x y是数据值 poly_n是多项式的项数======*/
/*===返回a0,a1,a2,……a[poly_n],系数比项数多一(常数项)=====*/
void main()
{
intn = 9, poly_n = 2;
//double x[20] = { 1 ,2,3,4,7 ,8 ,9,11,12, 13,15,15,17 ,17, 19,18 ,20,19,20, 20 },
//y[20] = { 1,3,6 ,10 ,17,25,34,45,57,70,85,100,117,134,153,171,191,210 ,230 ,250 };
double x[9]={1,3,4,5,6,7,8,9,10},
y[9]={10,5,4,2,1,1,2,3,4};
double a[50];
polyfit(n, x, y, poly_n, a);
for (int i = 0; ipoly_n1; i)/*这里是升序排列,Matlab是降序排列*/
cout"a["i"]="a[i]endl;
}
运行结果,我是拟合的2次的,你可以拟合多次 。
方程式:
0.267571*x*x-3.60531*x 13.4597=0
这个2次多项式的最低点还用我说吗,在那个区间上,自己代入;
计算方法中最小二乘法如何用C语言编程#include stdio.h
#include math.h
#define epsilon 1e-6
void nihe1(int n,int m,float sum_x,float sum_y,float sum_xy,float x2);
void nihe2(int n,int m,float sum_x,float sum_y,float sum_xy,float x2,float x2y,float x3,float x4);
int main(){
float x[100]={0.0};
float y[100]={0.0};
int n,i,flag=1;
float sum_y=0.0,sum_x=0,x2=0,sum_xy=0.0,x3=0,x4=0,x2y=0.0;
printf("请你输入需要测试的数据(先输入x[],后输入y[])的个数:");
scanf("%d",n);
for(i = 0; in; i){
scanf("%f",x[i]);}
for(i = 0; in; i){
scanf("%f",y[i]);}
for(i = 0; in; i){
sum_x= x[i];
sum_y= y[i];
sum_xy= x[i]*y[i];
x2= x[i]*x[i];
x2y= x[i]*x[i]*y[i];
x3= x[i]*x[i]*x[i];
x4= x[i]*x[i]*x[i]*x[i];}
printf("---------------请你输入的要拟合的函数------------------\n");
printf(" 1、拟合一次函数\n");
printf(" 2、拟合二次函数\n");
scanf("%d",flag);
switch(flag){
case 1:
nihe1(n,flag 1,sum_x,sum_y,sum_xy,x2); break;
case 2:
nihe2(n,flag 1,sum_x,sum_y,sum_xy,x2,x2y,x3,x4); break;
default:
printf("ERROR\n");}
return 0;}
void nihe1(int n,int m,float sum_x,float sum_y,float sum_xy,float x2){
int i,k,j;
float t,s=0;
float a[2][3] = {{(float)n,sum_x,sum_y},{sum_x,x2,sum_xy}};
n=m;
//if(m == 3)
// a[3][4] = {{n,sum_x,sum_y},{sum_x,x2,x3,sum_xy},{x2,x3,x4,x2y}};
for(k=0;kn-1;k) {
for(i=k 1;in;i)
if( abs((int)a[i][k])abs((int)a[k][k]) )
for(j=k;jn 1;j) {
t=a[k][j];
a[k][j]=a[i][j];
a[i][j]=t; }
if( abs((int)a[k][k])epsilon) {
printf("\nError,主元消去法 cann't be durable,break at %d!\n",k 1);
return; }
for(i=k 1;in;i){
a[i][k]=a[i][k] / a[k][k];
for(j=k 1;jn 1;j)
a[i][j]=a[i][j]-a[i][k] * a[k][j]; }}
a[n-1][n]=a[n-1][n] / a[n-1][n-1];
for(k=n-2;k=0;k--) {
s=0;
for(j=k 1;jn;j)
s =a[k][j]*a[j][n];
a[k][n]=( a[k][n]-s ) / a[k][k]; }
printf("\n*****The Result*****\n");
for(i=0;in;i)
printf(" x[%d]=%.4f\n",i 1,a[i][n]);
printf("函数为:p(x) = %.4f(%.4f)*x\n",a[0][n],a[1][n]);
getchar();}
void nihe2(int n,int m,float sum_x,float sum_y,float sum_xy,float x2,float x2y,float x3,float x4){
int i,k,j;
float t,s=0;
float a[3][4]=
{{(float)n,sum_x,x2,sum_y},{sum_x,x2,x3,sum_xy},{x2,x3,x4,x2y}};
n=m;
for(k=0;kn-1;k) {
for(i=k 1;in;i)
if( abs((int)a[i][k])abs((int)a[k][k]) )
for(j=k;jn 1;j) {
t=a[k][j];
a[k][j]=a[i][j];
a[i][j]=t; }
if( abs((int)a[k][k])epsilon) {
printf("\nError,主元消去法 cann't be durable,break at %d!\n",k 1);
return; }
for(i=k 1;in;i){
a[i][k]=a[i][k] / a[k][k];
for(j=k 1;jn 1;j)
a[i][j]=a[i][j]-a[i][k] * a[k][j]; } }
a[n-1][n]=a[n-1][n] / a[n-1][n-1];
for(k=n-2;k=0;k--) {
s=0;
for(j=k 1;jn;j)
s =a[k][j]*a[j][n];
a[k][n]=( a[k][n]-s ) / a[k][k]; }
printf("\n*****The Result*****\n");
for(i=0;in;i)
printf(" x[%d]=%.4f\n",i 1,a[i][n]);
printf("函数为:p(x) = %.4f(%.4f)*x(%.4f)*x*x\n",a[0][n],a[1][n],a[2][n]);
getchar();}
怎么用C语言实现最小二乘法?最小二乘法常用于根据实测数据求线性方程c语言实现最小二乘法函数的最近似解 。根据如图(图片引用于百度百科)c语言实现最小二乘法函数的描述c语言实现最小二乘法函数,利用C语言求c语言实现最小二乘法函数,使用最小二乘法算法求线性方程的解,程序如下c语言实现最小二乘法函数:
#include stdio.h
#define N 4//共有4个记录,根据需要增加记录
typedef struct Data{ //定义实验记录结构
int w; //实验次数
double x;
double y;
}DATA;
//根据d中的n个DATA记录,计算出线性方程的a,b两值
void getcs(DATA *d,int n,double a,double b){
double fi11=0,fi12=0,fi21=0,fi22=0,f1=0,f2=0;
int i;
for(i=0;in;i){
fi11 =d[i].w;
fi12 =d[i].w*d[i].x;
fi21=fi12;
fi22 =d[i].w*d[i].x*d[i].x;
f1 =d[i].w*d[i].y;
f2 =d[i].w*d[i].x*d[i].y;
}
//解一元一次方程
b=(f2*fi11/fi21-f1)/(fi22*fi11/fi21-fi12);
a=(f2*fi12/fi22-f1)/(fi21*fi12/fi22-fi11);
}
int main(){
DATA d[N]={//定义时赋初值 , 共4个记录
{2,0.1,1.1},
{1,0.2,1.9},
{1,0.3,3.1},
{1,0.4,3.9}
};
double a,b;
getcs(d,N,a,b);//计算线性方程参数a,b
printf("线性方程是:Y=%.4lf %.4lfX\n",a,b);
}
关于c语言实现最小二乘法函数和c语言最小二乘法拟合的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。
推荐阅读
- 微信外卖免费小程序,微信外卖小程序源码
- 移动端jquery框架,vue移动端框架
- thinkphp3.2.3php版本,thinkphp5023
- 巷口射击游戏,找一款射击游戏
- 高成本的菜品如何引流,高成本的菜品如何引流客户
- 乔峰为什么杀虚竹视频,乔峰和虚竹杀父之仇
- 手机怎么把照片头发p黑,手机怎么把照片头发p黑
- 八字排盘的java代码 八字排盘数据库
- 平板电视怎么调色彩度,平板电视的颜色怎么调