Why do I get the "Array indices must be positive integers or logical values" error?
Afficher commentaires plus anciens
I keep getting the "Array indices must be positive integers or logical values" error for the line of my code "fs_2(i) = 1 - exp((log((CS_scheil(i)/(k*C0)))/(k-1)));" that comes after the "else". How do I fix this? My entire entry of code is below.
C0 = 8;
k = 0.13;
FS = linspace(0,0.4079,100);
CS_scheil = k*C0*(1 - FS).^(k - 1);
for i = 0:0.01:1
if i == 1
fs_2(i) = 1 - exp((log((CS_scheil(i)/(k*C0)))/(k-1)));
a(i) = 1;
b(i) = (k*C0*((1 - fs_2(i))^(k - 1)));
else
fs_2(i) = 1 - exp((log((CS_scheil(i)/(k*C0)))/(k-1)));
a(i) = (fs_2(i) - fs_2(i-1));
b(i) = (1/2)*[(k*C0*((1 - fs_2(i))^(k - 1))) + (k*C0*((1 - fs_2(i-1))^(k - 1)))];
end
SUM(i) = a(i)*b(i);
end
for n = 1:1:100
if n == 1
SUM(n) = a(n)*b(n);
else
SUM(n) = SUM(n-1) + (a(n)*b(n));
end
end
C_avg = SUM;
Réponse acceptée
Plus de réponses (2)
Mario Malic
le 6 Oct 2020
Modifié(e) : Mario Malic
le 6 Oct 2020
% i = 0:0.01:1 % 101 element
i = 1 : 1 : 101 % 101 element
You should index arrays/variables with integers.
2 commentaires
Stephen23
le 6 Oct 2020
i = 0 : 1 : 100 % 101 element <- zero is not a valid MATLAB index.
Mario Malic
le 6 Oct 2020
Woopsie!
Walter Roberson
le 6 Oct 2020
for i = 0:0.01:1
i is first set to 0, then to 0.01, then about 0.02, then about 0.03, and so on.
fs_2(i) = 1 - exp((log((CS_scheil(i)/(k*C0)))/(k-1)));
and there you use i as a subscript for CS_scheil and for the output variable fs_2
First you have to ask what is meant by CS_scheil(i) . It would seem to imply the value of CS_scheil that is associated with the floating point value stored in i . But look at how you created CS_scheil :
FS = linspace(0,0.4079,100);
CS_scheil = k*C0*(1 - FS).^(k - 1);
There might be some ground for saying that you want CS_scheil(i) to mean that value of CS_scheil that is associated with FS having the same value as i, but FS runs 0 to 0.4079 while i runs 0 to 1, so when i > 0.4079 then there would be no obvious meaning to CS_scheil. Furthermore, if you ask for ismembertol(0:0.01:1, FS) you will find that the only intersection between the two, to within reasonable tolerances, is 0 .
Beyond that, you need to solve the problem of indexing with i for the output variable. For that, please read https://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22generalized+for+loop%22
Catégories
En savoir plus sur Descriptive Statistics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!