How can I fix this iteration script

2 Ansichten (letzte 30 Tage)
Marek Endrizal
Marek Endrizal am 15 Dez. 2020
Kommentiert: Hiro Yoshino am 15 Dez. 2020
Hi, I'm trying to solve problem.
I have to set max 2 iteration and to be honest I am not skilled in this kind of script. If someone will help me u will give me good christmas present thank you.
it = 0; % Počet iterácií
while max(abs(U-UU))>eps
UU = U; %Saving previously iteration
for m = 1:length(U) %Solve of new iteration
if metoda == 1
U(m,1) = a(m,m)\(-b(m,:)*UU+c(m,:));
else
U(m,1) = a(m,m)\(-b(m,:)*U+c(m,:));
end
end
it = it+1;
end
[U.'*1e-3 it]
  5 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 15 Dez. 2020
it = 0; % Počet iterácií
while max(abs(U-UU))>eps
UU = U;
for m = 1:length(1) %
if metoda == 1
U(m,1) = a(m,m)\(-b(m,:)*UU+c(m,:));
else
U(m,1) = a(m,m)\(-b(m,:)*U+c(m,:));
end
end
it = it+1;
if it>2
break;
end
end
end
[U.'*1e-3 it]
Marek Endrizal
Marek Endrizal am 15 Dez. 2020
Thank you so much <3

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Hiro Yoshino
Hiro Yoshino am 15 Dez. 2020
All you need is add one more restriction on the top of the "while" loop.
it = 0; % Počet iterácií
while max(abs(U-UU))>eps || it <= 1
UU = U; %Saving previously iteration
for m = 1:length(U) %Solve of new iteration
if metoda == 1
U(m,1) = a(m,m)\(-b(m,:)*UU+c(m,:));
else
U(m,1) = a(m,m)\(-b(m,:)*U+c(m,:));
end
end
it = it+1;
end
[U.'*1e-3 it]
  2 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 15 Dez. 2020
while max(abs(U-UU))>eps && it <= 1
Hiro Yoshino
Hiro Yoshino am 15 Dez. 2020
@KALYAN Yes, you're right! Haha

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by