float sx = 0, sy = 0, sxx = 0, sxy = 0; for (int i = 0; i < sample_size; i++) { sx += sample_data[i].year; sy += sample_data[i].salary; sxx += sample_data[i].year * sample_data[i].year; sxy += sample_data[i].year * sample_data[i].salary; } float a = (sxy * sample_size - sx * sy) / (sxx * sample_size - sx * sx); float b = (sxx * sy - sx * sxy) / (sxx * sample_size - sx * sx);将它变成拟合成二次函数模型
时间: 2024-01-24 20:05:42 浏览: 139
可以将上述代码改为拟合成二次函数模型的代码如下:
```
float sx = 0, sy = 0, sxx = 0, sxy = 0, sxxy = 0, sxxx = 0;
for (int i = 0; i < sample_size; i++) {
sx += sample_data[i].year;
sy += sample_data[i].salary;
sxx += sample_data[i].year * sample_data[i].year;
sxy += sample_data[i].year * sample_data[i].salary;
sxxy += sample_data[i].year * sample_data[i].year * sample_data[i].salary;
sxxx += sample_data[i].year * sample_data[i].year * sample_data[i].year;
}
float a = (sxx * sxxy - sxy * sxxx) / (sxx * sxx - sx * sxxx);
float b = (sxx * sxy - sx * sxxy) / (sxx * sxx - sx * sxxx);
float c = (sy - a * sxx - b * sx) / sample_size;
```
其中,a、b、c 分别是二次函数模型的三个系数,即 y = ax^2 + bx + c。
阅读全文