线性最小二乘法 c语言实现

公式用的出自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_linkicon-default.png?t=L9C2https://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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值