0

I wrote a code like this

clc;
clear;
clf;
A = NaN(141, 7);
ind = [1, 41, 141];
nind = setdiff(1:141, ind);
A(ind, :) = [1.46, 1.117, 0.945, 0.805, 0.675, 0.613, 0.555;
             0.877, 0.687, 0.605, 0.535, 0.463, 0.423, 0.385; 
             0.52, 0.425, 0.405, 0.37, 0.325, 0.315, 0.28];

for k = 1:7;
  A(nind, k) = interp1(ind, A(ind, k), nind, 'pchip');

end


  B = NaN(141, 61);
  inde = [1, 11, 21, 31, 41, 51, 61];
  ninde = setdiff(1:61, inde);
  B(:, inde) = A;
  for n = 1:141;
  B(n, ninde) = interp1(inde, B(n, inde), ninde, 'pchip');
  end

  for x = 30:90
  xi = x./100;
  XM = x.-29;
  for P = 1:0.1:15
   PM = P.*10-9;
   ASU = (68.070476/xi)*((xi-0.23)*(0.11+0.6998*log(P))+(1.23-xi)*0.7734*log(P));
   CSU = B(PM,XM);
   RCV = (0.00004*P.^5-0.004*P.^4+0.146*P.^3-2.5525*P.^2+21.396*P+806.87)*1000; 
   R = (RCV-ASU-CSU)/874590*100;
   fprintf('Pressure %f bar Concentration %f percent Efficiency %f percent\n', P, x, R)
 end
end

and it shows error of 'subscript indices must be either positive integers less than 2^31 or logicals'. Octave suggests that the problem is here.

CSU = B(PM,XM);

How can I solve this problem?

Filburt
  • 17,626
  • 12
  • 64
  • 115
V150
  • 13
  • 3
  • Use the script debugger and check what the actual value of the index variable is. It's much more likely that you have a zero (breaks "positive" requirement) or a fraction (breaks "integer" requirement) than exceeding 2^31. – Ben Voigt Jun 12 '17 at 17:47
  • I actually checked, but the index doesn't exceed 2^31 or go below zero, etc. – V150 Jun 12 '17 at 17:49
  • 2
    Please take a look here (possible duplicate): [Why is 24.0000 not equal to 24.0000 in MATLAB?](https://stackoverflow.com/q/686439/52738) Your problem is that `PM` does not have exact integer values due to floating point representation round off errors. – gnovice Jun 12 '17 at 17:50
  • what is strange is that it goes well until At 1.000000 by 1.000000 Pressure 1.000000 bar Concentration 30.000000 pct Efficiency 94.427370 pct At 1.000000 by 2.000000 Pressure 1.100000 bar Concentration 30.000000 pct Efficiency 94.614140 pct At 1.000000 by 3.000000 Pressure 1.200000 bar Concentration 30.000000 pct Efficiency 94.796275 pct At 1.000000 by 4.000000 Pressure 1.300000 bar Concentration 30.000000 pct Efficiency 94.973836 pct...... At 1.000000 by 14.000000 Pressure 2.300000 bar Concentration 30.000000 pct Efficiency 96.513444 pct but then stops. It's At XM by PM. – V150 Jun 12 '17 at 17:55
  • 1
    @V150: I think you just need to do `PM = round(P.*10-9);` The linked duplicate explains these sorts of perils of floating point operations. – gnovice Jun 12 '17 at 17:57
  • 1
    @V150 Just pointing out what gnovice is saying further: `octave:1> fprintf('%0.20f\n', PM); % returns 15.00000000000000355271`. Hence octave complains that you're not using an 'integer' as an index, and it doesn't know what to do with a value of 15.00000000000000355271 passed as an index. Also, can I ask when you post a question under the matlab tag, please ensure that it is valid matlab code (there is no `.-` operator in matlab; this is specific to octave, as octave supports "broadcasting"). – Tasos Papastylianou Jun 12 '17 at 19:08

0 Answers0