You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to define an iteration number ?
24 views (last 30 days)
Show older comments
Hi all!
i'm trying to implement a program in matlab attached, i'm wondering how to define the number of iteration j?
Answers (1)
Torsten
on 19 Nov 2022
The number of iterations is initialized as 0 :
i = 0;
Within the while loop, for each time the while loop is repeated, the number of iterations i is increased by 1:
i = i+1;
14 Comments
Image Analyst
on 19 Nov 2022
Personally I wouldn't use j or i. I'd use k as the iterator since we like to recommend not using variables that look like the imaginary variable.
Safia
on 19 Nov 2022
@Torsten @Image Analyst thank you for reply,if I have variables depending on the number of iterations and others not,how to differentiate between them for example i will calculate this in attached file,
where the number of iteration denoted by (l), i'm asking how to distinguish beween yn(l) and yn.
because both should exist when i calculate this formula.
Torsten
on 19 Nov 2022
Edited: Torsten
on 19 Nov 2022
We don't know what all these functions and variables in the picture mean.
If the old iterates are still needed in further calculations, save them all in a 2d-matrix y(n,l).
If the old iterates are no longer needed, save the y(n) in two one-dimensional arrays yold(n) and ynew(n) and override yold by ynew if a new iterate has been calculated: yold(n) = ynew(n).
Safia
on 19 Nov 2022
@Torsten in each iteration just i need the value of yl (n), it means that each time yl(n) will has new value.
my question is how to write a loop while or for in order to assign a number of iteration just because yl(n) will change.
How can i save yl(n) in one dimensional arrays and each time time it will be updated by new value?
i really need a help to solve this
Torsten
on 19 Nov 2022
Edited: Torsten
on 19 Nov 2022
I wrote it: by using two variables yold and ynew.
The following code implements the recursion y_(n+1) = y_n + 2 with y(0) = 10.
yold = 10;
i = 0;
while i < 20
i = i + 1;
ynew = yold + 2;
yold = ynew;
end
10 + 20*2
ans = 50
yold
yold = 50
I suggest you learn more about how to handle MATLAB:
Your mathematical problem is difficult enough by itself - you should try to improve your programming skills to ease this burden.
Safia
on 19 Nov 2022
Edited: Safia
on 19 Nov 2022
@Torsten i wrote my code may you could understand more, (l) is th number of iteration , i want to fix at 10.
and the first value of x_new i fix it at 0, iget this error
Index in position 1 exceeds array bounds (must not exceed 1).
x_new(1)=0 ;
l=0;
while l=10
l = l+1;
for i=1:m
for j=1:n
if N(i,j)==1 & BP(i,j)~=0
R_lb(i,j)=BP(i,j)*(log2(1+(Puissance(i,j)*(g0/n0(i,j)))/(sqrt(x_new(j)-Pos_c(i,j))^2+H^2))-(Puissance(i,j)*log2(e)*((sqrt(x(j)-Pos_c(i,j))^2)-(sqrt(x_new(j)-Pos_c(i,j))^2))*(g0/n0(i,j)))/((sqrt(x_new(j)-Pos_c(i,j))^2+H^2)*((sqrt(x_new(j)-Pos_c(i,j))^2+H^2)+(Puissance(i,j)*(g0/n0(i,j))))));
end
end
end
end
Torsten
on 19 Nov 2022
Edited: Torsten
on 19 Nov 2022
Your code will immediately throw a syntax error since
while l=10
will not be accepted by MATLAB.
Maybe you mean
while l <= 10
x_new is only defined for its first element x_new(1). So trying to access x_new(j) for 2 <= j <= n will throw an error.
Since it's not at all obvious what you want to do in the while loop, nobody will be able to help without further explanation.
Safia
on 19 Nov 2022
@Torsten i will use a solver to find in each time the optimal point x through some constraints. This solver will extract as a result a new x which is the (x+1) means the next point, so i want to use this new x to the next iteration.
That's why i'm asking how to fix a number of iteration and in each time update x by the new value resulting by the solver in order to get in the end a vector of all points.
Safia
on 19 Nov 2022
@Torsten i will try to make it easier, i want to use an optimizer to find the optimal location of drone to collect data, at first i will initialize the first point in the first iteration, after doing optimization , a new result will be given which is the next location point denoted x_new, this point will be used in the next iteration,
for example , in the first iteration , i initialize x at 0,the number of iteration is 10
iteration=10;
x=0;
for i=1:iteration
%doing optimization
%extract x_new
end
in the next iteration the program will be
x=x_new;
for i=1:iteration
%doing optimization
%extract x_new
end
until finish the number of iteration.
How can i do this in a simple loop and each time will update the new value in the next iteration.
Safia
on 19 Nov 2022
Edited: Safia
on 19 Nov 2022
@Torsten no i don't need two iteration loops, just i need the best way to write my code, i wrote above two in order to explain that i will use the result of the first iteration as input for the second one
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)