現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
how to define an iteration number ?
24 ビュー (過去 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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)