how to define an iteration number ?
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
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
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
yold
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.
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
on 19 Nov 2022
I don't understand the workflow.
Safia
on 19 Nov 2022
Torsten
on 19 Nov 2022
And why do you need two iteration loops with 10 iterations instead of one iteration loop with 20 iterations ?
Torsten
on 19 Nov 2022
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 ?
Safia
on 19 Nov 2022
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!