4

We run such ParallelTable based on @Syed https://mathematica.stackexchange.com/a/261603/54516.

Clear[f, g, a]
f[x_] = x^3 - 6*x^2 + 11*x - 6
g[x_] = D[f[x], x]
a[0] = 0;
ParallelTable[a[n + 1] = a[n] - N[f[a[n]]/g[a[n]]], {n, 0, 7}]
Table[a[n], {n, 0, 7}]

It returns: enter image description here

If we try ParallelDo

Clear[f, g, a]
f[x_] = x^3 - 6*x^2 + 11*x - 6
g[x_] = D[f[x], x]
a[0] = 0;
ParallelDo[a[n + 1] = a[n] - N[f[a[n]]/g[a[n]]], {n, 0, 7}]
Table[a[n], {n, 0, 7}]

It returns

enter image description here

ABCDEMMM
  • 1,816
  • 1
  • 9
  • 17
  • 5
    Apart from your implementation, your calculation is not easily parallelizable: to calculate $a(n+1)$ you need to know $a(n)$, so at a minimum you would waste a lot of time passing information between kernels. What are you trying to achieve by attempting to run it in parallel? – MarcoB Jan 05 '22 at 02:53
  • 1
    While MarcoB has stated the problem quite well, and the question they pose should be answered to better find the actual problem being shown here...this is just not a logically parallelizable problem, because each subsequent result relies on the previous one...that said, there is definitely something funny going on with the first result, as a[0]!=0 in the output shown. The second result shows, from my understanding, that the kernels are not communicating with one another. Perhaps one should use DistributeDefinitions? – CA Trevillian Jan 05 '22 at 03:32
  • 2
    @CATrevillian The output shown in the first result is from ParallelTable, and shows a[1] through a[8]. The OP doesn't show the output of Table for the first example. So I think a[0] is still equal to 0 (on all kernels). The question seems based on a misunderstanding of how variables are treated in parallel kernels. —Well, technically, no question is actually asked. We're simply shown code that runs as expected. – Michael E2 Jan 05 '22 at 04:55
  • 1
    From the docs Details section: ParallelTable will give the same results as Table, except for side effects during the computation . So there are no guarantees that ParallelTable will gracefully revert to Table if the parallel functionality is of no use. – Syed Jan 05 '22 at 07:31

1 Answers1

6

As I understand we have 3 questions:

  1. How to implement Newton's iterations method to solve nonlinear problem?
  2. How to implement Newton's iterations method in a case of vector form a[0], for example, randomly distributed a[0]?
  3. How to parallelize code in a case of randomly distributed a[0]?

Solution 1-2

Clear[f, g, a]

f = x^3 - 6x^2 + 11x - 6; g = D[f, x]; nmax = 10; jmax = 15; a[0] = RandomReal[{-1, 1}, jmax]; Do[a[n + 1] = Table[a[n][[j]] - N[f/g /. x -> a[n][[j]]], {j, jmax}], {n, 0, nmax}]

mat = Table[a[n], {n, 0, nmax}]

(* {{-0.567701, 0.183781, -0.122707, 0.981023, -0.78634, 0.841471, 0.490745, 0.132565, -0.0935607, 0.829216, 0.454513, -0.229974, 0.478757, -0.404825, 0.907072}, {0.197046, 0.653079, 0.471811, 0.999483, 0.0591077, 0.972456, 0.82135, 0.623468, 0.48941, 0.968683, 0.802566, 0.406538, 0.815177, 0.298725, 0.989351}, {0.660693, 0.89993, 0.81158, 1., 0.580462, 0.998931, 0.966173, 0.886414, 0.820665, 0.998629, 0.959918, 0.777159, 0.964157, 0.718194, 0.999834}, {0.903334, 0.987817, 0.962965, 1., 0.866068, 0.999998, 0.998409, 0.984694, 0.965952, 0.999997, 0.997796, 0.950916, 0.998222, 0.927972, 1.}, {0.988559, 0.999784, 0.998106, 1., 0.979485, 1., 0.999996, 0.999661, 0.998389, 1., 0.999993, 0.996757, 0.999995, 0.993336, 1.}, {0.999809, 1., 0.999995, 1., 0.999398, 1., 1., 1., 0.999996, 1., 1., 0.999984, 1., 0.999934, 1.}, {1., 1., 1., 1., 0.999999, 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}, {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}, {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}, {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}, {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}*)

Visualization

ListLinePlot[Transpose[mat], PlotRange -> All]

Figure 1

Solution 3

ParallelDo[

a[n + 1] = Table[a[n][[j]] - N[f/g /. x -> a[n][[j]]], {j, jmax}], {n, 0, nmax}]

mat1 = Table[a[n], {n, 0, nmax}]

(* {{-0.567701, 0.183781, -0.122707, 0.981023, -0.78634, 0.841471, 0.490745, 0.132565, -0.0935607, 0.829216, 0.454513, -0.229974, 0.478757, -0.404825, 0.907072}, {0.197046, 0.653079, 0.471811, 0.999483, 0.0591077, 0.972456, 0.82135, 0.623468, 0.48941, 0.968683, 0.802566, 0.406538, 0.815177, 0.298725, 0.989351}, {0.660693, 0.89993, 0.81158, 1., 0.580462, 0.998931, 0.966173, 0.886414, 0.820665, 0.998629, 0.959918, 0.777159, 0.964157, 0.718194, 0.999834}, {0.903334, 0.987817, 0.962965, 1., 0.866068, 0.999998, 0.998409, 0.984694, 0.965952, 0.999997, 0.997796, 0.950916, 0.998222, 0.927972, 1.}, {0.988559, 0.999784, 0.998106, 1., 0.979485, 1., 0.999996, 0.999661, 0.998389, 1., 0.999993, 0.996757, 0.999995, 0.993336, 1.}, {0.999809, 1., 0.999995, 1., 0.999398, 1., 1., 1., 0.999996, 1., 1., 0.999984, 1., 0.999934, 1.}, {1., 1., 1., 1., 0.999999, 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}, {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}, {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}, {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}, {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}*)

Visualization

ListLinePlot[Transpose[mat1], PlotRange -> All]

Figure 2

Alex Trounev
  • 44,369
  • 3
  • 48
  • 106