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.

 Akzeptierte Antwort

Akihumi
Akihumi am 7 Mai 2020

1 Stimme

Have you considered using built-in function 'round'?
Then you can just do:
if round(x,3) == round(y,3)
...
end

11 Kommentare

mohamed hassan
mohamed hassan am 7 Mai 2020
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
mohamed hassan am 7 Mai 2020
The round function dont give em the amount correct decimals ?
Akihumi
Akihumi am 7 Mai 2020
Bearbeitet: Akihumi am 7 Mai 2020
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
Akihumi
Akihumi am 7 Mai 2020
Bearbeitet: Akihumi am 7 Mai 2020
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
mohamed hassan am 7 Mai 2020
Bearbeitet: mohamed hassan am 7 Mai 2020
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.
Akihumi
Akihumi am 7 Mai 2020
Bearbeitet: Akihumi am 7 Mai 2020
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
mohamed hassan am 7 Mai 2020
I want to make it more general by doubling N instead of increasing with 50 until the condiction of the while loop is set.
Akihumi
Akihumi am 7 Mai 2020
Bearbeitet: Akihumi am 7 Mai 2020
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
Akihumi
Akihumi am 7 Mai 2020
Bearbeitet: Akihumi am 8 Mai 2020
@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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

mohamed hassan
mohamed hassan am 7 Mai 2020

0 Stimmen

Thanks for the help guys, I've done the question and got it correct.

Kategorien

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by