Iterating using while loop over a vector

Dear all,
I want to iterate using while loop over a vector of size (10x1) to reach the convergence. how can I make this, such as the below figure
Can any one help me please ?
note P, ro, and z all of them are vetor with size 10x1
the actual values of ro,P, and z as follows:
ro=[1,0,0,0,0,0,0,0,0,0];
P=[1,1,1,1,1,1,1,1,1,1];
z=ro=[1,0,0,0,0,0,0,0,0,0];

Answers (1)

Torsten
Torsten on 20 Nov 2022
Edited: Torsten on 20 Nov 2022
And how do we obtain ro_j and z_j from P_(j-1) in step 4 ?
And how to calculate P_j ?
j = 0;
error = 1;
rho=[1,0,0,0,0,0,0,0,0,0];
P=[1,1,1,1,1,1,1,1,1,1];
z=rho;
while error > 1e-4
j = j + 1;
[rho;z] = some function of P; % calculate ro_j and z_j as functions of P_(j-1)
P = ? % calculate new P_j (most probably as solution of an optimization problem)
error = ? % calculate error
end

6 Comments

I recommend using a different variable name. eps already has a meaning in MATLAB.
Thank you, Steven.
Thanks Torston for your comment.
so I have the value of G1 and G2
and ro,z,p are vector with size 10x1
G1=ro*p;
G2=z*p;
so to compelete your code, is the following code correct ?
j = 0;
eps = 1;
ro=[1,0,0,0,0,0,0,0,0,0];
P=[1,1,1,1,1,1,1,1,1,1];
z=ro;
while eps > 1e-4
j = j + 1;
[ro] = G1/p(j-1); % calculate ro_j
[z] = G2/p(j-1); z_j as functions of P_(j-1)
then calculate p(j) using the values of [ro] and [z]?, but should I make for loop over indexes, such as 10 indexes ??
end
j is an iteration index, not a component of the vector p. p^(j-1) is meant as the complete vector p in the (j-1)-th iteration through the while-loop. So
ro = G1/p(j-1)
is incorrect.
I think this misunderstanding is the reason why you always try to optimize over one component of the vector p.
I don't know how ro is calculated from p, but the complete vector p at the (j-1)-th iteration (which is written as p^(j-1) in your graphics from the algorithm) has to be used for this operation.
so how can I interpret this term p^(j-1) in the code please? how can I differentiate between p(j) and p(j-1)
Torsten
Torsten on 20 Nov 2022
Edited: Torsten on 20 Nov 2022
so how can I interpret this term p^(j-1) in the code please?
As a vector of length 10 that stems from the (j-1)th iteration within the while-loop.
how can I differentiate between p(j) and p(j-1)
Work with two vectors - call the first pjm1 and the second pj.
At the end of the while loop, set pjm1 = pj which means that you take the result of iteration j as the new input of iteration j+1.

Sign in to comment.

Categories

Asked:

on 20 Nov 2022

Edited:

on 20 Nov 2022

Community Treasure Hunt

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

Start Hunting!