2

以下のコードがあります。

for k=40:10:80
 T(k)=273.15+k;
 z=[0.2 0.2 0.2 0.4];
 W_PR=0.245;
 C=4;
 omega=[0.344 0.467 0.578 0.789];
 Tc=[600 700 500 570];
 Pc=[50 70 58 76];
    for c=1:C

        x_PR(1,c)=z(c)/(1+W_PR*(K_PR(c)-1));
        x_PR(2,c)=K_PR(c)*x_PR(1,c);
    end
    for c=1:C
        kappa_PR=0.37464+1.54226.*omega(c)-0.26992.*omega(c).^2;
        alpha_PR=(1+kappa_PR.*(1-sqrt(T(k)./Tc(c)))).^2;
        a_PR(c,c)=0.45724.*R.^2.*Tc(c).^2./Pc(c).*alpha_PR;
        b_PR(c)=0.07780*R.*Tc(c)./Pc(c);
    end
    for c=2:C
        for n=1:(c-1)
            a_PR(c,n)=sqrt(a_PR(c,c).*a_PR(n,n));
            a_PR(n,c)=a_PR(c,n);
        end
    end
    for c=1:C
        A_PR(c,c)=a_PR(c,c).*P./(R.*T(k)).^2;
        B_PR(c)=b_PR(c).*P./(R.*T(k));
    end
    for c=1:C
        Z(c,c)=A_PR(c,c)./5;
        V(c)=B_PR(c).*6;
    end
end

コードを実行するたびに、各 T(k) での Z と V の結果が必要です。そのままのコードでは、1 つの T 値の結果のみが得られますが、ループを実行して、すべての T(k) の Z と V の結果を得る必要があります。

4

2 に答える 2

2

メイン ループの外でいくつかのパラメーターを定義して、少なくともストレージを事前に割り当てたい場合があります。

Z_store = ones(C,C,5); % if you want to use a 3d matrix

V_store = ones(5,C);

新しい機能:

function [Z_store V_store] = SO_test1()
  z=[0.2 0.2 0.2 0.4];
  W_PR=0.245;
  C=4;
  omega=[0.344 0.467 0.578 0.789];
  Tc=[600 700 500 570];
  Pc=[50 70 58 76];
  R=8.314; 
  P=20; 
  Z_store = ones(C,C,5);
  V_store = ones(5,C);
  K_PR=[1.546e-2, 0.456, 1.432e2, 14.32];
  iter = 0;

for k=40:10:80

T(k)=273.15+k;
  for c=1:C
      x_PR(1,c)=z(c)/(1+W_PR*(K_PR(c)-1));
      x_PR(2,c)=K_PR(c)*x_PR(1,c);  
      kappa_PR=0.37464+1.54226.*omega(c)-0.26992.*omega(c).^2;
      alpha_PR=(1+kappa_PR.*(1-sqrt(T(k)./Tc(c)))).^2;
      a_PR(c,c)=0.45724.*R.^2.*Tc(c).^2./Pc(c).*alpha_PR;
      b_PR(c)=0.07780*R.*Tc(c)./Pc(c);
  end
  for c=2:C
      for n=1:(c-1)
          a_PR(c,n)=sqrt(a_PR(c,c).*a_PR(n,n));
          a_PR(n,c)=a_PR(c,n);
      end
  end
  for c=1:C
      A_PR(c,c)=a_PR(c,c).*P./(R.*T(k)).^2;
      B_PR(c)=b_PR(c).*P./(R.*T(k)); 
      Z(c,c)=A_PR(c,c)./5;
      V(c)=B_PR(c).*6;
  end
  iter = iter + 1;
  Z_store(:,:,iter) = Z;
  V_store(iter,:) = V;

  end
end
于 2012-09-02T09:01:07.197 に答える
0

Your result matrices V and Z need to have higher dimension. From your code you only assign values to Z in the form

 Z(c,c) = value

and V in the form

 V(c) = value

thus it appears that your Z is simply a 2d matrix who only ever has diagonal entries defined and V is just a simple vector. Your iteration runs over all values of your loop variable k but each time you are overwriting the current values within these arrays. Consider making V a 2D array and Z a 3D array so that they have the capacity to store the results at a separate index for each value of the iteration variable k

于 2012-09-02T11:03:23.590 に答える