while loop until x amount correct digits

5 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Akihumi
Akihumi il 7 Mag 2020
Have you considered using built-in function 'round'?
Then you can just do:
if round(x,3) == round(y,3)
...
end
  11 Commenti
Akihumi
Akihumi il 7 Mag 2020
Modificato: Akihumi il 8 Mag 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

Accedi per commentare.

Più risposte (1)

mohamed hassan
mohamed hassan il 7 Mag 2020
Thanks for the help guys, I've done the question and got it correct.

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by