Using While Loops in Mathematica for Iterative Computations

In summary, the conversation discusses the use of a while loop in Mathematica to compute a sequence, and the need for defining a condition that will eventually be false in order to terminate the loop. The use of curly brackets to indicate multiple operations within the loop is also mentioned. However, the problem being tackled is flawed as the sequence does not converge.
  • #1
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
  • #2
Code:
n=0; NotDone = True;
While [NotDone,
  <your computation>;
  NotDone = <your decision>;
  n++]
 
  • #3
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)?
 
  • #4
.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?
 
  • #5
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++]
 
  • #6
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 {}.
 
  • #7
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: 995
  • #8
After setting the new value for NotDone, can you just say that Φold = Φnew ? Then it will use this on the next iteration.
 
  • #9
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,605
  • #10
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.
 
  • #11
joshmccraney said:
I thought this might work too, but I'm getting a bunch of errors usingView attachment 221100
Missing semicolons to separate the statements?
 
  • #12
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 member 428835
  • #13
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:

1. What is a "While Loop" in Mathematica?

A "While Loop" is a programming construct in Mathematica that allows you to repeat a set of instructions while a certain condition is true. This facilitates the automation of tasks and makes it easier to process large amounts of data.

2. How is a "While Loop" different from a "For Loop" in Mathematica?

While Loops and For Loops are both used for repetition in Mathematica, they differ in the way they are structured. While Loops continue to repeat as long as the condition is true, while For Loops have a specific number of iterations and a defined increment.

3. How do I write a "While Loop" in Mathematica?

To write a While Loop in Mathematica, you need to specify the condition that needs to be met for the loop to continue, and the instructions that need to be repeated. For example:
While[x<10, Print[x]; x++]. This loop will print the value of x and increment it until x reaches 10.

4. Can I use multiple conditions in a "While Loop" in Mathematica?

Yes, you can use multiple conditions in a While Loop in Mathematica by using logical operators such as "&&" (AND) and "||" (OR). For example:
While[x<10 && y>5, Print[x,y]; x++; y--]. This loop will continue as long as x is less than 10 AND y is greater than 5.

5. What are some common mistakes to avoid when using "While Loops" in Mathematica?

Some common mistakes to avoid when using While Loops in Mathematica include not updating the variable used in the condition, which can result in an infinite loop, and not using curly brackets to enclose the instructions to be repeated. It is also important to make sure the condition will eventually become false, otherwise the loop will continue indefinitely.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
827
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
267
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
938
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
12K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top