How can I fix this iteration script

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 件のコメント

Marek Endrizal
Marek Endrizal 2020 年 12 月 15 日
Mb I found solution
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;
end
[U.'*1e-3 it]
???
KSSV
KSSV 2020 年 12 月 15 日
You have to use for to have fixed number of iterations. If you want to run loop twice:
for i = 1:2
i
% do what you want
end
Marek Endrizal
Marek Endrizal 2020 年 12 月 15 日
u mean replacing m by i ? cause I think it's the same as my latest script.
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 15 日
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 2020 年 12 月 15 日
Thank you so much <3

サインインしてコメントする。

回答 (1 件)

Hiro Yoshino
Hiro Yoshino 2020 年 12 月 15 日

0 投票

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 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 15 日
while max(abs(U-UU))>eps && it <= 1
Hiro Yoshino
Hiro Yoshino 2020 年 12 月 15 日
@KALYAN Yes, you're right! Haha

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

製品

タグ

質問済み:

2020 年 12 月 15 日

コメント済み:

2020 年 12 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by