八度线图不跟随数据点

时间:2018-11-25 14:38:33

标签: octave

我正在尝试绘制适合数据集的函数,但是折线图无法正确连接函数图上的点:(green points are original data, blue circles are points on the function and blue line should be connecting the blue circles.)从开始将毫秒转换为纪元后,问题似乎已经开始迄今为止(x轴)。

%convert millis since epoch to days since 0000
  timeVec = zeros(size(x,1), 1);
  f = "ddd mmm dd HH:MM:SS yyyy";

  for i = 1:size(x,1)
    timeTmp = ctime(x(i)/1000);
    timeVec(i) = datenum(timeTmp(1:end-1), f);
  endfor
  %

  %convert millis since epoch to days since 0000 (now for the training set examples)
  timeVecXX = zeros(size(XX,1), 1);

  for i = 1:size(XX,1)
    timeTmp = ctime(XX(i)/1000);
    timeVecXX(i) = datenum(timeTmp(1:end-1), f);
  endfor
  %

  hold("off");
  plot(timeVec, plotFunc(x), '-ob');
  datetick("ddd mmm dd");
  hold("on");
  grid on;
  plot(timeVecXX,yy, '.g');

MCVE:

optimTheta = [8.0916e+004; -3.4102e+003; 7.5091e+003];
 optimA = 78250000;
 mu = [1.5431e+012, 5.8217e-003];
 s = [2.4831e+007, 7.1022e-001];

  plotFunc = @(p) predict(p,optimTheta,mu,s,optimA);

  linZero = -(optimTheta(1)*s(1)-optimTheta(2)*mu(1))/optimTheta(2)   %solution for the equation theta0 + theta1*(x-mu(1))/s(1) = 0

  x = [1543076107026:(linZero-1543076107026)/100:linZero];
  x = x';

  %convert millis since epoch to days since 0000
  timeVec = zeros(size(x,1), 1);
  f = "ddd mmm dd HH:MM:SS yyyy ";

  for i = 1:size(x,1)
    timeTmp = ctime(x(i)/1000);
    timeVec(i) = datenum(timeTmp, f);
  endfor
  %

  hold("on");
  plot(timeVec, plotFunc(x), 'o-b');
  datetick("ddd mmm dd");
  grid on;
  xlabel ("Day");
  hold("off");

function [y] = predict (X, theta, mu, s, a)

  Xtemp = [X, sin((X-a)/(1000*60*60*24/(2*pi)))];
  Xtemp = (Xtemp-mu)./s;
  Xtemp = [ones(size(Xtemp,1),1), Xtemp];

  y = Xtemp*theta;

endfunction

1 个答案:

答案 0 :(得分:1)

让它像这样工作:

 optimTheta = [8.0916e+004; -3.4102e+003; 7.5091e+003];
 optimA = 78250000;
 mu = [1.5431e+012, 5.8217e-003];
 s = [2.4831e+007, 7.1022e-001];

 plotFunc = @(p) predict(p,optimTheta,mu,s,optimA);

 linZero = -(optimTheta(1)*s(1)-optimTheta(2)*mu(1))/optimTheta(2);  %solution for the equation theta0 + theta1*(x-mu(1))/s(1) = 0

 tOffset = 1543076107026;

 x = [1543076107026:(linZero-1543076107026)/100:linZero];
 x = x';

 timeVec = x-tOffset;

 xTicks = [-65707026:(24*60*60*1000):539092974];

 hold("on");
 set(gca, 'xtick', xTicks);
 plot(timeVec, plotFunc(x), '-ob');

 lTVec = [];
 for i=1:size(xTicks,2)
 lTVec = [lTVec; strftime("%a %b %d", localtime((xTicks(i)+tOffset)/1000))];
 endfor


 set(gca, 'xticklabel', lTVec);
 grid on;
 xlabel ("Day");
 hold("off");

graph