Skip to content

Instantly share code, notes, and snippets.

@aolo2
Created October 10, 2018 23:28
Show Gist options
  • Select an option

  • Save aolo2/aee89fd921b80865af0b3b53fcca75c6 to your computer and use it in GitHub Desktop.

Select an option

Save aolo2/aee89fd921b80865af0b3b53fcca75c6 to your computer and use it in GitHub Desktop.
mnk
#include <iostream>
#include <cstdint>
#include <cmath>
#include <vector>
const std::vector<float> xs = {129.0f, 240.0f, 341.0f, 434.0f, 527.0f, 618.0f};
const std::vector<float> ys = {5939.0f, 7634.0f, 10986.0f, 13862.0f, 16094.0f, 17917.0f};
const uint32_t n = 6;
static std::vector<float>
ABCD()
{
float A, B, C, D;
A = B = C = D = 0.0f;
for (uint32_t i = 0; i < n; ++i)
{
float x_i = xs[i], y_i = ys[i];
A += x_i * x_i;
B += x_i;
C += x_i * y_i;
D += y_i;
}
return {A, B, C, D};
}
static std::vector<float>
MNK()
{
auto abcd = ABCD();
float a = (abcd[2] * n - abcd[3] * abcd[1]) / (n * abcd[0] - abcd[1] * abcd[1]);
float b = (abcd[3] - a * abcd[1]) / n;
return {a, b};
}
void
MNK9()
{
/*
float x_0 = xs[0], x_n = xs[n - 1];
float y_0 = ys[0], y_n = ys[n - 1];
float x_a = (x_0 + x_n) / 2.0f;
float y_a = (y_0 + y_n) / 2.0f;
float x_g = std::sqrt(x_0 * x_n);
float y_g = std::sqrt(y_0 * y_n);
float x_h = (2 * x_0 * x_n) / (x_0 + x_n);
float y_h = (2 * y_0 * y_n) / (y_0 + y_n);
*/
std::vector<float> delta =
{
std::abs(373.5f - 11928.0f),
std::abs(282.351f - 10315.5f),
std::abs(373.5f - 10315.5f),
std::abs(252.351 - 11928.0f),
std::abs(213.446f - 11928.0f),
std::abs(373.5f - 8920.95f),
std::abs(213.446f - 8920.95f),
std::abs(213.446f - 10315.5f),
std::abs(282.351f - 8920.95f)
};
float delta_min = delta[0];
uint32_t min_i = 0;
for (uint32_t i = 1; i < delta.size(); ++i)
{
if (delta[i] < delta_min)
{
delta_min = delta[i];
min_i = i;
}
}
++min_i;
auto ab = MNK();
switch (min_i)
{
case 1:
{
std::cout << "Using y = a*x + b" << std::endl;
std::cout << "a = " << ab[0] << std::endl
<< "b = " << ab[1] << std::endl;
break;
}
case 2:
{
std::cout << "Using y = a*x^b" << std::endl;
break;
}
case 3:
{
std::cout << "Using y = a*e^(bx)" << std::endl;
break;
}
case 4:
{
std::cout << "Using y = a*ln(x) + b" << std::endl;
break;
}
case 5:
{
std::cout << "Using y = a/x + b" << std::endl;
break;
}
case 6:
{
std::cout << "Using y = 1/(ax + b)" << std::endl;
break;
}
case 7:
{
std::cout << "Using y = x(ax + b)" << std::endl;
break;
}
case 8:
{
std::cout << "Using y = a*e^(b/x)" << std::endl;
break;
}
case 9:
{
std::cout << "Using y = 1/(a*ln(x) + b)" << std::endl;
break;
}
}
}
int32_t
main(void)
{
// auto ab = MNK();
// std::cout << ab[0] << " " << ab[1] << std::endl;
MNK9();
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment