Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Contoh regresi linier dan regresi kuadratik pada C++
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// ==================
// CONTOH DATA (X, Y)
// ==================
const int n = 6;
float data[n][2] = {
{ 0, 2.1 },
{ 1, 7.7 },
{ 2, 13.6 },
{ 3, 27.2 },
{ 4, 40.9 },
{ 5, 61.1 }
};
// only support 3x3 for now
void printGaussMatrix (float X[3][3], float Y[3]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout.width(10);
cout << right << X[i][j];
}
cout << " | ";
cout.width(10);
cout << right << Y[i] << '\n';
}
}
int main () {
cout.precision(2);
cout.setf(ios::fixed);
float sumX = 0, sumX2 = 0, sumX3 = 0, sumX4 = 0;
float sumY = 0, sumXY = 0, sumX2Y = 0;
for (int i = 0; i < n; ++i) {
sumX += data[i][0];
sumX2 += pow(data[i][0], 2);
sumX3 += pow(data[i][0], 3);
sumX4 += pow(data[i][0], 4);
sumY += data[i][1];
sumXY += data[i][0] * data[i][1];
sumX2Y += pow(data[i][0], 2) * data[i][1];
}
float X[3][3] = {
n, sumX, sumX2,
sumX, sumX2, sumX3,
sumX2, sumX3, sumX4
};
float Y[3] = {
sumY,
sumXY,
sumX2Y
};
cout << "Iter# " << 0 << ":" << endl;
printGaussMatrix(X, Y);
cout << endl;
// ELIMINASI GAUSS
const int XROWS = 3;
const int XCOLS = 3;
const int YROWS = 3;
for (int i = 0; i < XCOLS - 1; ++i) {
for (int j = i + 1; j < XROWS; ++j) {
float d = X[j][i] / X[i][i];
for (int k = 0; k < XCOLS; ++k) {
X[j][k] = X[j][k] - X[i][k] * d;
}
Y[j] = Y[j] - Y[i] * d;
cout << "Iter# "
<< i + 1
<< ","
<< j + 1
<< " pembagi: "
<< d
<< endl;
printGaussMatrix(X, Y);
cout << endl;
}
}
// SUBSTITUSI MUNDUR
float A[3]; // i => 0, 1, 2
float s;
for (int i = 2; i >= 0; i--) {
s = 0;
if (i < 2) {
for (int j = i + 1; j <= 2; ++j) {
s += X[i][j] * A[j];
}
}
A[i] = (Y[i] - s) / X[i][i];
cout << "A"
<< i
<< " = "
<< A[i]
<< endl;
}
float totalErrorY = 0;
for (int i = 0; i < n; ++i) {
float prediction = A[0]
+ A[1] * data[i][0]
+ A[2] * pow(data[i][0], 2);
totalErrorY += abs(data[i][1] - prediction);
}
float meanAbsErrorY = totalErrorY / n;
cout << "y = "
<< A[0]
<< " + "
<< A[1]
<< "x"
<< " + "
<< A[2]
<< "x2"
<< ", err ~"
<< meanAbsErrorY
<< endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
// ========================
// DATA PERTUMBUHAN TANAMAN
// ========================
const int n = 7;
int data[n][2] = {
{ 1, 2 },
{ 3, 5 },
{ 7, 12 },
{ 9, 14 },
{ 11, 15 },
{ 20, 20 },
{ 22, 25 }
};
int main () {
int sumXY = 0, sumX = 0, sumY = 0, sumX2 = 0;
for (int i = 0; i < n; ++i) {
sumXY += data[i][0] * data[i][1];
sumX += data[i][0];
sumY += data[i][1];
sumX2 += pow(data[i][0], 2);
}
float avgY = sumY / n;
float avgX = sumX / n;
float a1 = (n * sumXY - sumX * sumY)
/ (n * sumX2 - pow(sumX, 2));
float a0 = avgY - a1 * avgX;
float totalErrorY = 0;
for (int i = 0; i < n; ++i) {
float prediction = a0 + a1 * data[i][0];
totalErrorY += abs(data[i][1] - prediction);
}
float meanAbsErrorY = totalErrorY / n;
cout << "y = "
<< a0
<< " + "
<< a1
<< "x"
<< ", err ~"
<< meanAbsErrorY
<< endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment