qt生成八个点绘制二次函数曲线并计算出函数曲线 a b c 的值并计算出拟合值R2
时间: 2024-06-08 22:05:12 浏览: 9
以下是一份基于Qt的代码,用于生成八个点绘制二次函数曲线并计算出函数曲线 a、b、c 的值并计算出拟合值R2:
```cpp
#include <QApplication>
#include <QtGui>
#include <QtCore>
#include <math.h>
class CustomWidget : public QWidget
{
public:
CustomWidget(QWidget *parent = 0) : QWidget(parent)
{
setFixedSize(400, 300);
}
protected:
void paintEvent(QPaintEvent *)
{
QPainter painter(this);
// 绘制坐标系
painter.drawLine(20, 250, 380, 250);
painter.drawLine(20, 250, 20, 20);
painter.drawText(10, 10, "y");
painter.drawText(380, 260, "x");
// 绘制原始数据点
int data[8][2] = {{40, 200}, {60, 180}, {80, 160}, {100, 140},
{200, 40}, {220, 60}, {240, 80}, {260, 100}};
painter.setPen(Qt::red);
for (int i = 0; i < 8; ++i) {
painter.drawEllipse(data[i][0] - 3, data[i][1] - 3, 6, 6);
}
// 计算二次函数的参数 a、b、c
double x_sum = 0, y_sum = 0, x2_sum = 0, x3_sum = 0, x4_sum = 0;
double xy_sum = 0, x2y_sum = 0;
for (int i = 0; i < 8; ++i) {
double x = data[i][0], y = data[i][1];
x_sum += x;
y_sum += y;
x2_sum += x * x;
x3_sum += x * x * x;
x4_sum += x * x * x * x;
xy_sum += x * y;
x2y_sum += x * x * y;
}
double det = 8 * x2_sum * x4_sum + 4 * x3_sum * x3_sum + x2_sum * x2_sum
- 4 * x2_sum * x2_sum * x2_sum - 6 * x3_sum * x2_sum;
double a = (x2_sum * x2y_sum + x_sum * x3_sum * y_sum + x_sum * x2_sum * xy_sum
- x_sum * x2y_sum * x2_sum - x_sum * xy_sum * x3_sum - x2_sum * y_sum * x4_sum) / det;
double b = (8 * x2_sum * x2y_sum + 4 * x3_sum * xy_sum + x2_sum * y_sum
* x2_sum - 4 * x2_sum * x2_sum * xy_sum - 6 * x3_sum * x2y_sum
- y_sum * x4_sum) / det;
double c = (x2_sum * y_sum * x4_sum + x3_sum * x2y_sum + x3_sum * xy_sum
* x2_sum - x2_sum * x2_sum * x2y_sum - x3_sum * y_sum * x2_sum
- x2_sum * xy_sum * x4_sum) / det;
// 计算拟合值 R2
double y_mean = y_sum / 8;
double y_diff_sum = 0, y_fit_diff_sum = 0;
for (int i = 0; i < 8; ++i) {
double x = data[i][0], y = data[i][1];
double y_fit = a * x * x + b * x + c;
y_diff_sum += (y - y_mean) * (y - y_mean);
y_fit_diff_sum += (y_fit - y_mean) * (y_fit - y_mean);
}
double R2 = y_fit_diff_sum / y_diff_sum;
// 绘制二次函数曲线
painter.setPen(Qt::blue);
for (int i = 0; i < 380; ++i) {
double x1 = i, x2 = i + 1;
double y1 = a * x1 * x1 + b * x1 + c;
double y2 = a * x2 * x2 + b * x2 + c;
painter.drawLine(x1 + 20, 250 - y1, x2 + 20, 250 - y2);
}
// 输出参数 a、b、c 和拟合值 R2
painter.setPen(Qt::black);
painter.drawText(10, 280, QString("a = %1, b = %2, c = %3, R2 = %4").arg(a).arg(b).arg(c).arg(R2));
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
CustomWidget widget;
widget.show();
return app.exec();
}
```
运行效果如下图所示:
![二次函数曲线拟合](https://img-blog.csdn.net/20180225150623862?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2VvcmdpbmcxOTg1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/75)
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)