While loop with for loop problem
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
José Everardo Baldo Junior
am 19 Apr. 2019
Kommentiert: José Everardo Baldo Junior
am 23 Apr. 2019
Hello everyone!
I have a situation here trying to solve a while loop inside a for loop.
As you can see in the code, I want to calculate "xn", varying "omega" and then, evaluate which "omega" gives the minor number of iterations (nite), storing in a vector (nite_vec) the each "nite" for each "omega" used in the looping.
But the counte "i" does not to up date inside the "while".
%% Iterations
D = [3 0 0; 0 2 0; 0 0 1];
b = [0; 1; 0];
L = [0 0 0; 1 0 0; 0 1 0];
U = [0 1 0; 0 0 1; 0 0 0];
tol = 10^-7;
nite_max = 100;
n = length(b);
x0 = zeros(n,1);
omega = (1:0.05:2)';
nite = 0;
xn = 1;
xi = x0;
for i = 1:length(omega)
while norm(xn-xi) >= tol && nite <= nite_max
xn = (D-omega(i)*L)\((1-omega(i))*D+omega(i)*U)*x0+omega(i)*((D-omega(i)*L)\b);
xi = x0;
x0 = xn;
nite = nite+1;
end
nite_vec(i) = nite;
end
Could anyone help me with this!
Many thanks!
0 Kommentare
Akzeptierte Antwort
Stephen23
am 19 Apr. 2019
Bearbeitet: Stephen23
am 19 Apr. 2019
Perhaps you meant something like this:
D = [3 0 0; 0 2 0; 0 0 1];
b = [0; 1; 0];
L = [0 0 0; 1 0 0; 0 1 0];
U = [0 1 0; 0 0 1; 0 0 0];
tol = 10^-7;
n = numel(b);
nite_max = 100;
omega = 1:0.05:2;
for k = 1:numel(omega)
nite = 0;
x0 = zeros(n,1);
xn = 1;
xi = x0;
while norm(xn-xi)>=tol && nite<=nite_max
xn = (D-omega(k)*L)\((1-omega(k))*D+omega(k)*U)*x0+omega(k)*((D-omega(k)*L)\b);
xi = x0;
x0 = xn;
nite = nite+1;
end
nite_vec(k) = nite;
end
Giving:
>> nite_vec
nite_vec = 41 36 32 28 24 19 16 17 20 22 25 29 34 40 48 58 75 101 101 101 101
1 Kommentar
José Everardo Baldo Junior
am 23 Apr. 2019
Hello Stephen!
That's it! Many thankls for your help!!!
Kind regards,
Junior.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!