Using While Loops in Mathematica for Iterative Computations

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Loop Mathematica
Click For Summary

Discussion Overview

The discussion revolves around using while loops in Mathematica for iterative computations, specifically for calculating a sequence defined by an integral equation. Participants explore how to implement the while loop structure, manage iterations, and update values within the loop.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks help with implementing a while loop in Mathematica to compute a sequence defined by an integral equation.
  • Another participant provides a basic structure for the while loop but does not specify the computations or decisions needed.
  • Several participants express uncertainty about how to perform multiple operations within the while loop and how to manage variable updates.
  • There are discussions about the necessity of tracking the iteration variable n and how to define the termination condition for the loop.
  • One participant shares a working example of a simple while loop but struggles with applying it to the original problem due to issues with updating previous values.
  • Another participant suggests that the old value can be updated to the new value for the next iteration, but there are reports of errors when attempting this.
  • Concerns are raised about the convergence of the sequence being computed, with one participant stating that the sequence does not converge.
  • Questions arise about how to handle output from the computations performed within the loop.

Areas of Agreement / Disagreement

Participants generally agree on the structure of the while loop but express differing views on the specifics of implementation, particularly regarding variable updates and convergence of the sequence. The discussion remains unresolved regarding the correct approach to achieve the desired computations.

Contextual Notes

Participants mention limitations in their understanding of Mathematica compared to other software, which may affect their ability to implement the while loop correctly. There are unresolved issues regarding the mathematical properties of the sequence being computed.

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
Code:
n=0; NotDone = True;
While [NotDone,
  <your computation>;
  NotDone = <your decision>;
  n++]
 
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)?
 
.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,145
After setting the new value for NotDone, can you just say that Φold = Φnew ? Then it will use this on the next iteration.
 
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,739
  • #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   Reactions: 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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
13K