2013-07-17 97 views
0

我需要基於二維座標系上給定的一組點樣本對下一個點進行預測。如果最佳擬合直線是預測的最佳方法

我正在使用Best-Fit Straight Line方法進行此類預測。

請讓我知道是否有比Best-Fit直線更好的方法?

我的代碼如下:

public class LineEquation 
{ 
    public double m; //slope 
    public double c; //constant in y=mx+c 
} 
public class Point 
{ 
    public double x; 
    public double y; 
} 

public class BestFitLine 
{ 
    public Point[] points = new Point[7]; 

    public void InputPoints(Point[] points) 
    { 

     for (int i = 0; i < points.Length; i++) 
     { 
      points[i] = new Point(); 
     } 

     points[0].x = 12; 
     points[0].y = 13; 

     points[1].x = 22; 
     points[1].y = 23; 


     points[2].x = 32; 
     points[2].y = 33; 


     points[3].x = 42; 
     points[0].y = 23; 


     points[4].x = 52; 
     points[4].y = 33; 


     points[5].x = 62; 
     points[5].y = 63; 

     points[6].x = 72; 
     points[6].y = 63; 



    } 

    public LineEquation CalculateBestFitLine(Point[] points) 
    { 
     double constant = 0; 
     double slope=0; 
     for (int i = 0; i < points.Length - 1; i++) 
     { 
      for (int j = i + 1; j < points.Length; j++) 
      { 

       double m = (points[j].y - points[i].y)/(points[j].x - points[i].x); 
       double c = points[j].y - (m * points[j].x); 
       constant += c; 
       slope += m; 
      } 
     } 
     int lineCount =((points.Length-1)*points.Length)/2; 

     slope = slope/lineCount; 
     constant = constant/lineCount; 
     LineEquation eq = new LineEquation(); 
     eq.c = constant; 
     eq.m = slope; 
     return eq; 

    }} 
+1

請提供點的根本性質的更多信息您嘗試預測座標。當選擇最佳逼近方法時,它非常依賴於您嘗試近似的主題。 –

+1

這個問題似乎是題外話題,因爲它是關於數學建模,而不是編程。 – Servy

回答

0

如果你的x座標由的日期,您可以依靠具有以下組件的廣義相加模型: - 趨勢 - 年度配置文件 - 每週配置文件 - 日常簡檔

GAM模型中的R可用,所以我會建議你使用JRI爲了與R.接口Java代碼

乾杯