简体   繁体   中英

How to find the minimum sum and the corresponding index by iterations?

I need to determine the minimum value and the corresponding index when it is achieved according to the equation that divides the time series by two parts:

方程

In this equation, n1 and n2 are the sizes of the two parts of the time series which constitute the original time series of the size n . The following additional conditions are set to the equation: the minimum size of the first part of the time series is set to be n1 = 10 for which n2 = n – n1 , and then the size is increased: n1 = 11, 12,…, k , where k = n – 10 at which n2 = 10 .

I have tried to program this equation to find the minimum sum by iterations, but I am not sure that the script is correct. In particular, are the loops correctly applied? Also, it seems like n2=11 here at the end, but it has to be 10 when the iterations finish.

n=66; % The size of the original time series for the characteristic Y (e.g., temperature)
n1=n-20; % This operation is needed for the loops below
minsum=10000000; % Declaration of the variable needed for the "if" operator at the end of the script.
for j=1:n1
    ik=10+j-1;  % This is from 10 to 55
    s31=0; % This is preallocation
    for i=1:ik  % This is from 1 to 10, 11,...,55
        s31=s31+Y(i); % The values are taken from 1 to 10,11,...,55
        cn=ik; % This is from 10 to 55
        Mean1=s31/cn; % Finding of the means from 1 to 10,11,...,55. So, the minimum size of the sample over which the mean is found is 10 years
        s32=0;
        s32=s32+(Y(i)-Mean1).^2; % This is the first term of the equation
        s41=0;
        in1=ik+1; % This is from 11 to 56
        for k=in1:n % This is from 11 to 66
            s41=s41+Y(k);
            mn=n-in1+1; % This is from 56 to 11
            Mean2=s41/mn; % Finding of the means from 66 to 11,12,...,56 or from 11,12,...,56 to 66 to be consistent with the notation in the formula. So, the minimum size of the sample over which the mean is found is 11 years
            s42=0;
            s42=s42+(Y(k)-Mean2).^2; % This is the second term of the equation
            summation=s32+s42; % Finding the sums between all possible sizes of the two parts of the time series
            if summation<minsum
                minsum=summation % The minimum sum is displayed in the last output among the iterated values.
                imin=in1 % Finding the index in the original time series for which the minimum sum is achieved.
            end
        end
    end
end

The task is actually simpler and requires a shorter code. Suppose that we have monthly temperature data for a range of years. Then, it is enough to use two 'for' loops to solve the problem:

a=load('filename.txt');
JanT=a(:,2); % Monthy temperature data for January
Years=a(:,1); % Years in the time series
n=length(JanT); % The size of the original time series
n1=n-19; % This operation is needed for the 'for' loop below
n2=n-9; % This operation is needed for the 'for' loop below

S1=[]; % Initialisation of the sum for the first term of the equation (in the vector form) 
for i=1:n1
    A=sum((JanT(1:i+9)-mean(JanT(1:i+9))).^2); % Finding of the sums from 1 to 10, 11,...56. 
S1=[S1,A]; % Writing of the obtained results into the array
end

S2=[]; % Initialisation of the sum for the second term of the equation (in the vector form)
for j=11:n2
    B=sum((JanT(j:n)-mean(JanT(j:n))).^2); % Finding of the sums from 66 to 11, 12,...57
S2=[S2,B]; % Writing of the obtained results into the array
end          

Sum=S1+S2; % Finding the sums between all possible sizes of the two parts of the time series

[minsum,imin]=min(Sum) % Finding the minimum sum and the corresponding index when it is achieved
Years=Years+10; % Shifting the years properly to find the year when the minimum sum is achieved
imin=Years(imin) % Attribution of the found index to the year in the original time series

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM