How can I do this iteration?

조회 수: 3 (최근 30일)
Elia Paini 2021년 7월 10일
댓글: ANKUR KUMAR 2021년 7월 11일
Hi, I have a simple question related to a problem.
I have to make an iterative code which updates values of some hypothesized vectors, until I reach convergence.
To simplify, I have two vectors a,b with initial assumed values, then the cycle updates their values until it stops when:
, where "i" is the iteration index.
I think this can be solved with an easy code, maybe with a while cicle?

댓글을 달려면 로그인하십시오.

답변 (1개)

ANKUR KUMAR 2021년 7월 11일
편집: ANKUR KUMAR 2021년 7월 11일
Here is the sample code using random data
a=rand(1,50);
b=rand(1,50);
threshold=0.5; % You need to change it to 1e-6 and use original data
i=1;
out= -9999; % setting random data
while out<threshold
out(i)=abs(a(i+1)-a(i))+abs(b(i+1)-b(i));
i=i+1;
end
out
out = 1×3
0.3175 0.4034 0.6554
댓글 수: 2없음 표시없음 숨기기
Elia Paini 2021년 7월 11일
Thanks! But how can I save all simulated vectors in a matrix, at each iteration? In your code, "i" refers to the element of the vectors, while it should represent the column of a matrix built at every iteration.
ANKUR KUMAR 2021년 7월 11일
Updated the code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by