Index exceeds the number of array elements. Index must not exceed 1
Ältere Kommentare anzeigen
I dont understand why it stops after the first iteration and gives this error:
"Index exceeds the number of array elements. Index must not exceed 1"
g=9.8
nu= 10^-6 ;
c=1/2 ;
var_Y=0.1;
I_Y= 2.8;
s= 5;
C_beta=zeros(s)
C_Y=zeros(s)
S=0:1:4
for i=1:s
C_Y= (var_Y^2)*exp(abs(-S(i)./I_Y))
C_beta= (1/ (exp(c*var_y^2)))* ( ( exp(0.5*c*(c+1)*((var_y)^2) ) -1 )^2- c*(c+1) *(exp(0.5*c*(c+1)*(var_y)^2)-1)*(exp(var_y^2)-1)+(c^2)*(exp(C_Y(i))-1) )
plot (S,C_beta)
end
Akzeptierte Antwort
Weitere Antworten (2)
Hi Sepideh,
The error "Index exceeds the number of array elements. Index must not exceed 1" is likely due to an indexing issue in your loop. Specifically, the variable S is defined as a row vector with 5 elements, but you are trying to access S(i) where i ranges from 1 to s. However, your loop should iterate over the length of S instead of s.
Additionally, there are a few other issues in your code:
- C_Y and C_beta should be updated correctly inside the loop.
- var_y should be var_Y for consistency.
- The plot should be outside the loop to avoid overwriting it in each iteration.
Here is how you can update your code:
g = 9.8;
nu = 10^-6;
c = 1/2;
var_Y = 0.1;
I_Y = 2.8;
s = 5;
C_beta = zeros(1, s);
C_Y = zeros(1, s);
S = 0:1:4;
for i = 1:length(S)
C_Y(i) = (var_Y^2) * exp(abs(-S(i) / I_Y));
C_beta(i) = (1 / (exp(c * var_Y^2))) * ...
((exp(0.5 * c * (c + 1) * (var_Y^2)) - 1)^2 - ...
c * (c + 1) * (exp(0.5 * c * (c + 1) * (var_Y^2)) - 1) * ...
(exp(var_Y^2) - 1) + ...
(c^2) * (exp(C_Y(i)) - 1));
end
plot(S, C_beta);
I hope this resolves the issue that you are facing!
Umar
am 4 Jul. 2024
0 Stimmen
Hi Sepideh,
After analyzing your code, it seems the variables C_Y and C_beta are being reassigned in each iteration without considering the previous values. This leads to incorrect indexing and array size mismatch errors.To resolve the issue and ensure proper calculation and plotting of C_beta, you need to modify the code to update specific elements of the arrays C_Y and C_beta in each iteration.
C_beta = zeros(s, 1); C_Y = zeros(s, 1);
Hopefully, making these modifications will help you achieve your desired goal.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
