Clear Filters
Clear Filters

Determining parameters with loops

1 view (last 30 days)
Madlab
Madlab on 9 Oct 2018
Commented: Star Strider on 10 Oct 2018
I need to fit an equation for this cosine curve. Equation in the form y = A * cos(2*pi * (time - d)/T), with 3 unknowns (A,d,T) Data for y axis is newy Data for x axis is time
Right now the error I get is "Index exceeds array bounds." Any tips? I must go by this method as I need to use mathematics to solve this. I am finding the values of the parameters for the least mean error.
% 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');

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!