Please your help is needed so that i can fprint results of a matrix(say matrix M) at each iteration of a for loop.

7 views (last 30 days)
Please your help is needed so that i can fprint results of a matrix(say matrix M) at each iteration of a for loop. Because i want to work with a particular matrix of all the iterations and i want to know at what iteration that such matrix occurred. Many thanks in advance.
  3 Comments
Peju Oyewole
Peju Oyewole on 19 Apr 2018
I want to have full representation of each matrix at each iteration. I have used 'disp' which is not give me each matrix at each iteration Thanks.
Peju Oyewole
Peju Oyewole on 20 Apr 2018
I am doing an operation to get a matrix M_C2 from If…elseif condition within a two for loop in parallel. The for loop will be running for 8760(for i=1:8760) for a specific value at each iteration. At some iterations based on the if condition, matrix M_C2 will be generated. I want any matrix generated at a particular iteration to be saved into a text file and at what iteration that matrix was generated, and I need to recall the text file to be again utilized as the primary data file for further operations within the loop. Also my two parallel for loop will be repeated for 1000 times. Also I want to know numbers of matrix generated at the end of the 8760times and save. Below is some of my code Tvals = 1:8760;%hourly iteration a4vals = [0.63 0.62 0.6 0.58 0.59 0.65 0.74 0.86.......] %hourly load demand for K = 1 : length(Tvals) T = Tvals(K); a4= a4vals(K); .........my working ........ U=rand % if conditions if U>=PRO1(J,2)+PRO1(J,3)+PRO1(J,4) counter = counter + 1 disp ('up state') elseif (PRO1(J,2)+PRO1(J,3)<=U) && (U<PRO1(J,2)+PRO1(J,3)+PRO1(J,4)) counter_1 = counter_1 + 1 M_C(J,:)=zeros(1,5)% To identify the failed component M_C2=M_C Many thanks.

Sign in to comment.

Answers (4)

Image Analyst
Image Analyst on 19 Apr 2018

Just put the name of the matrix on its own line without a semicolon in your loop

for k = 1 : whatever
    matrixName
end

It will print to the command window. Or you can use fprintf():

for row = 1 : size(m, 1)
    fprintf('%f ', m(row, :));
    fprintf('\n');
end
  7 Comments
Image Analyst
Image Analyst on 22 Apr 2018
Edited: Image Analyst on 22 Apr 2018

It might be easier if you just provided us with the full code rather than partial snippets. Attach any data that needs to be read in.

To format your code read this link

Before you paste in here, type control-a, control-i, in the MATLAB editor, to fix up your indenting.

Peju Oyewole
Peju Oyewole on 22 Apr 2018

