Prediksi Extrapolation dengan Arduino

Ekstrapolasi (Extrapolation) adalah proses matematis untuk memprediksi nilai yang akan datang berdasarkan keterkaitan matematis dari nilai-nilai sebelumnya.

Dalam contoh ini akan diprediksi nilai sinus 60° dari deret sinusional sin-0, 10, 20, 30, 40, 50. Pendekatan yang digunakan adalah linear dan polynomial (quadratic, cubic, quartic dan quintic), menggunakan operasi matrik. Formula matematis ini dikerjakan dengan bantuan arduino Uno.

dan hasilnya adalah:

  • Seharusnya (sinus(60) * 1000) = 866.02
  • Linear Extrapolation = 889.34
  • Polynomial Extrapolation (quadratic)= 869.84
  • Polynomial Extrapolation (cubic) = 865.54
  • Polynomial Extrapolation (quartic) = 904.61
  • Polynomial Extrapolation (quintic) = 866.39

Berikut sketch/koding ekstra polasi arduino:

Link download library MatrixMath

#include <MatrixMath.h>

float dataSinus[] = {0.0, 173.64, 342.02, 500.00, 642.78, 766.06};// -- 866.02

void setup() {
  Serial.begin(9600);
  Serial.println("Prediksi Extrapolation dengan Arduino");
  Serial.println("https://www.project.semesin.com/");
  Serial.println();

  byte indexTerakhir = sizeof(dataSinus) / sizeof(float) - 1;

  Serial.print("Seharusnya (sinus(60) * 1000) = ");
  Serial.println(866.02);

  float prediksi;
  prediksi = linearExtrapolation(dataSinus[indexTerakhir], dataSinus[indexTerakhir - 1]);
  Serial.print("Linear Extrapolation = ");
  Serial.println(prediksi);
  
  prediksi = polynomialExtrapolationQuadratic(dataSinus[indexTerakhir], dataSinus[indexTerakhir - 1], dataSinus[indexTerakhir - 2]);
  Serial.print("Polynomial Extrapolation (quadratic)= ");
  Serial.println(prediksi);

  prediksi = polynomialExtrapolationCubic(
    dataSinus[indexTerakhir], 
    dataSinus[indexTerakhir - 1], 
    dataSinus[indexTerakhir - 2], 
    dataSinus[indexTerakhir - 3]);
  Serial.print("Polynomial Extrapolation (cubic) = ");
  Serial.println(prediksi);

  prediksi = polynomialExtrapolationQuartic(
    dataSinus[indexTerakhir], 
    dataSinus[indexTerakhir - 1], 
    dataSinus[indexTerakhir - 2], 
    dataSinus[indexTerakhir - 3], 
    dataSinus[indexTerakhir - 4]);
  Serial.print("Polynomial Extrapolation (quartic) = ");
  Serial.println(prediksi);

  prediksi = polynomialExtrapolationQuintic(
    dataSinus[indexTerakhir], 
    dataSinus[indexTerakhir - 1], 
    dataSinus[indexTerakhir - 2], 
    dataSinus[indexTerakhir - 3], 
    dataSinus[indexTerakhir - 4], 
    dataSinus[indexTerakhir - 5]);
  Serial.print("Polynomial Extrapolation (quintic) = ");
  Serial.println(prediksi);

  while (1);
}

void loop() {

}

float linearExtrapolation(float dataN, float dataNminus1)
{
  return dataNminus1 + (2 * (dataN - dataNminus1));
}
float polynomialExtrapolationQuadratic(float dataN, float dataNminus1, float dataNminus2)
{
  float A[3][3] = {{1.0, 0.0, 0.0},
    {1.0, 1.0, 1.0},
    {1.0, 2.0, 4.0}
  };
  float B[3] = {dataNminus2, dataNminus1, dataN};
  float C[3];

  Matrix.Invert((float*)A, 3);
  Matrix.Multiply((float*)A, (float*)B, 3, 3, 1, (float*)C);

  return(C[0] + (C[1] * 3.0) + (C[2] * 9.0));
}

float polynomialExtrapolationCubic(float dataN, float dataNminus1, float dataNminus2, float dataNminus3)
{
  float A[4][4] = {
    {1.0, 0.0, 0.0, 0.0},
    {1.0, 1.0, 1.0, 1.0},
    {1.0, 2.0, 4.0, 8.0},
    {1.0, 3.0, 9.0, 27.0}
  };
  float B[4] = {dataNminus3, dataNminus2, dataNminus1, dataN};
  float C[4];

  Matrix.Invert((float*)A, 4);
  Matrix.Multiply((float*)A, (float*)B, 4, 4, 1, (float*)C);

  return(C[0] + (C[1] * 4.0) + (C[2] * 16.0) + (C[3] * 64.0));
}

float polynomialExtrapolationQuartic(float dataN, float dataNminus1, float dataNminus2, float dataNminus3, float dataNminus4)
{
  float A[5][5] = {
    {1.0, 0.0, 0.0, 0.0, 0.0},
    {1.0, 1.0, 1.0, 1.0, 1.0},
    {1.0, 2.0, 4.0, 8.0, 16.0},
    {1.0, 3.0, 9.0, 27.0, 81.0},
    {1.0, 4.0, 8.0, 64.0, 256.0}
  };
  float B[5] = {dataNminus4, dataNminus3, dataNminus2, dataNminus1, dataN};
  float C[5];

  Matrix.Invert((float*)A, 5);
  Matrix.Multiply((float*)A, (float*)B, 5, 5, 1, (float*)C);

  return(C[0] + (C[1] * 5.0) + (C[2] * 25.0) + (C[3] * 125.0) + (C[4] * 625.0));
}

float polynomialExtrapolationQuintic(float dataN, float dataNminus1, float dataNminus2, float dataNminus3, float dataNminus4, float dataNminus5)
{
  float A[6][6] = {
    {1.0, 0.0, 0.0, 0.0, 0.0, 0.0},
    {1.0, 1.0, 1.0, 1.0, 1.0, 1.0},
    {1.0, 2.0, 4.0, 8.0, 16.0, 32.0},
    {1.0, 3.0, 9.0, 27.0, 81.0, 243.0},
    {1.0, 4.0, 8.0, 64.0, 256.0, 1024.0},
    {1.0, 5.0, 25.0, 125.0, 625.0, 3125.0}
  };
  float B[6] = {dataNminus5, dataNminus4, dataNminus3, dataNminus2, dataNminus1, dataN};
  float C[6];

  Matrix.Invert((float*)A, 6);
  Matrix.Multiply((float*)A, (float*)B, 6, 6, 1, (float*)C);

  return(C[0] + (C[1] * 6.0) + (C[2] * 36.0) + (C[3] * 216.0) + (C[4] * 1296.0) + (C[5] * 7776.0));
}

hasil Serial monitor:

Leave a Reply

Your email address will not be published. Required fields are marked *