Azzera filtri
Azzera filtri

for and while loop

1 visualizzazione (ultimi 30 giorni)
T
T il 21 Gen 2021
Commentato: T il 21 Gen 2021
Hi
I am looking to run a while loop that solves a rootfinding problem for two initail values, stored in a matrix as described below. It should store the two solved coordiantes in xysolve.
The while loop works fine when running with ii = 1 or ii = 2 (indexing the two initial values) independtly.
However it does not work within the for loop, that I want to use to run the rootfinding for both initail values, one after the other.
Not sure what is the problem here.
Many thanks in advance for any help.
T
%% Solving
% Initial estimates are 2 2x1 vectors: [-10; 10] and [10;-10]
% These are stored together in 2x2 matrix xy
xy = [-10 10 ; 10 10];
iter = 0; imax=100; tol=0.001; err=tol;
xysolve = zeros(2,2)
for ii = 1:2
while iter <= imax && err >= tol
iter = iter + 1;
xy_new = xy(:,ii) - Jacob(xy(:,ii))\f(xy(:,ii));
err = norm(f(xy(:,ii)));
xy = [0 0 ; 0 0]
xy(:,ii) = xy(:,ii) + xy_new
xysolve = xy
end
end
  3 Commenti
T
T il 21 Gen 2021
no error. it evaluates xy_solve as [ 0 Nan; 0 Nan]
T
T il 21 Gen 2021
xy = [-10 10 ; 10 10];
% iter = 0; imax=100; tol=0.001; err=tol;
xysolve = zeros(2,2)
for ii = 1:2
iter = 0; imax=100; tol=0.001; err=tol;
while iter <= imax && err >= tol
iter = iter + 1;
xy_new = xy(:,ii) - Jacob(xy(:,ii))\f(xy(:,ii));
err = norm(f(xy(:,ii)));
xy = [0 0 ; 0 0]
xy(:,ii) = xy(:,ii) + xy_new
xysolve = xy
end
xysolve %LINE
end
so if I make a break at the indicate LINE, xysolve shows that it has solved the root finding for the first coordinates.
so something is not working in repeating the whole loop now with ii = 2.
Thanks

Accedi per commentare.

Risposta accettata

Rik
Rik il 21 Gen 2021
You need to reset the error term and iteration count at the start of the for loop. Otherwise it will keep the previous values and immediately exit the while loop.
  3 Commenti
Rik
Rik il 21 Gen 2021
You are wiping your initial value array as well by setting xy=zeros(2);. But otherwise, yes, this is what I meant.
As a further note: I would strongly suggest using the automatic indentation for readability. I would also suggest adding comments about what the code is doing.
T
T il 21 Gen 2021
Thanks for your help Rik and the suggestions.
I have worked out how to do it. Ill include it below for any future people with a similar question.
% Initial estimates are 2 2x1 vectors: [-10; 10] and [10;-10]
% These are stored together in 2x2 matrix xy
xysolve = zeros(2,2) % Blank matrix for root cooridantes.
for ii = 1:2 % Indexing to locate the two initial estiamtes in xy.
xy = [-10 10 ; 10 10]; % Initial estimates
iter = 0; imax=100; tol=0.01; err=tol;
while iter <= imax && err >= tol
% Root finding method for a system of linear equations (using functions)
iter = iter + 1;
xy_new = xy(:,ii) - Jacob(xy(:,ii))\f(xy(:,ii));
% Error for stopping criteria
err = norm(f(xy(:,ii)));
% New matrix to position evaluated coordinates back in 2x2 matrix so loop can continue.
XY = zeros(2)
XY(:,ii) = XY(:,ii) + xy_new
xy = XY
end
% xysolve matrix populated with coordiantes when stopping criteria ends while loop.
xysolve = xysolve + xy
end

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by