I got different error messages each time I run the code

2 views (last 30 days)
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 Comment
Jan
Jan on 25 Jul 2017
Edited: Jan on 25 Jul 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.

Sign in to comment.

Answers (2)

Jan
Jan on 25 Jul 2017
Edited: Jan on 25 Jul 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.

fs
fs on 25 Jul 2017
Edited: fs on 25 Jul 2017
Thank you all for responds.
First of all; I want to find values of l(i) which is a vector, then take the sum of that vector. second, for d_0(:,j) I want to create a matrix so I can plot it later, that takes different values from l(i),n_0,n_1 each time. If I take the values for n_0 and n_1 and put in the for loop I will get index error because it should be logic or integer number.
Yes Jan my matrix is overwritten and do not know how to avoid it. Note, I want in d_0 and d_1 n_0 and n_1 to take values from linspace. for example in the first iteration n_0= 2 n_1= 2.30 then second iteration take the next value in linspace.
  1 Comment
Jan
Jan on 27 Jul 2017
Edited: Jan on 27 Jul 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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!