From a8d1aeb11322c27d650bdd8381edcfd1a3017a23 Mon Sep 17 00:00:00 2001 From: Alex Razoumov Date: Mon, 23 Sep 2024 09:39:59 -0700 Subject: [PATCH] increase niter to 10_000 so that we reach convergence after 7505 iterations; update this chapter accordingly --- episodes/04-conditionals.md | 5 +++-- episodes/05-loops.md | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/episodes/04-conditionals.md b/episodes/04-conditionals.md index 10ecf92..42c133e 100644 --- a/episodes/04-conditionals.md +++ b/episodes/04-conditionals.md @@ -90,7 +90,7 @@ The main loop in our simulation can be programmed using a while statement like t ```chpl //this is the main loop of the simulation var c = 0; -var delta = tolerance; +delta = tolerance; while (c < niter && delta >= tolerance) do { c += 1; @@ -157,7 +157,8 @@ writeln('Temperature at start is: ', temp[x, y]); //this is the main loop of the simulation var c = 0; -while (c < niter) do +delta = tolerance; +while (c < niter && delta >= tolerance) do { c += 1; if (c % outputFrequency == 0) diff --git a/episodes/05-loops.md b/episodes/05-loops.md index 27942c1..09d19d7 100644 --- a/episodes/05-loops.md +++ b/episodes/05-loops.md @@ -216,8 +216,17 @@ Temperature at iteration 500: 0.823152 ## Challenge 3: Can you do it? -So far, `delta` has been always equal to `tolerance`, which means that our main while loop will always run the -500 iterations. So let's update `delta` after each iteration. Use what we have studied so far to write the +Let us increase the maximum number of iterations to `niter = 10_000`. The code now does 10_000 iterations: + +```output +... +Temperature at iteration 9960: 0.79214 +Temperature at iteration 9980: 0.792139 +Temperature at iteration 10000: 0.792139 +``` + +So far, `delta` has been always equal to `tolerance`, which means that our main `while` loop will always run +`niter` iterations. So let's update `delta` after each iteration. Use what we have studied so far to write the required piece of code. :::::::::::::::::::::::: solution @@ -249,7 +258,7 @@ chpl base_solution.chpl -o base_solution ```output The simulation will consider a matrix of 100 by 100 elements, -it will run up to 500 iterations, or until the largest difference +it will run up to 10000 iterations, or until the largest difference in temperature between iterations is less than 0.0001. You are interested in the evolution of the temperature at the position (1,100) of the matrix... @@ -259,9 +268,12 @@ Temperature at iteration 0: 25.0 Temperature at iteration 20: 2.0859 Temperature at iteration 40: 1.42663 ... -Temperature at iteration 460: 0.826941 -Temperature at iteration 480: 0.824959 -Temperature at iteration 500: 0.823152 +Temperature at iteration 7460: 0.792283 +Temperature at iteration 7480: 0.792281 +Temperature at iteration 7500: 0.792279 + +Final temperature at the desired position after 7505 iterations is: 0.792279 +The difference in temperatures between the last two iterations was: 9.99834e-05 ``` :::::::::::::::::::::::::::::::::