I got different error messages each time I run the code
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Here is the code and hopefully someone could help.
l_0=1.5;
l_1=1.6;
Lambda_min=2*(1+1)*l_0;
Lambda_max=2*(1+1)*l_1;
n_0=linspace(2,2.11,8);
n_1=linspace(2.30,2.50,8);
for i=1:10
for j=1:10
for k=1:10
l(i) = Lambda_min * ( Lambda_max/Lambda_min)^(i/10)
L=l(i)
sum(i)=sum(L)
d_0(:,j)= l(i)/((n_0(i)/n_1(i)+1))
d_1(:,k)= (n_0(i)/n_1(i))*d_0(:,j)
end
end
end
1 commentaire
Jan
le 25 Juil 2017
Modifié(e) : Jan
le 25 Juil 2017
It is very unlikely that you get different error messages running the same code. Please post the error message, because it is much easier to answer if the problem is known. Note that all the readers know is the failing code. To suggest an improvement, one has to guess what it should do. Some useful and clear comments in the code are strongly recommended - for the forum and for the debugging.
I've formatted the code using the "{} Code" button this time. You can do this by your own in the future.
Réponses (2)
Jan
le 25 Juil 2017
Modifié(e) : Jan
le 25 Juil 2017
sum(i)=sum(L)
The first time this statement is reached, the command sum is applied to the scalar L, which is l(1). Beside the fact that a sum over the scalar is the value of the scalar - so why using the sum at all? - the problem is, that you assign the output to a variable called "sum". In the next iteration "sum(L)" does not call the function, but the variable with "L" as index. This should fail, because L is not a positive integer most likely.
You could omit the two lines L=l(i), sum(i)=sum(L). Then the code still looks strange: n_0 and n_1 are defined with 8 elements, but you access them by n_0(i) and n_1(i) in the loop for i=1:10. At the 9th iteration the code must fail in consequence.
The question is, why you use a for loop over i at all, because you overwrite the former results in the assignment d_0(:,j)=... and d_1(:,k)=... in each iteration. If the code does not crash before, the result would contain only the values from i=10.
Some clear comments in the code are required to understand what the code should achieve. This would help the readers and yourself during debugging also.
0 commentaires
fs
le 25 Juil 2017
Modifié(e) : fs
le 25 Juil 2017
1 commentaire
Jan
le 27 Juil 2017
Modifié(e) : Jan
le 27 Juil 2017
Please do not post a comment in the section for answers. Who exactly is "all"?
find values of l(i) which is a vector, then take the sum of that vector
This is not clear already. Inside the loop, your l(i) is defined until i only. Do you mean the sum over l(1:i)? Or do you want to sum the elements finally after the loops? Then the result is:
sumLi = sum(Lambda_min * ( Lambda_max/Lambda_min) .^ ((1:10)/10));
Avoid overwriting by using an additional index:
d_0(:, j, i) = ...
You did not explain the problem concerning n_0 and n_1 having 8 elements only, but the loops run until 10.
It is still not clear what you want to calculate. Therefore fixing the code is not easy.
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!