while loop until x amount correct digits
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
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
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
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
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
