公式用的出自https://blog.csdn.net/hezhefly/article/details/79517684?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.no_search_linkhttps://blog.csdn.net/hezhefly/article/details/79517684?utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~default-5.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~default-5.no_search_link
直接贴代码? 这是 linearLeastSquares.h
#ifndef LINEARLEASTSQUARES_H
#define LINEARLEASTSQUARES_H
typedef struct Sample{
float x;
float y;
} Sample;
typedef struct NodeOfInput{
Sample data;
struct NodeOfInput* next;
} NodeOfInput;
//y=ax+b;
typedef struct OutPut{
float a;
float b;
} OutPut;
OutPut getCoefficientOfLinearEquation(NodeOfInput* headOfChainList);
void initChainList(NodeOfInput** headOfChainList);
void addNewNodeToChain(NodeOfInput* headOfChainList,Sample data);
#endif // LINEARLEASTSQUARES_H
这是?linearLeastSquares.c
#include "linearLeastSquares.h"
#include <string.h>
#include <stdlib.h>
unsigned int getTheSumAndAverageSampleOfNodeInChainList(NodeOfInput* headOfChainList,\
Sample* averageSample);
float getCoefficientA( NodeOfInput* headOfChainList,unsigned int sumOfNodes, Sample* sample);
float getCoefficientB(unsigned int sumOfNodes,float coefficientOfA,Sample* sample );
OutPut getCoefficientOfLinearEquation(NodeOfInput* headOfChainList)
{
OutPut outcome={0,0};
unsigned int sumOfNodes = 0;
Sample averageSample={0,0};
sumOfNodes = getTheSumAndAverageSampleOfNodeInChainList(headOfChainList,\
&averageSample);
outcome.a = getCoefficientA( headOfChainList,sumOfNodes, &averageSample);
outcome.b = getCoefficientB(sumOfNodes,outcome.a,&averageSample );
return outcome;
}
void initChainList(NodeOfInput** headOfChainList)
{
*headOfChainList = (NodeOfInput*)malloc(sizeof (NodeOfInput));
memset(*headOfChainList,0,sizeof (NodeOfInput));
(*headOfChainList)->next = *headOfChainList;
return;
}
void addNewNodeToChain(NodeOfInput* headOfChainList,Sample data)
{
NodeOfInput* newNode = (NodeOfInput*)malloc(sizeof (NodeOfInput));
memset(newNode,0,sizeof (NodeOfInput));
newNode->data = data;
newNode->next = headOfChainList->next;
headOfChainList->next = newNode;
return;
}
unsigned int getTheSumAndAverageSampleOfNodeInChainList(NodeOfInput* headOfChainList,\
Sample* averageSample)
{
unsigned int count = 0;
float sumOfX=0,sumOfY=0;
NodeOfInput* current = headOfChainList->next;
while(current != headOfChainList )
{
sumOfX += current->data.x;
sumOfY += current->data.y;
count++;
current = current->next;
}
averageSample->x = sumOfX/(float)count;
averageSample->y = sumOfY/(float)count;
return count;
}
float getCoefficientA( NodeOfInput* headOfChainList,unsigned int sumOfNodes, Sample* sample)
{
NodeOfInput* current = headOfChainList->next;
float alpha =0,beta =0;;
while(current != headOfChainList )
{
alpha += (current->data.x - sample->x)*(current->data.y - sample->y);
beta += (current->data.x - sample->x)*(current->data.x - sample->x);
current = current->next;
}
return alpha/beta;
}
float getCoefficientB(unsigned int sumOfNodes,float coefficientOfA,Sample* sample )
{
return sample->y - sample->x*coefficientOfA;
}
这是main.c
#include <stdio.h>
#include "linearLeastSquares.h"
int main()
{
NodeOfInput* headOfChainList =NULL;
OutPut outcome;
initChainList(&headOfChainList);
int flag = 0;
Sample data;
while(1)
{
printf("if input?\n");
scanf("%d",&flag);
if(flag==0) break;
printf("please input\n");
scanf("%f %f",&(data.x),&(data.y));
addNewNodeToChain(headOfChainList,data);
}
outcome = getCoefficientOfLinearEquation( headOfChainList);
printf("a:%f b:%f \n",outcome.a,outcome.b);
return 0;
}
|