用循环确定3个未知数

时间:2018-10-09 16:38:31

标签: matlab loops

我需要为该余弦曲线拟合一个方程。形式为y = A * cos(2 * pi *(time-d)/ T)的方程,带有3个未知数(A,d,T)y轴的数据为ydata x轴的数据为时间

现在我得到的错误是“索引超出数组范围”。有小费吗?我必须采用这种方法,因为我需要使用数学来解决这个问题。我正在寻找参数值的平均值最小。

% SOLVING FOR 3 PARAMETERS

plot(time,newy)
% Setting the intervals for the amplitude
amp_interval = 21;

% Setting the intervals for the delay
delay_interval = 61;

% Setting the intervals for the period
period_interval = 61;

% Creating a 3D matrix "list_variables"
list_variables = ones(amp_interval,delay_interval,period_interval);

% From plot, I can determine range of rate and constant values
% Set range of amplitude
amp = linspace(9,11,amp_interval);

% Set range of delay
delay = linspace(0,1,delay_interval);

% Set range of period
period = linspace(24,26,period_interval);

% Determining values of rate and constant with help of mean error

% For loop looping through the number of rate intervals
% Counting through for raterow
for   ampX=1:amp_interval;

      % Assigning the value "valuerate" corresponding to the rate for each respective
      % "raterow"
      valueamp = amp(ampX);

      % For loop looping through the number of constant intervals
      % Counting through for constcol
      for delayY = 1:delay_interval;

          % Assigning the value "valueconst" corresponding to the constant for each respective
          % "constcol"
          valuedel = delay(delayY);

           % For loop looping through the number of constant intervals
      % Counting through for constcol
      for periodZ = 1:period_interval;

          % Assigning the value "valueconst" corresponding to the constant for each respective
          % "constcol"
          valueper = period(periodZ);

          % Setting the equation for y = mx + c
          data_curve = valueamp * cos(2*pi * (time - valuedel)/ valueper );

          % Finding mean error "mean_range" - taking the modulous of
          % (predicted value - data value), and then averaging the value.
          mean_range = mean(abs(data_curve - data_model_2));

          % Replacing the "1" values of the matrix to the corresponding mean
          % error for each row and column
          list_variables(ampX,delayY,periodZ) = mean_range;

          % end of for loop looping through the number of period intervals
      end

          % end of for loop looping through the number of delay intervals
      end

      % end of for loop looping through the number of amp intervals
  end

% "lowestmeanr" is the lowest mean error in "list_variables" - and the values of corresponding rate
% and constant is wanted
lowestmeanr = min(min(min(list_variables)));

% Finding the row and column corresponding to lowest mean error
[X,Y,Z] = find(list_variables==lowestmeanr);

% Determined rate from lowest mean error "ratedeter"
ampdeter = amp(X);

% Determined constant from lowest mean error "constdeter"
deldeter = delay(Y);

% Determined constant from lowest mean error "constdeter"
perdeter = period(Z);

% Determined equation for cosine plot
data_curvedeter = ampdeter * cos(2*pi * (time - deldeter)/ perdeter );

% Plotting the determined data by mean value in red
plot(time,data_curvedeter,'r');

0 个答案:

没有答案