이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)