How can I do this iteration?

2 views (last 30 days)
Elia Paini
Elia Paini on 10 Jul 2021
Commented: ANKUR KUMAR on 11 Jul 2021
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?
Thank you for your help!

Answers (1)

ANKUR KUMAR
ANKUR KUMAR on 11 Jul 2021
Edited: ANKUR KUMAR on 11 Jul 2021
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 Comments
ANKUR KUMAR
ANKUR KUMAR on 11 Jul 2021
Updated the code.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by