代码之家  ›  专栏  ›  技术社区  ›  Abhishek Bhatia

拟合n阶多项式

  •  1
  • Abhishek Bhatia  · 技术社区  · 9 年前

    我试图绘制回归曲线:

    coef_fit = polyfit(norm_dist,norm_time,7); 
    y_fit = polyval(coef_fit,xlim); 
    plot(xlim,y_fit,'r');
    

    但它总是按照我通过的顺序画一条线。

    1 回复  |  直到 9 年前
        1
  •  2
  •   Luis Mendo    9 年前

    问题是 x 您使用的值是 xlim 其是长度为2的向量。您需要定义 x(x) 具有更多值的向量:

    norm_dist = sort(5*randn(1,50) + (1:50)); %// example x values
    norm_time = 5*randn(1,50) + (1:50).^2; %// example y values
    x = linspace(min(norm_dist), max(norm_dist), 200); %// define x values for plot
    coef_fit = polyfit(norm_dist,norm_time,7); 
    y_fit = polyval(coef_fit,x); 
    plot(x,y_fit,'r');
    hold on
    plot(norm_dist, norm_time, 'b.') %// plot original points for comparison
    

    enter image description here