how to define an iteration number ?

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)

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

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.
@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
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).
@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
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.
@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
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.
@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.
I don't understand the workflow.
@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.
And why do you need two iteration loops with 10 iterations instead of one iteration loop with 20 iterations ?
Safia
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
I really don't understand the problem. If you save x in each of the iteration loops (maybe by overwriting a previous x), all would be fine, wouldn't it ?
@Torsten the current x used will be replaced by the output result to do the calculation in next iteration.

Sign in to comment.

Categories

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

Asked:

on 19 Nov 2022

Commented:

on 19 Nov 2022

Community Treasure Hunt

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

Start Hunting!