how to define an iteration number ?
28 次查看(过去 30 天)
显示 更早的评论
Safia
2022-11-19
Hi all!
i'm trying to implement a program in matlab attached, i'm wondering how to define the number of iteration j?
回答(1 个)
Torsten
2022-11-19
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 个评论
Image Analyst
2022-11-19
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
2022-11-19
@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
2022-11-19
编辑:Torsten
2022-11-19
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
2022-11-19
@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
2022-11-19
编辑:Torsten
2022-11-19
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
2022-11-19
编辑:Safia
2022-11-19
@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
2022-11-19
编辑:Torsten
2022-11-19
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
2022-11-19
@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
2022-11-19
@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.
Torsten
2022-11-19
And why do you need two iteration loops with 10 iterations instead of one iteration loop with 20 iterations ?
Torsten
2022-11-19
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 ?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)