Ado1phu5 2021-03-16 20:16 采纳率: 0%
浏览 30

怎么初始化这个L呀?大佬 救救

#include<iostream>
#include<math.h>
struct abr
{
	double a;
	double b;
	double r2;
};
using namespace std;
double sum(double x[], int n, int f)
{
	double s = 0;
	for (int i = 0; i < n; i++)
	{
		s += pow(x[i], f);
	}
	return s;
}
double _sum(double x[], double y[], int n)
{
	double s = 0;
	for (int i = 0; i < n; i++)
	{
		s += x[i] * y[i];
	}
	return s;
}
void LinearFit(double x[], double y[], int n, abr L)
{
	double xsum = 0, ysum = 0, x2sum = 0, xysum=0;
	xsum = sum(x, n,1);
	ysum = sum(y, n,1);
	x2sum = sum(x,n,2);
	xysum = _sum(x, y, n);
	L.a = (n * xysum - xsum * ysum) / (n * x2sum - xsum * xsum);
	L.b = (ysum - L.a * xsum) / n;
	double yavge = ysum / n;
}
double Lf(abr L, double x)
{
	return L.a * x + L.b;
}
double lose(double x[], double y[], int n, abr L)
{
	double yavge = sum(y, n, 1) / n;
	double dysum1 = 0, dysum2 = 0;
	for (int i = 0; i < n; i++)
	{
		dysum1 += (Lf(L, x[i]) - yavge) * (Lf(L, x[i]) - yavge);
		dysum2 += (y[i] - yavge) * (y[i] - yavge);
	}
	L.r2 = dysum1 / dysum2;
	return L.r2;
}
double tf(double x)
{
	return 2*x + 4;
}
int main()
{
	struct abr L ;
	double x[100], y[100];
	for (int i = 0;i < 10;i++)
	{
		x[i] = i + 1.5;
		y[i] = tf(x[i]);
	}
	LinearFit(x, y, 10,L);
	cout << L.a << "  " << L.b << "  " << endl;
	cout <<lose(x, y, 10, L) << endl;
	system("pause");
}

  • 写回答

2条回答 默认 最新

  • Huoon 2021-03-16 22:10
    关注
    // 如下abr结构体增加一个构造函数
    struct abr
    {
        abr()
        : a(0.0)
        , b(0.0)
        , r2(0.0)
        {}
    
    	double a;
    	double b;
    	double r2;
    };
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境