M_C=[1.0000 10.0000 0 10.0000 0; 1.0000 10.0000 0 10.0000 0; 1.0000 76.0000 14.1000 30.0000 -25.0000; 1.0000 76.0000 14.1000 30.0000 -25.0000; 2.0000 10.0000 0 10.0000 0; 2.0000 10.0000 0 10.0000 0; 2.0000 76.0000 7.0000 30.0000 -25.0000; 2.0000 76.0000 7.0000 30.0000 -25.0000; 7.0000 80.0000 17.2000 60.0000 0; 7.0000 80.0000 17.2000 60.0000 0; 7.0000 80.0000 17.2000 60.0000 0; 13.0000 95.1000 40.7000 80.0000 0; 13.0000 95.1000 40.7000 80.0000 0; 13.0000 95.1000 40.7000 80.0000 0; 15.0000 12.0000 0 6.0000 0; 15.0000 12.0000 0 6.0000 0; 15.0000 12.0000 0 6.0000 0; 15.0000 12.0000 0 6.0000 0; 15.0000 12.0000 0 6.0000 0; 15.0000 155.0000 0.0500 80.0000 -50.0000; 16.0000 155.0000 25.2200 80.0000 -50.0000; 18.0000 400.0000 137.4000 200.0000 -50.0000; 21.0000 400.0000 108.2000 200.0000 -50.0000; 22.0000 50.0000 -4.9600 16.0000 -10.0000; 22.0000 50.0000 -4.9600 16.0000 -10.0000; 22.0000 50.0000 -4.9600 16.0000 -10.0000; 22.0000 50.0000 -4.9600 16.0000 -10.0000; 22.0000 50.0000 -4.9600 16.0000 -10.0000; 22.0000 50.0000 -4.9600 16.0000 -10.0000; 23.0000 155.0000 31.7900 80.0000 -50.0000; 23.0000 155.0000 31.7900 80.0000 -50.0000; 23.0000 350.0000 71.7800 150.0000 -25.0000; 33.0000 1.0000 2.0000 0.0030 0.0140; 34.0000 1.0000 3.0000 0.0550 0.2110; 35.0000 1.0000 5.0000 0.0220 0.0850; 36.0000 2.0000 4.0000 0.0330 0.1270; 37.0000 2.0000 6.0000 0.0500 0.1920; 38.0000 3.0000 9.0000 0.0310 0.1190; 39.0000 3.0000 24.0000 0.0020 0.0840; 40.0000 4.0000 9.0000 0.0270 0.1040; 41.0000 5.0000 10.0000 0.0230 0.0880; 42.0000 6.0000 10.0000 0.0140 0.0610; 43.0000 7.0000 8.0000 0.0160 0.0610; 45.0000 8.0000 9.0000 0.0430 0.1650; 46.0000 8.0000 10.0000 0.0430 0.1650; 47.0000 9.0000 11.0000 0.0020 0.0840; 48.0000 9.0000 12.0000 0.0020 0.0840; 49.0000 10.0000 11.0000 0.0020 0.0840; 50.0000 10.0000 12.0000 0.0020 0.0840; 51.0000 11.0000 13.0000 0.0060 0.0480; 52.0000 11.0000 14.0000 0.0050 0.0420; 53.0000 12.0000 13.0000 0.0060 0.0480; 54.0000 12.0000 23.0000 0.0120 0.0970; 55.0000 13.0000 23.0000 0.0110 0.0870; 57.0000 14.0000 16.0000 0.0050 0.0590; 58.0000 15.0000 16.0000 0.0020 0.0170; 59.0000 15.0000 21.0000 0.0060 0.0490; 60.0000 15.0000 21.0000 0.0060 0.0490; 61.0000 15.0000 24.0000 0.0070 0.0520; 62.0000 16.0000 17.0000 0.0030 0.0260; 63.0000 16.0000 19.0000 0.0030 0.0230; 64.0000 17.0000 18.0000 0.0020 0.0140; 65.0000 17.0000 22.0000 0.0140 0.1050; 66.0000 18.0000 21.0000 0.0030 0.0260; 67.0000 18.0000 21.0000 0.0030 0.0260; 68.0000 19.0000 20.0000 0.0050 0.0400; 69.0000 19.0000 20.0000 0.0050 0.0400; 70.0000 20.0000 23.0000 0.0030 0.0220; 71.0000 20.0000 23.0000 0.0030 0.0220; 72.0000 21.0000 22.0000 0.0090 0.0680]; Tvals = 1:8760;%hourly iteration a4vals = [0.63 0.62 0.6 0.58 0.59 0.65 .......] %hourly load demand for K = 1 : length(Tvals) T = Tvals(K); a4= a4vals(K); for J=1:70 J L=Component_data(:,1); U=Component_data(:,2);

        syms 's' 'l' 'u' 't'
        CompMat=[7*l -4*u -2*u -1*u;-4*l 1*l+6*u -2*l -1*u;-2*l -2*u 3*l+2*u -1*u;-1*l -1*l -1*l 3*u];
        diag_s=[s 0 0 0; 0 s 0 0; 0 0 s 0; 0 0 0 s];
        CompMat_2=diag_s+CompMat;
        D_CompMat_2=det(CompMat_2);
        F_D_CompMat_2 = factor(D_CompMat_2);
        P=inv(CompMat_2)*[1 0 0 0]';
        for i=1:3
            i
            [n, d] = numden(P(i));
            F=simplifyFraction(P(i));
            E_n = expand(n);
            F_n = factor(n);
            F_d=factor(d);
            syms s l u A B C D real
            T_deno=F_d';
            K_1= expand(A*T_deno(2)*T_deno(3));
            K_2=expand((B)*(T_deno(1))*(T_deno(3)));
            K_3=expand(((C*s)+D)*T_deno(1)*T_deno(2));
            K_1_S=subs(K_1,s,0);%When s=0 to solve for A
            K_2_S=subs(K_2,s,0);
            K_3_S=subs(K_3,s,0);
            E_n_S=subs(E_n,s,0);
            F_E_n_S=factor(E_n_S);
            K_T_S=K_1_S+K_2_S+K_3_S;
            F_K_T_S=factor(K_T_S,A);
            Mat_F_K_T_S=F_K_T_S';
            P_F_A=simplify(E_n_S/Mat_F_K_T_S(1));%Value of A
            pretty(P_F_A);
            P_F_A_D=double(subs(P_F_A,[l,u],[L(J),U(J)]));
            K_1_S_B=subs(A*T_deno(2)*T_deno(3),s,(-l-u));%When s=-l-u to solve for B
            K_2_S_B=subs(B*T_deno(1)*T_deno(3),s,(-l-u));
            K_2_S_BB=simplify(K_2_S_B);
            K_3_S_B=subs(((C*s)+D)*T_deno(1)*T_deno(2),s,(-l-u));
            E_n_SB=simplify(subs(E_n,s,(-l-u)));
            P_F_B=simplify((E_n_SB/K_2_S_BB)*B);%Value of B
            %pretty(P_F_B);
            P_F_B_D=double(subs(P_F_B,[l,u],[L(J),U(J)]));
            J_1=expand((P_F_A)*(T_deno(2)*T_deno(3)));
            J_2=expand((P_F_B)*(T_deno(1)*T_deno(3)));
            J_3=expand(((C*s)+D)*(T_deno(1)*T_deno(2)));
            J_1_C = collect(J_1,s);
            J_1_C_coeff=coeffs(J_1_C,s);
            J_1_C_coeff_Flip=fliplr(J_1_C_coeff);
            J_TT=(J_1)+(J_2)+(J_3);
            J_TT_Coll = collect(J_TT,s);
            J_TT_coeff=coeffs(J_TT_Coll,s);
            J_TT_coeff_Flip=fliplr(J_TT_coeff);
            J_TT_coeff_Flip_Transpose=J_TT_coeff_Flip';
            E_n_Coll = collect(E_n,s);
            E_n_coeff=coeffs(E_n_Coll,s);
            E_n_coeff_Flip=fliplr(E_n_coeff);
            E_n_coeff_Flip_Transpose=E_n_coeff_Flip';
            Value_C=(-1*((J_TT_coeff_Flip_Transpose(1))-C));%Value of C
            Value_C_F=simplify(Value_C);%Value of C
            Value_C_F_D=double(subs(Value_C_F,[l,u],[L(J),U(J)]));
            pretty(Value_C_F);
            Q_D=subs(J_TT_coeff_Flip_Transpose(2),C,Value_C);
            Q_D_1=(-1*((Q_D)-D))+E_n_coeff_Flip_Transpose(2);%Value of D_1
            Q_D_1_F=simplify(Q_D_1);%Value of D_1
            pretty(Q_D_1_F);
            Q_D_1_F_D=double(subs(Q_D_1_F,[l,u],[L(J),U(J)]));
            Q_D_2=(J_TT_coeff_Flip_Transpose(3))-((D*l) + (D*u));
            Q_DD_2=(-1*(Q_D_2))+E_n_coeff_Flip_Transpose(3);%Value of D_2
            Q_DD_2_F=simplify(Q_DD_2);%Value of D_2
            %pretty(Q_DD_2_F);
            Q_DD_2_F_D=subs(Q_DD_2_F,[l,u],[L(J),U(J)]);
            Final_P_L_1=ilaplace(A*(1/s));
            Final_P_L_1_D=double(subs(Final_P_L_1,A,P_F_A_D));
            Final_P_L_2=ilaplace(B*1/T_deno(2));
            Final_P_L_2_S=subs(Final_P_L_2,B,P_F_B_D);
            Final_P_L_2_S_D=double(subs(Final_P_L_2_S,[l,u,t],[L(J),U(J),T]));
            %Final_P_L_2_S_D=double(subs(Final_P_L_2,[l,u,t],[L(J),U(J),T]))
            Final_P_L_33_S=subs((C*s+D)/T_deno(3),[l,u,C,D],[L(J),U(J),Value_C_F,Q_D_1_F]);
            Final_P_L_33_S_L=ilaplace(Final_P_L_33_S);
            Final_P_L_33_S_L_S=subs(Final_P_L_33_S_L,[l,u,],[L(J),U(J)]);
            Final_P_L_33_S_L_S_D=double(subs(Final_P_L_33_S_L_S,t,T));
            Final_P_L_3=ilaplace((C*s+D)/T_deno(3));
            Final_P_L_3_D=subs(Final_P_L_3,[l,u,C,D],[L(J),U(J),Value_C_F_D,Q_D_1_F_D]);
            Final_P_L_3_W=rewrite(Final_P_L_3,'exp');
            Final_P_L_3_W_D=subs(Final_P_L_3_W,[l,u,C,D],[L(J),U(J),Value_C_F_D,Q_D_1_F_D]);
            Final_P_L_3_W_D_D=double(subs(Final_P_L_3_W_D,t,T));
            %Final_P_L_321=(Final_P_L_1)+(Final_P_L_2)+(Final_P_L_3);
            Final_P_3_S=subs(((C*s)+D)/T_deno(3),[C,D],[Value_C_F,Q_D_1_F]);
            Final_P_3_S_L=ilaplace(Final_P_3_S);
            Final_P_3_S_L_W=rewrite(Final_P_3_S_L,'exp');
            %pretty(Final_P_3_S_L_W)
            Final_P_3_S_L_W_S=subs(Final_P_3_S_L_W,[l,u,t],[L(J),U(J),T]);
            %=simplify(Final_P_3_S_L_W_S)
            Final_P_3_S_L_W_S_D=double(subs(Final_P_3_S_L_W,[l,u,t],[L(J),U(J),T]));
            For_Disp=Final_P_L_1+Final_P_L_2+Final_P_L_3;
            PRO(J,i)=(Final_P_L_1_D)+(Final_P_L_2_S_D)+(Final_P_L_3_W_D_D);
        end
    Final_P_1_L=ilaplace(P(4));
    ST_PR_4(J)=double(subs(Final_P_1_L,[l,u,t],[L(J),U(J),T]));
    ST_PR_4';
    PRO;
    PRO1=[PRO,ST_PR_4'];
    PROB_UP=PRO1(J,1);
    PROB_SMART=PRO1(J,2);
    PROB_DERATED=PRO1(J,3);
    PROB_FaILED=PRO1(J,4);
    FOR_S_UP=PRO1(J,1)+PRO1(J,2)+PRO1(J,3)+PRO1(J,4);
    FOR_UP=PRO1(J,2)+PRO1(J,3)+PRO1(J,4);
    FOR_DETD=PRO1(J,2)+ PRO1(J,3);
    FOR_SMT=PRO1(J,2);
    U=rand
        if U>=PRO1(J,2)+PRO1(J,3)+PRO1(J,4)
            counter = counter + 1
            disp ('up state')
        elseif (PRO1(J,2)+PRO1(J,3)<=U) && (U<PRO1(J,2)+PRO1(J,3)+PRO1(J,4))
            counter_1 = counter_1 + 1
            M_C(J,:)=zeros(1,5)% To identify the failed component
        elseif (PRO1(J,2)<=U)&&(U<PRO1(J,2)+ PRO1(J,3))
            counter_2 = counter_2 + 1
            disp ('smart')
        elseif (0<=U)&&(U<PRO1(J,2))
            counter_3 = counter_3 + 1 
            disp ('derated')
        end
     end
   M_C2=M_C;
   M_C2(K)
 indices1 = find(M_C2(:,2)==0)
   Line_INF=M_C2(33:end,:)
    % To get the Line data 
    Line_data=xlsread('myfileD.xlsx','sheet20','A2:B39')%Calling other Line data
    Flinedata=[Line_INF,Line_data]   %Horizontally Concatenating the matrix
    Flinedata(:,1)=1:38 %Replacing the column 1 with 1-38
    %Deleting any line that is not available
    indices = Flinedata(:,2)==0 %To check for any column with zeros
    Flinedata(indices,:) = []          %To remove any row(s)with zeros
    T_Line = array2table(Flinedata, 'VariableNames', {'Line_No', 'From_Bus', 'To_Bus','R_pu', 'X_pu','B_pu','Xmer_Tap'})
    Line_No = Flinedata(:,1);
    From_Bus = Flinedata(:,2);
    To_Bus = Flinedata(:,3);
    R = Flinedata(:,4); %Resistance between buses (transmission lines or power transformers)
    X = Flinedata(:,5); %Reactance between buses (transmission lines or power transformers)
    B_TL = Flinedata(:,6)/2;     %Susceptance of transmission lines
    a = Flinedata(:,7);          %Xmer_Tap
    Z = R + 1i*X; 
    y = 1./Z; %%To calculate inverse of each element
    size(y);
    y_2 = 1i*B_TL;
    Bus_number = max(max(From_Bus),max(To_Bus)); %Maximum number of buses
    Branch_num = length(From_Bus); %Number of Branches
    Y = zeros(Bus_number, Bus_number); %Creation of an empty admittance matrix
            %Off-diagonal elements of the admittance matrix formation
                for i=1:Branch_num
                    Y(From_Bus(i),To_Bus(i)) = Y(From_Bus(i),To_Bus(i)) -y(i)/a(i);
                    Y(To_Bus(i),From_Bus(i)) = Y(From_Bus(i),To_Bus(i));
                end    
            %Diagonal elements of the admittance matrix formation
      for m=1:Bus_number
          for n=1:Branch_num
              if From_Bus(n)==m
                 Y(m,m) = Y(m,m) + y(n)/(a(n)^2) + y_2(n);
              elseif To_Bus(n)==m
                     Y(m,m) = Y(m,m) + y(n) + y_2(n);
             end
          end
      end
 Bus_data=Bus_data1.*a4vals(K);

end Apologies for any inconveniences.

Sign in to comment.


Mini
Mini on 19 Apr 2018
Hope Peju is aware of file operation commands. Pls find the attached sample file. Check the ab.op file for the results.
  1 Comment
Peju Oyewole
Peju Oyewole on 20 Apr 2018
Please Above code is not working for me. I am doing an operation to get a matrix M_C2 from If…elseif condition within a two for loop in parallel. The for loop will be running for 8760(for i=1:8760) for a specific value at each iteration. At some iterations based on the if condition, matrix M_C2 will be generated. I want any matrix generated at a particular iteration to be saved into a text file and at what iteration that matrix was generated, and I need to recall the text file to be again utilized as the primary data file for further operations within the loop. Also my two parallel for loop will be repeated for 1000 times. Also I want to know numbers of matrix generated at the end of the 8760times and save. Below is some of my code Tvals = 1:8760;%hourly iteration a4vals = [0.63 0.62 0.6 0.58 0.59 0.65 0.74 0.86.......] %hourly load demand for K = 1 : length(Tvals) T = Tvals(K); a4= a4vals(K); .........my working ........ U=rand % if conditions if U>=PRO1(J,2)+PRO1(J,3)+PRO1(J,4) counter = counter + 1 disp ('up state') elseif (PRO1(J,2)+PRO1(J,3)<=U) && (U<PRO1(J,2)+PRO1(J,3)+PRO1(J,4)) counter_1 = counter_1 + 1 M_C(J,:)=zeros(1,5)% To identify the failed component M_C2=M_C Many thanks.

Sign in to comment.


Edik Manoukian
Edik Manoukian on 18 Apr 2020
Problem 1 Consider the feeder in the following figure. The substation transformer is connected to an infinite bus. The infinite bus voltages (i.e. the substation primary voltages) are balanced and being held at 69 kV for all power-flow problems. The substation transformer ratings are: 5000 kVA, 69 kV delta – 13.8 kV grounded Y, Z = 1.7 + j8.5 % The phase impedance matrix for a four-wire wye line between Node 2-3 is The four-wire wye feeder between Node 2-3 is 0.75 miles long. An unbalanced wye-connected load is located at node 3 and has the following values: Phase a: 650 kVA at 0.85 lagging power factor Phase b: 500 kVA at 0.90 lagging power factor Phase c: 950 kVA at 0.95 lagging power factor Assuming that the regulators are in the neutral position: 1) Determine the forward and backward sweep matrices for the substation transformer and the line segment. 2) Use the modified ladder technique to determine the line-to-ground voltages at all nodes. Use a tolerance of 0.0001 per unit. 3) Calculate all node voltages in actual values in volts and on a 120-V base.

Edik Manoukian
Edik Manoukian on 18 Apr 2020
Three type B step-voltage regulators are installed in a wye connection at the substation in order to hold the load voltages (node 3) at a voltage level of 121 V and a bandwidth of 2 V. 1) Compute the actual equivalent line impedance between nodes 2 and 3. 2) Determine a potential transformer ratio and current transformer ratio given that the compensator circuit ratings are 120V and 5A. Determine the R and X compensator settings calibrated in volts and Ohms. The settings must be the same for all three regulators. 3) For the load conditions of Problem 1 with the regulators in the neutral position, compute the voltages across the voltage relays in the compensator circuits. 4) Determine the appropriate tap settings for the three regulators to hold the node 3 voltages at 121 V in a bandwidth of 2 V. 5) With the regulators taps set, compute the actual load voltages in volts and in per unit.

Categories

Find more on Phase-Locked Loops 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!