Using While Loops in Mathematica for Iterative Computations

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Loop Mathematica
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
12 replies · 5K views
member 428835
Hi PF!

I'm trying to compute $$\phi_n(x)=\pi x^2+\int_0^{\pi} 3(0.5 \sin (3x)-tx^2)\phi_{n-1}(t)\,dt$$ given ##\phi_0 = 0##. I'd like to iterate this computation until ##\sqrt{\int(\phi_{n+1}-\phi_n)^2}< 0.1##. I've never used the while loop in Mathematica, tried reading the documentation but was still uncertain. Any help would be great!
 
Physics news on Phys.org
.Scott said:
Code:
n=0; NotDone = True;
While [NotDone,
  <your computation>;
  NotDone = <your decision>;
  n++]
Could you explain this a little bit please? Why have you defined NotDone as True? For <your computation> and <your decision>, what would I put? I've only used Matlab for while loops, so I'm very lost with Mathematica. Also, what does the n++ mean?

We could simplify the problem and state x = x+1, initializing x = 0, and have this run until x is greater than 10. I just want to understand how to do this in Mathematica.
scottdave said:
I'm just a passerby on this question. I don't know Mathematica, but I know other software. What is the way to have multiple operations in (your computation)?
I assume you're asking .Scott this?
 
joshmccraney said:
Why have you defined NotDone as True?
Because you want the while loop to execute at least once. While starts by checking the condition, and will execute <your computation> only if the condition is true.

joshmccraney said:
For <your computation> and <your decision>, what would I put?
<your computation>: what you want Mathematica to calculate. In your case, I guess its ##\phi_n##. <your decision> must by something that will be false when you want the loop to terminate, so the inverse of ##\sqrt{\int(\phi_{n+1}-\phi_n)^2}< 0.1##.

joshmccraney said:
Also, what does the n++ mean?
Increment n by 1. You might not actually need to keep track of n in your case.

joshmccraney said:
We could simplify the problem and state x = x+1, initializing x = 0, and have this run until x is greater than 10. I just want to understand how to do this in Mathematica.
Try the example in the Mathematica help:
Code:
n = 1; While[n < 4, Print[n]; n++]
 
scottdave said:
I'm just a passerby on this question. I don't know Mathematica, but I know other software. What is the way to have multiple operations in (your computation)?
Curly brackets {}.
 
DrClaude said:
Because you want the while loop to execute at least once. While starts by checking the condition, and will execute <your computation> only if the condition is true.<your computation>: what you want Mathematica to calculate. In your case, I guess its ##\phi_n##. <your decision> must by something that will be false when you want the loop to terminate, so the inverse of ##\sqrt{\int(\phi_{n+1}-\phi_n)^2}< 0.1##.Increment n by 1. You might not actually need to keep track of n in your case.Try the example in the Mathematica help:
Code:
n = 1; While[n < 4, Print[n]; n++]
Thanks, this was very helpful!

I was able to get the loop to work for some simple iterations, like
Code:
n = 0;
NotDone = True;
While[NotDone,
 \[Phi][x_] = x + n;
 NotDone = Integrate[\[Phi][x] , {x, 0, 1}] < 3;
 n++]

but when I try the real thing what I have is
Screen Shot 2018-02-26 at 11.19.03 AM.png

which I know can't work since I have no way of updating ##\phi old##. Could you help me here?
 

Attachments

  • Screen Shot 2018-02-26 at 11.19.03 AM.png
    Screen Shot 2018-02-26 at 11.19.03 AM.png
    10.6 KB · Views: 1,158
scottdave said:
After setting the new value for NotDone, can you just say that Φold = Φnew ? Then it will use this on the next iteration.
I thought this might work too, but I'm getting a bunch of errors using
Screen Shot 2018-02-26 at 12.31.20 PM.png
 

Attachments

  • Screen Shot 2018-02-26 at 12.31.20 PM.png
    Screen Shot 2018-02-26 at 12.31.20 PM.png
    12.1 KB · Views: 1,752
joshmccraney said:
Could you explain this a little bit please? Why have you defined NotDone as True? For <your computation> and <your decision>, what would I put? I've only used Matlab for while loops, so I'm very lost with Mathematica. Also, what does the n++ mean?

We could simplify the problem and state x = x+1, initializing x = 0, and have this run until x is greater than 10. I just want to understand how to do this in Mathematica.

I assume you're asking .Scott this?
Anyone who could answer. I wasn't sure how to do multiple statements in Mathematica. From the sample code, I see that it looks like you separate with semicolons.
 
Here is the while loop:
Code:
While[NotDone, \[Phi]new[x_] = \[Pi] x^2 +
   Integrate[3 (Sin[3 x]/2 - t x^2) \[Phi]old[t], {t, 0, \[Pi]}];
NotDone =
  Sqrt[Integrate[(\[Phi]new[x] - \[Phi]old[x])^2, {x, 0, \[Pi]}]] >
   0.1; \[Phi]old[x_] = \[Phi]new[x]; n++;]
But your problem is flawed: the sequence ##\{\phi_n\}## does not converge.
 
  • Like
Likes   Reactions: member 428835
Gotcha! Ok, I thought this might be the case! Thanks so much for the advise! And I learned something. How do you work with the solution output though? When I run the code I do not receive an output.
 
Last edited by a moderator: