Mathematica Using While Loops in Mathematica for Iterative Computations

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Loop Mathematica
Click For Summary
The discussion focuses on using while loops in Mathematica for iterative computations, specifically for calculating a function defined by an integral. Participants seek clarification on how to structure the while loop, including defining the condition for continuation and updating variables. Key points include the need to initialize a condition variable to ensure the loop executes, the correct syntax for multiple operations within the loop, and how to increment a counter. The conversation highlights challenges in implementing the loop for the specific mathematical problem and addresses concerns about convergence of the sequence being computed. Overall, the thread provides insights into the practical application of while loops in Mathematica for complex iterative calculations.
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,124
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,720
  • #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:

Similar threads

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