while loop until x amount correct digits
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi
Can someomene give me an example or an general way to write a while loop where the condition is that you need to have 3 correct decimals. ?
In my code I have a for loop but I need to make it more adapt.
採用された回答
Akihumi
2020 年 5 月 7 日
Have you considered using built-in function 'round'?
Then you can just do:
if round(x,3) == round(y,3)
...
end
11 件のコメント
mohamed hassan
2020 年 5 月 7 日
My original code is:
for ii = 25:50:500
N=ip;
[r y] = main(N,a,k,Ta); % it calculates u for different N. k a ,Ta are constants.
vec=[vec; y(N+1)]
end
This would give me some answers and then i can manually compare how many decimals are the same( correct) but I want to do it in a smart way.by doublig N til I get 3 correct digits. It should stop iterating then. That would mean i should use i while loop but I dont know how to do it.
Hope you understand
mohamed hassan
2020 年 5 月 7 日
The round function dont give em the amount correct decimals ?
Let me clarify, you want to compare y with another number, is this correct?
I don't think 'round' can give you the number of 'correct decimal', because it is just doing rounding instead of comparison.
But you can use a while loop with 'round', which would look something like this:
n = 1;
while round(y,n) ~= round(x,n) % x is the number that you want to compare with
n = n + 1;
end
But you better set a limit, otherwise N might go into infinity. So it will look something like this
n = 1;
nLim = 10;
while n <= nLim && round(y,n) ~= round(x,n) % x is the number that you want to compare with
n = n + 1;
end
Akihumi
2020 年 5 月 7 日
I see. I think in this case you want to stop the while loop when Utemputsida >= 2.5306e2, right? Then you can try something like this:
thresh = 253.06; % which is equivalent to 1.0e+02 * 2.5306
N = 0;
NLim = 100; % to stop the while loop if it doesn't fulfil the condition for 100 iterations
[r u] = main(N,a,k,Ta);
while N < NLim && round(u(N+1),2) < thresh
N = N + 50;
[r u] = main(N,a,k,Ta);
end
If you want to still use a for loop, you can actually try 'break'
thresh = 253.06; % which is equivalent to 1.0e+02 * 2.5306
for ip = (25*2.^(0:8))
N1=ip;
[r u] = main(N1,a,k,Ta);
Utemputsida=[Utemputsida; u(N1+1)] %vector with answers for different N1
if round(Utemputsida(end),2) >= thresh
break
end
end
Ah I see. Then it should be
N = 0;
NLim = 1e10; % to stop the while loop if it goes too big
decimalPlace = 2;
[r u] = main(N,a,k,Ta);
[r u2] = main(N+50,a,k,Ta);
while N < NLim && round(u(N+1),decimalPlace) ~= round(u(N+50+1),decimalPlace)
N = N + 50;
u = u2;
[r u2] = main(N+50,a,k,Ta);
end
mohamed hassan
2020 年 5 月 7 日
編集済み: mohamed hassan
2020 年 5 月 7 日
I need to calculate the the u untill I get 5 correct digits. In your case I only calculate two times under the while loop and in my assigment I need to double the N until I get 4 for example correct digits.
I have changed the NLim from 100 to 1e10 just now, you may check it again.
If you want to check up to 5 correct digits, try changing decimalPlace from 2 to 5.
I did a N = N + 50 in the while loop. So it will increase like what you did in your for loop (0, 50, 100,... , 400).
You can still print out the u in each loop if you want:
N = 0;
NLim = 1e10; % to stop the while loop if it goes too big
decimalPlace = 5;
[r u] = main(N,a,k,Ta);
[r u2] = main(N+50,a,k,Ta);
disp(u)
disp(u2)
while N < NLim && round(u(N+1),decimalPlace) ~= round(u(N+50+1),decimalPlace)
N = N + 50;
u = u2;
[r u2] = main(N+50,a,k,Ta);
disp(u2)
end
mohamed hassan
2020 年 5 月 7 日
I want to make it more general by doubling N instead of increasing with 50 until the condiction of the while loop is set.
N = 0;
NLim = 1e10; % to stop the while loop if it goes too big
decimalPlace = 5;
[r u] = main(N,a,k,Ta);
[r u2] = main(2*N,a,k,Ta);
disp(u)
disp(u2)
while N < NLim && round(u(N+1),decimalPlace) ~= round(u(2*N+1),decimalPlace)
N = N * 2;
u = u2;
[r u2] = main(2*N,a,k,Ta);
disp(u2)
end
Stephen23
2020 年 5 月 7 日
Rounding is not the correct approach, read these to know why:
The correct way to is to compare the absolute difference against the required tolerance:
abs(A-B)<tol
@Stephen Cobeldick thank you for the lesson.
Then it should be something like this i think
N = 0;
NLim = 1e10; % to stop the while loop if it goes too big
tol = 1e-5;
[r u] = main(N,a,k,Ta);
[r u2] = main(2*N,a,k,Ta);
disp(u(N+1))
disp(u2(2*N+1))
while N < NLim && abs(u(2*N+1)-u(N+1))>tol
N = N * 2;
u = u2;
[r u2] = main(2*N,a,k,Ta);
disp(u2(2*N+1))
end
その他の回答 (1 件)
mohamed hassan
2020 年 5 月 7 日
Thanks for the help guys, I've done the question and got it correct.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
