Newest questions tagged iteration - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-10-17T12:10:01Z https://mathematica.stackexchange.com/feeds/tag?tagnames=iteration&sort=newest https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/207862 1 Bug in FindRoot? Andreas Lauschke https://mathematica.stackexchange.com/users/1598 2019-10-14T14:48:43Z 2019-10-14T16:38:41Z <pre><code>Manipulate[With[{sol=(Reap@FindRoot[Tan@z-(value[]+I start[]),{z,start[]+I start[]},StepMonitor:&gt;Sow[z]])[[2,1]]//Flatten}, ComplexListPlot[sol,Joined-&gt;True, PlotLabel-&gt; "start: "&lt;&gt;ToString@Chop[start[]+I start[]]&lt;&gt; "\nfirst iterate: "&lt;&gt;ToString@Chop@First@sol&lt;&gt; "\nvalue of first iterate: "&lt;&gt;ToString@Chop@Tan@First@sol&lt;&gt; "\nlast iterate: "&lt;&gt;ToString@Chop@Last@sol&lt;&gt; "\nvalue of last iterate: "&lt;&gt;ToString@Chop@Tan@Last@sol&lt;&gt; "\nvalue: "&lt;&gt;ToString[value[]+I value[]], AspectRatio-&gt;1,PlotRange-&gt;All,ImageSize-&gt;300,PlotMarkers-&gt;Automatic,Epilog-&gt;{PointSize[Large],Blue,Point[First@ReIm@sol],Red,Point[Last@ReIm@sol]}]], {start,{-5,-5},{5,5}}, {value,{-5,-5},{5,5}} ] </code></pre> <p>I notice that value and value of last iterate are always identical in the real part and always different in the imaginary part (sometimes "extreme", like 3.9I vs. -2.7I). I can't see how I would be treating real and imaginary parts differently in my code. Is that a bug in FindRoot, or do I have a bug in my code?</p> <ul> <li>if you see only a red dot in the middle it means that the FindRoot iteration got stuck (FindRoot::lstol). Can you help me get it unstuck and actually converge?</li> <li>is there a way to give the 2dim sliders some GridLines too?</li> </ul> https://mathematica.stackexchange.com/q/207491 0 Solving a system of linear equations with a For-loop [on hold] Nurul Hidayati https://mathematica.stackexchange.com/users/67775 2019-10-08T06:47:43Z 2019-10-08T18:45:38Z <p>I need to find the value of <code>c1, c2, c3, c4</code> and <code>c5</code>, but the output from my code is the simplified equations, not the exact value of <code>c1, c2, c3, c4</code> and <code>c5</code>. How can I get the values of <code>c1, c2, c3, c4</code> and <code>c5</code> without having to insert those into 'Solve'?</p> <pre><code>n = 5; m = n + 1; For[i = 1; t = c, i &lt; m, i++, t = q Subscript[c, i - 1] - q Subscript[c, i] + e (Subscript[c, i -1] Subscript[c, i]) + e (Subscript[c, i + 1] - Subscript[c, i]) - v Subscript[c, i] + Subscript[w, i] == 0; Print[Solve[t]]]; </code></pre> https://mathematica.stackexchange.com/q/205041 0 Iterating over a list and defining the range accordingly William https://mathematica.stackexchange.com/users/2871 2019-09-10T11:55:25Z 2019-09-10T11:59:24Z <p>I have the following list,</p> <pre><code>l = {{"a", "b"}, {"a", "b"}, {"c", "d"}, {"e", "f"}, {"e", "f"}, {"x", "y", "z"}, {"m", "n", "o"}, {"p", "q", "s"}}; </code></pre> <p>for each sublist I want to count them and make a polynomial out of it, for example for <code>{"a", "b"}</code> I have </p> <pre><code>Total[x^-2 x^Map[Length, Select[l, ContainsAll[{"a", "b"}]]]] </code></pre> <p>Note that the <code>x^-2</code> terms is important to be there for my later calculations. Now if I want to do this for <code>{"x","y","z"}</code> I shall do </p> <pre><code>Total[x^-3 x^Map[Length, Select[l, ContainsAll[{"x", "y","z"}]]]] </code></pre> <p>So as you see the power of first term is now <code>x^-3</code>, now if I want to do this on a table I can write, </p> <pre><code>AssociationThread[ l -&gt; Table[ Total[x^-i x^Map[Length, Select[l, ContainsAll[l[[i]]]]]], {i, Length[l]}]] </code></pre> <p>My question is how to iterate the <code>x^-i</code> such that it works correspondingly? meaning for entries of length 2 the power should be <code>x^-2</code>, for entries of length 3 the power should be <code>x^-3</code> and generalisable to higher length in similar manner. </p> https://mathematica.stackexchange.com/q/204789 0 In an iterative process, how does one simply bypass a step producing an error? [closed] Paul B. Slater https://mathematica.stackexchange.com/users/29989 2019-09-04T17:08:06Z 2019-09-05T21:20:06Z <p>This is undoubtedly rather elementary, but the remedy escapes me presently (and does not, as far as I can see, readily appear in Help).</p> <p>I'm engaged in an iterative process with several computations at each iteration, and for rare steps I generate: </p> <p>Power::infy: Infinite expression 1/Sqrt encountered</p> <p>and </p> <p>CompiledFunction::cfse: Compiled expression ComplexInfinity should be a "machine-size real number </p> <p>and</p> <p>CompiledFunction::cfex: Could not complete external evaluation; proceeding with uncompiled evaluation</p> <p>I would just simply like to bypass such steps (while recording--though not essential--their specific numbers).</p> <p>How might I proceed?</p> https://mathematica.stackexchange.com/q/203971 1 How to iteratively (or most efficiently) create such list? CasperYC https://mathematica.stackexchange.com/users/62701 2019-08-19T23:26:24Z 2019-08-20T20:11:48Z <p><strong>Update</strong></p> <p>A very crude attemp</p> <pre><code>myfun[n_] := Block[{PlaceValue}, PlaceValue = IntegerDigits@n; If[ FreeQ[PlaceValue, 9], PlaceValue[[-1]] = PlaceValue[[-1]] + 1, If[ AllTrue[PlaceValue, # == 9 &amp;], PlaceValue = Table[2, Length@PlaceValue + 1], PlaceValue[[First@FirstPosition[PlaceValue, 9] - 1 ;;]] = PlaceValue[[First@FirstPosition[PlaceValue, 9] - 1]] + 1 ] ]; FromDigits@PlaceValue ]; myfun myfun myfun myfun myfun </code></pre> <p>as a function.</p> <p><strong>OP</strong></p> <p>I want to write a code that is able to iteratively create a list that looks like this:</p> <pre><code>l2 = Table[FromDigits@{i, j}, {i, 2, 9}, {j, i, 9}] // Flatten; l3 = Table[FromDigits@{i, j, k}, {i, 2, 9}, {j, i, 9}, {k, j, 9}] // Flatten; l4 = Table[FromDigits@{i, j, k, l}, {i, 2, 9}, {j, i, 9}, {k, j, 9}, {l, k, 9}] // Flatten; desiredList = Flatten[{l2, l3, l4}] </code></pre> <p>That is, the next number is the smallest(first) number that all the digits are in non-decreasing order. I can manully do this for numbers up to 26 digits until I ran out of letters to use and then move on to use <strong>double letter</strong> for the iterators, but I am wondering if there is a better way to creat it? I want to use it for large number that has 60/70 digits and maybe even larger.</p> <p>It can be a function that takes a number for example,</p> <pre><code> f = 345 f = 349 f = 355 f = 444 </code></pre> <p>and </p> <pre><code>f = 22222 </code></pre> <p>ect.</p> <p>Or some kind of iterative formula that I can use in a <code>While</code> or <code>Fold</code> loop?</p> <p>Thanks.</p> https://mathematica.stackexchange.com/q/203863 0 solve the iterative integral equatio [duplicate] Thenmozhi S https://mathematica.stackexchange.com/users/66955 2019-08-17T12:26:16Z 2019-08-20T18:05:59Z <div class="question-status question-originals-of-duplicate"> <p>This question already has an answer here:</p> <ul> <li> <a href="/questions/203853/solve-integral-equation-by-iteration" dir="ltr">Solve integral equation by iteration</a> <span class="question-originals-answer-count"> 1 answer </span> </li> </ul> </div> <p>The following code is incorrect, please specify my mistakes </p> <pre><code>y[0, t_] := 4 - 3 t; y[n_, x_] := 4 - 3 x + 0.59489439* NIntegrate[ t (1 - x) {D[y[n - 1], {t,2}] - 3/2 y[n - 1, t]^2}, {t, 0, x}] + 0.594894 NIntegrate[ x (1 - t) {D[y[n - 1], {t,2}] - 3/2 y[n - 1, t]^2}, {t, x, 1}] Table[{n, x, y[n, x]}, {n, 1, 3}, {x, .1, 1, .1}] </code></pre> https://mathematica.stackexchange.com/q/203853 0 Solve integral equation by iteration Thenmozhi S https://mathematica.stackexchange.com/users/66955 2019-08-17T05:56:52Z 2019-08-17T09:12:02Z <pre><code> For[{n = 1, y[x_] = 1}, n &lt; 4, n++, y[n][x_] = 1 + Integrate[y[n - 1][t]^2 + t^2, {t, 0, x}]; Print[{n, y[n][t]}]] </code></pre> <p>here how can I get table of values for t=0.1, 0.2, 0.3,.....,1?</p> https://mathematica.stackexchange.com/q/203562 7 How can I iterate this process? geoffrey https://mathematica.stackexchange.com/users/66934 2019-08-10T14:30:33Z 2019-08-10T20:07:45Z <pre><code> a = RandomVariate[UniformDistribution[{0, 1}]]; b = RandomVariate[UniformDistribution[{0, 1}]]; c = RandomVariate[UniformDistribution[{0, 1}]]; a + a b + a b c </code></pre> <p>I want to continue picking random reals in (0,1) and adding the product to the previous sum. How can Mathematica help me here?</p> <p>Best regards Geoffrey Critzer</p> https://mathematica.stackexchange.com/q/203342 2 Question about iteration [closed] Jensen Kang https://mathematica.stackexchange.com/users/61379 2019-08-06T06:21:47Z 2019-08-06T15:09:37Z <p>I have the following expression:</p> <pre><code>sol1 = (11 + 3*δ - 11*θ + 4*m*θ - 3*δ*θ - 8*Sqrt[(-(2 + δ))*(-1 + θ)*(1 - θ + m*θ)])/(4 + 4*δ) </code></pre> <p>I want to plug numbers into <span class="math-container">$\theta$</span> that range from <span class="math-container">$0.1$</span> to <span class="math-container">$0.9$</span>.</p> <p>Does anybody know how to do this more efficiently than using a loop? I tried to use <code>For</code> and <code>Do</code>, but I am not sure how to implement the procedure I want correctly.</p> https://mathematica.stackexchange.com/q/202505 6 How to iteratively build a list? SmileyCraft https://mathematica.stackexchange.com/users/66664 2019-07-21T18:47:16Z 2019-07-23T18:52:29Z <p>I have the following c++ code that I want to translate to Mathematica.</p> <pre><code>std::vector&lt;int&gt; squbes; for (int p : primes) for (int q : primes) if (p != q){ if (p*p*q*q*q &gt; maxi) break; squbes.push_back(p*p*q*q*q); } </code></pre> <p>As you can see I simply have a list of numbers that I want to iteratively append elements to.</p> <p>The first thing I tried was the following functional approach.</p> <pre><code>sqube[{x_,y_}] := x*x*y*y*y unequal[{x_, y_}] := x != y smallenough[x_] := x &lt;= maxi pairs := Select[Tuples[primes, 2], unequal] squbes := Select[Map[sqube, pairs], smallenough] </code></pre> <p>However this is way too slow because it misses the very important break condition in the loop in the c++ version.</p> <p>Next I tried the most direct translation I could come up with.</p> <pre><code>sqube[{x_,y_}] := x*x*y*y*y squbes := {} For[i = 1, i &lt;= Length[primes], i++, For[j = 1, j &lt;= Length[primes] &amp;&amp; sqube[{primes[[i]], primes[[j]]}] &lt;= maxi, j++, AppendTo[squbes, sqube[{primes[[i]], primes[[j]]}]]]] </code></pre> <p>However for some reason this turned out to be really slow, even if <code>primes</code> has only <span class="math-container">$100$</span> elements it took more than a second. I suspect <code>AppendTo</code> creates a completely new list every time?</p> <p>The final thing I tried was to use <code>ReplacePart</code>, but the attempt is not even worth showing. First problem is that you need to initialize <code>squbes</code> to be large enough, even though I have no good idea how large it will be. Second problem is that I see no reason to assume that <code>ReplacePart</code> will not create a completely new list every time.</p> <p>Can you please help me simply create this list of squbes in a decent amount of time?</p> https://mathematica.stackexchange.com/q/201999 0 How to save a all matrices renewed in each iteration? Delaram Nematollahi https://mathematica.stackexchange.com/users/49217 2019-07-12T16:27:54Z 2019-07-12T19:46:36Z <p>I have a (n by m) matrix , in which the elements are recalculated and replaced by new ones in each iteration. (In other words I have a loop in which the elements of the mentioned matrix are being calculated and renewed in each step). What is the best way to save and export all the matrices?</p> <p>So as an example:</p> <pre><code> ne = 4 ns = 6 avec = Table[0, {ie, 1, ne}, {i, 1, ns}]; Dat = Table[avec[[ie, i]] = ie + 1; anew = avec; Export[NotebookDirectory[] &lt;&gt; "avecexample.xls", anew] , {ie, 1, ne}, {i, 1, ns}] </code></pre> <p>but this one only exports the last avec. My first question is how to save all of them ? and in the next step, I want to know in case of more iterations , how to chose particular avecs to save (I mean for example how to save/export only the last five avecs calculated in the loop)?</p> https://mathematica.stackexchange.com/q/201562 0 How can I create loop for this iteration integration [closed] zain ud din https://mathematica.stackexchange.com/users/66177 2019-07-04T15:59:03Z 2019-07-07T12:55:02Z <p>I have definite integral of the form <span class="math-container">$$f(x)=\int_{-\pi}^\pi\frac{x^2-1}{(x^2-1)^2+x^2+y^2+f(x)}dxdy$$</span> I want to make a code to calculate <span class="math-container">$f(x)$</span> numerically, starting from <span class="math-container">$f(x)=0$</span> and stopping when the solution converges.</p> https://mathematica.stackexchange.com/q/201128 5 Iterate over deepest values in a nested Association BOUNCE https://mathematica.stackexchange.com/users/61492 2019-06-27T20:08:10Z 2019-06-28T01:08:04Z <p>Is it possible to iterate over the values of the deepest layer of keys in a nested association? For example:</p> <pre><code>assoc = &lt;| "a"-&gt;0, "b"-&gt; &lt;| "c"-&gt;"one" ,"d"-&gt;2 , "e"-&gt;3|&gt;, "f"-&gt;4|&gt; </code></pre> <p>Would it be possible to iterate over just the values of at the deepest level in this example <code>{0,"one",2,3,4}</code>? Or, possible to iterate over the deepest level of keys such that I could test the type for the value of each key?</p> https://mathematica.stackexchange.com/q/201030 0 Function iteration bohnanza https://mathematica.stackexchange.com/users/66298 2019-06-26T11:46:54Z 2019-06-26T11:46:54Z <p>I wrote a function in Mathematica that applies certain construction steps on the "incidence matrix" of a graph in order to obtain a new graph with certain properties. These construction steps can be iterated and here is the point where I fail. If I write something like</p> <pre><code>Henneberg1[Henneberg1[{{{1,1,1,1}}},a,b],c,d] </code></pre> <p>I get a weird error of the following form: <a href="https://i.stack.imgur.com/SwQmq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SwQmq.png" alt="error when iterating function"></a></p> <p>Now to the implementation:</p> <pre><code>Henneberg1[mats_, a_, b_] := ... </code></pre> <p>The first parameter <code>mats_</code> describes a set of (incidence) matrices where <code>a</code> and <code>b</code> are only placeholders for variables to be added. If I evaluate <code>Henneberg1[{{{1,1,1,1}}},a,b]</code> I retrieve</p> <p><span class="math-container">$\{\begin{pmatrix} 1 &amp; 1 &amp; 1 &amp; 1 &amp; 0 &amp; 0 \\ a &amp; 1 &amp; 0 &amp; 0 &amp; -a &amp; -1 \\ 0 &amp; 0 &amp; b &amp; 1 &amp; -b &amp; -1 \end{pmatrix}\}$</span></p> <p>Directly passing the result as argument for <code>Henneberg1</code> then works as desired, but I am not able to iterate in the way described above.</p> <p>The actual implementation of <code>Henneberg1</code> can be found <a href="https://pastebin.com/CUyG0c7u" rel="nofollow noreferrer">here</a>, but is quite a mess, so sorry for that. (What it actually does can be found <a href="https://en.wikipedia.org/wiki/Laman_graph#Henneberg_construction" rel="nofollow noreferrer">here</a> (1.))</p> <p>If someone knows where the problem lies I would really appreciate that.</p> https://mathematica.stackexchange.com/q/200944 4 How do we optimise double Tables? William https://mathematica.stackexchange.com/users/2871 2019-06-24T17:00:03Z 2019-06-27T16:18:55Z <p>I have made the following code which I shall explain it in two parts: </p> <p>First Part: </p> <pre><code>ELCo = Alphabet["English"]; (*ELCo are the letters in English*) Characters[ToLowerCase[WordList[Language -&gt; "English"]]]; Select[%, SubsetQ[ELCo, ToLowerCase[#1]] &amp;]; ELPr = Map[Sort, Map[DeleteDuplicates, %]]; (*In the past two steps I turn English words into characters and to lowercase*) ELP = Length[ELPr]; ELPrPo = Total[x^Map[Length, ELPr]]; (*Here I make polynomials of english words based on their length, for example 3068 x^4 shows there are 3068 elements of size 4 and so on*) </code></pre> <p>This part of the code is fairly simple and easy to run. This is just the tool I need in second part of my code. </p> <p>Second Part: </p> <p>I first take the letters, there are 26 of them and make all the possible pairs: (there are 325 of them)</p> <pre><code>Pairs = Subsets[ELCo, {2}]; </code></pre> <p>Then, </p> <pre><code>Table[LU = Select[ELPr, SubsetQ[#1, Pairs[[j]]] &amp;]; (*I go through the original word list ELPr and select each pair to this part shall be done 325 times for each pair*) LUer = Total[x^Map[Length, LU]]; (*Then I make the new polynomial for each pair after selection*) LUerDelta = Expand[ELPrPo - LUer + LUer/x]; (*Use the new polynomial and the original one to make this equation*) Table[c = i; (27 - c)/ 27 (Coefficient[LUerDelta, x, c]/Coefficient[ELPrPo, x, c]) // N, {i, 1, 2}], {j, 1, Length[Pairs]}]; PairPerformance = AssociationThread[Pairs, %]; </code></pre> <p>In the last part for each new <code>LUerDelta</code> I want to make a ratio of the coefficients for <code>c= 1</code> and <code>2</code>, but having two table command into each other makes this process very slow, I was wondering if there is a better way of doing this? </p> https://mathematica.stackexchange.com/q/200425 3 Efficient way to iterate over large list and check conditions amasics https://mathematica.stackexchange.com/users/37366 2019-06-15T22:46:46Z 2019-06-16T23:08:17Z <p>The problem I am trying to solve is how to assign charges to a number of particles based on some conditions on the charges. It is conceptually very simple but requires processing a very large list. There are four possible charges: (0,1,2,3), assigned to 19 particles, so there are a total of <span class="math-container">$4^{19}$</span> lists of length 19 to loop over. My current attempt uses </p> <pre><code>Length[Select[Tuples[{0, 1, 2, 3}, 19], C1]] </code></pre> <p>(C1 is some condition), which seems to store all the lists in memory and crashes my computer. Since I only require to check how many possible assignments meet the required condition, I don't need to store any list in memory. What is the most efficient way to iterate over this very large list and solve the problem in a reasonable amount of time? Thanks in advance.</p> <p>ps. The conditions I want to check are simple conditions like the last three particles having the same charge or the sum of charges of some particles add to zero. For example, C1 is </p> <p><code>C1[charges_]:=Equal@@charges[[{-1,-2,-3}]]</code></p> <p>This is a simple case that can be calculated by hand, but I am trying to combine multiple conditions that make it hard to do so.</p> https://mathematica.stackexchange.com/q/198692 1 Iterating of piecewise function Javohir Usmonov https://mathematica.stackexchange.com/users/65317 2019-05-20T05:04:20Z 2019-05-20T13:53:37Z <p>I have this piecewise function <span class="math-container">$$f(x)=\begin{cases} x(1.5-0.5x) \quad\text{if x\le0.5},\\ x(0.5+0.5x) \quad\text{if x&gt;0.5}. \end{cases}$$</span> How can I calculate iterated functions for it?</p> https://mathematica.stackexchange.com/q/198429 0 How to create a list of undefined functions? Alex https://mathematica.stackexchange.com/users/65382 2019-05-15T20:21:03Z 2019-05-19T06:27:24Z <p>I have a <strong>undefined</strong> function <span class="math-container">$\phi[i,j]$</span> where <span class="math-container">$i=0,1,...,m$</span> and <span class="math-container">$i=0,1,...,n$</span>.</p> <p>How can I generate this list:</p> <p>{<span class="math-container">$\phi[0,0],\phi[1,0],...,\phi[m,0],\phi[0,1],\phi[1,1],...\phi[m,1],\phi[0,2],...,\phi[m,n]$</span>}</p> <p>where I have all the iterations of <span class="math-container">$i$</span> and <span class="math-container">$j$</span> on the <span class="math-container">$\phi$</span> function?</p> https://mathematica.stackexchange.com/q/194047 2 Solving for the recursion relation for the expansion coefficients of the asymptotic expansion of an ODE user583893 https://mathematica.stackexchange.com/users/59685 2019-03-27T14:33:03Z 2019-04-26T23:01:08Z <p>I want to solve for the asymptotic solution of the following differential equation</p> <p><span class="math-container">$$\left(y^2+1\right) R''(y)+y\left(2-p \left(b_{0} \sqrt{y^2+1}\right)^{-p}\right) R'(y)-l (l+1) R(y)=0$$</span></p> <p>as <span class="math-container">$y\rightarrow \infty$</span>, where <span class="math-container">$p&gt;0$</span>. I did the standard way by obtaining a series solution by the Frobenius method prescription in the form</p> <p><span class="math-container">$$R(y)=\sum_{n=0}^\infty \frac{a_{n}}{y^{n+k}}$$</span> where <span class="math-container">$k=l+1$</span> is the indicial exponent. I had difficulty finding, by hand, for a recurrence relation for the coefficients <span class="math-container">$a_n$</span> for arbitrary value of the parameter <span class="math-container">$p$</span>. Right now, I am just doing the brute force method of solving individual <span class="math-container">$a_n$</span> for every value of <span class="math-container">$p$</span>. </p> <p>But I am just wondering whether the recurrence relation is possible to obtain using Mathematica routine. Any help is appreciated.</p> https://mathematica.stackexchange.com/q/190574 1 How can I solve iteratively for $\Sigma(r)$, T(r) and h(r)? mattiav27 https://mathematica.stackexchange.com/users/8822 2019-01-31T12:56:13Z 2019-02-01T02:39:57Z <p>My naive attempt:</p> <pre><code>ClearAll["Global*"] Md = 10^(-9); P = 10; R = 10^4; α = 10^(-2); ϵ = 10^(-4); γ = 10^(-2); ke = 0.02*(1 + 0.6625); k0 = 5*10^20; σ = 5.67*10^-8; Rg = 8315; c = 3*10^8; G = 6.67/10^11; M = 2.8*10^30; Ωk[r_] := Sqrt[(G*M)/r^3]; μ = Md/(3*Pi); κ = (27*ke)/(2*σ) Rg/μ; Co[r_] := 1; β[r_] := 0; Do[Σ[ r_] := κ^(-1/5) α^(-4/5) μ^(3/ 5) Ωk[r]^(2/5)*Co[r]^(-1/5); h[r_] := (κ α Σ[r]^2 Ωk[ r]^(-5) Co[r])^(1/6); T[r_] = 1/2 μ/Rg (Ωk[r]^2 h[r]^2)/(1 + β[r]); Kkr[r_] := k0 Σ[r]/h[r]*T[r]^(-7/2); β[r_] := μ/Rg (4 σ)/(3 c) T[r]^3/Σ[r] h[r]; (*Co[r_] := (1 + β[r])^4*(1 + Kkr[r]/ke)*), {2}] Plot[Σ[r],{r,10^4, 10^10}] Plot[h[r],{r,10^4, 10^10}] Plot[T[r],{r,10^4, 10^10}] </code></pre> <p>to solve iteratively the system of equations:</p> <p><span class="math-container">$$\begin{array} &amp;\mu=\dfrac{M_d}{3\pi}\\ &amp;\rho(r) = \Sigma(r)\, h(r)\\ &amp;\Omega_K(r)= \sqrt{\dfrac{G M}{r^3}}\\ &amp;\beta(r) = \dfrac{\mu}{Rg}\, \dfrac{4\sigma}{3c}\, \dfrac{T^3(r)}{\rho(r)}\\ &amp;T(r) =\dfrac{1}{2} \, \Omega_k^2(r)\, h^2(r)\, \dfrac{1}{1+\beta(r)}\, \dfrac{\mu}{Rg} \\ &amp;h^6(r)=\kappa \alpha \Sigma^2(r) \, \Omega_k(r)^{-5}C(r)\\ &amp;\Sigma(r) = k^{-1/5}\,\alpha^{-4/5}\mu^{3/5}\, \Omega_k^{2/5}(r)\, C^{-1/5}(r)\\ &amp;C(r)=(1-\beta(r))^4 \left( 1+\dfrac{Kkr}{ke} \right)\\ &amp;Kkr(r)=ko \rho(r) \, T^{-7/2}\\ \end{array}$$</span></p> <p>fails. How can I solve it?</p> <p>The iteration procedure goes like this:</p> <ul> <li>Impose <span class="math-container">$C=1$</span> and <span class="math-container">$\beta(r)=0$</span></li> <li>Solve for <span class="math-container">$\Sigma$</span></li> <li>Solve for <span class="math-container">$T$</span> and <span class="math-container">$h$</span></li> <li>Calculate <span class="math-container">$C$</span> and <span class="math-container">$\beta$</span> and <span class="math-container">$Kkr(r)$</span></li> <li>Solve for <span class="math-container">$\Sigma$</span></li> <li>...</li> </ul> https://mathematica.stackexchange.com/q/190563 -1 Is there a way to get values from a set of functions with circular dependencies? [closed] mattiav27 https://mathematica.stackexchange.com/users/8822 2019-01-31T08:27:35Z 2019-01-31T21:48:03Z <p>I am using the following code to find iteratively the functions <span class="math-container">$\Sigma(r)$</span>, <span class="math-container">$h(r)$</span> and <span class="math-container">$T(r)$</span></p> <pre><code>ClearAll["Global*"] Md = 10^(-9); P = 10; R = 10^4; α = 10^(-2); ϵ = 10^(-4); γ = 10^(-2); ke = 0.02*(1 + 0.6625); k0 = 5*10^20; σ = 5.67/10^8; Rg = 8315; c = 3*10^8; G = 6.67/10^11; M = 2.8*10^30; Ωk[r_] := Sqrt[(G*M)/r^3]; μ = Md/(3*Pi); κ = ((27*ke)/(2*σ))*(Rg/μ); Co[r_] := 1; β[r_] := 0; Do[Σ[r_] := (μ^(3/5)*Ωk[ r]^(2/5))(κ^5^(-1)*α^(4/5)*Co[r]^5^(-1)); h[r_] := (κ*α*Σ[r]* Co[r])/Ωk[r]^5; T[r_] := (1/2)*Ωk[r]* h[r]^2*(μ/Rg)*(1/(1 + β[r])); Kkr[r_] := (k0*(Σ[r]/h[r]))/T[r]^(7/2); β[r_] := (μ/Rg)*((4*σ)/(3*c))*(T[r]^3/(Σ[r]/h[r])); Co[r_] := (1 + β[r])^4*(1 + Kkr[r]/ke), {2}] Plot[Σ[r],{r,10^4, 10^10}] Plot[h[r],{r,10^4, 10^10}] Plot[T[r],{r,10^4, 10^10}] </code></pre> <p>The problem is that the last line <code>Co[r_] := (1 + β[r])^4*(1 + Kkr[r]/ke)</code> makes the kernel crash and I don't understand why.</p> <p>I am using version 10.0.</p> https://mathematica.stackexchange.com/q/188217 0 How to use a test function correctly to terminate a While loop? user55777 https://mathematica.stackexchange.com/users/55777 2018-12-20T11:32:42Z 2018-12-21T00:58:28Z <p>I define a test function in order to stop my <code>While</code> loop:</p> <pre><code>J[g_, phiT_, y_] := NIntegrate[Dot[g[y], g[y]], {y, Ly, Uy}]/NIntegrate[Dot[phiT[y], phiT[y]], {y, Ly, Uy}]; </code></pre> <p>where <code>Ly</code> and <code>Uy</code> are constants to specify the spatial domain, both <code>g[y]</code> and <code>phiT[y]</code> are functions that are obtained within a loop. </p> <p>In my code, I want to evaluate and save <code>J[[iter]]</code> continuously in the loop with an iterator <code>iter</code>. Then <code>J[[iter]]</code> will be used for stopping the <code>While</code> loop as follows:</p> <pre><code>While[True, (*some code here*) J[[iter]] = J[g, phiT, y]; AppendTo[J, J[[iter]]]; If[Abs[J[[iter]] - J[[iter - 1]]] &lt; acc, Break[]]; (*some code here*) iter++;]; </code></pre> <p>As indicated by @xzczd, <code>J[[iter]] = J[g, phiT, y]</code> is meaningless. My understanding is as follows:</p> <p><strong><code>J[[iter]]</code> means that one is going to access an element (indexed by <code>iter</code>) of a <em>list</em>, named <code>J</code>. However, <code>J</code> is the name of a <em>fucntion</em> defined in the beginning. This is an apparent contradiction.</strong></p> <p>But I cannot figure out how to resolve this problem. The main reason, I think, is that I do not know how many loops are needed and that's why I used the <code>While</code> loop.</p> <p>In contrast, if I know how many loops are involved, then I can come up with, for example, the following code using <code>Do</code> loop:</p> <pre><code>ti = 0; tf = 10; \[Delta]t = 0.01; ItrNo = Round[(tf - ti)/\[Delta]t]; Jtest = ConstantArray[0, ItrNo - 1]; Do[(*some code here*); Jtest[[iter]] = J[g, phiT, y]; (*some code here*), {iter, ItrNo - 1}] </code></pre> <p>Can anybody help me? Thank you in advance!</p> <p><strong>Here is a small example</strong>, though may not be minimal, since I want to use <a href="https://mathematica.stackexchange.com/questions/127980/dynamic-euler-bernoulli-beam-equation/127997#127997">pdetoode</a>. Please copy the function <code>pdetoode</code> from the above link.</p> <pre><code>feqSolver[s_, T_, g_] := Module[{s0 = s, Tend = T, feqIC = g}, With[{\[Phi] = \[Phi][t, y]}, feq = {D[\[Phi], t] + y*D[\[Phi], y] - D[\[Phi], {y, 2}] - (s0 - 1/20*y^2)*\[Phi] == 0}; fic = {\[Phi] == feqIC[y]} /. t -&gt; 0; fbc = {{\[Phi] == 0} /. y -&gt; Ly, {\[Phi] == 0} /. y -&gt; Uy};]; fptoofunc = pdetoode[\[Phi][t, y], t, grid, difforder]; fdel = #[[2 ;; -2]] &amp;; fode = fdel@fptoofunc@feq[]; fodeic = fptoofunc@fic; fodebc = fptoofunc@fbc; fsollst = NDSolveValue[{fodebc, fodeic, fode}, Map[\[Phi], grid], {t, 0, Tend}]; fsol = ListInterpolation[Developer`ToPackedArray@#["ValuesOnGrid"] &amp; /@ fsollst//Transpose, {Flatten@fsollst[]["Grid"], grid}]; phisol[t_, y_] = \[Phi][t, y] /. \[Phi] -&gt; fsol[];] s0 = 0.4; Tend = 5; acc = 10^-4; Ly = -20; Uy = 20; iter = 1; (*Random IC*) SetAttributes[g0, Listable]; SeedRandom; g0[y_?NumericQ] = BSplineFunction[Join[{0.}, RandomReal[{-1, 1}, 39], {0.}], SplineClosed -&gt; False][(y + 20)/40]; g[y] = g0[y]; J[g_, phiT_, y_] := g[y]/phiT[y]; While[True, gOld[y] = g[y]; phisol[t, y] = feqSolver[s0, Tend, gOld]; phiT[y] = phisol[Tend, y]; Jtest[[iter]] = J[g, phiT, y]; AppendTo[Jtest, Jtest[[iter]]]; If[Abs[Jtest[[iter]] - Jtest[[iter - 1]]] &lt; acc, Break[]]; g[y] = -1/2*phiT[y]; iter++;]; </code></pre> https://mathematica.stackexchange.com/q/188145 0 How can I make my code containing a For loop more Mathematica-style? Klangen https://mathematica.stackexchange.com/users/49819 2018-12-19T08:41:04Z 2018-12-19T08:56:41Z <p>I know there are a lot of questions on this site regarding the use of <code>For</code> loops and how to avoid them in <em>Mathematica</em>. However, in my case I believe it is justified, as I will show below. My issue is that for very large amount of iterations, the kernel crashes, and so I am looking for a more <em>Mathematica</em>-style solution to my problem.</p> <hr> <p>The aim of my code is to compute successive values of an integral between two points <code>a</code> and <code>b</code> in a cumulative way, i.e., feed back the value of the previous integral into the new value and perform some calculations with the intermediate value. Here is a short code snippet that illustrates my situation:</p> <pre><code>MyIntegral[a_, b_]:= ...; numericalLimit:=10^20; For[i=0, i&lt;numericalLimit, i++, ( myIntermediateValue=MyIntegral[i,i+1]; (* ... some computations with myIntermediateValue *) myCumulatedValue+=myIntermediateValue; (* ... some computations with myCumulatedValue *) )] </code></pre> <p>So as you can see, I need the iterative nature of the <code>For</code> loop in order to work with my cumulated value. Needless to say, as it has been reiterated before on this website (pun not intended), <code>For</code> loops are evil. Moreover, for very large values of <code>numericalLimit</code> the kernel crashes. So my questions are:</p> <ul> <li>How can I make this code more <em>Mathematica</em>-like?</li> <li>How can I optimize the computations so that my kernel does not crash for large values of <code>numericalLimit</code>?</li> </ul> <p>Other answers on this website suggest using <code>Table</code> in similar situations, but I fear that it will actually be <em>less</em> efficient when considering <span class="math-container">$10^{20}$</span> copies, moreover I don't see how I will be able to cumulate my intermediary results.</p> <p>Any help is appreciated.</p> https://mathematica.stackexchange.com/q/188004 3 Iteration on a matrix ithilquessirr https://mathematica.stackexchange.com/users/61024 2018-12-16T20:01:54Z 2018-12-17T14:16:21Z <p>I want to update "matrix1" 100 times. "matrix3" will be new "matrix1" and it will iterate 100 times. Should I use a loop or function? First iteration is:</p> <pre><code>matrix1 = ( { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } ); matrix2 = matrix1*2 - 1; matrix3 = matrix2 + 5; matrix3 </code></pre> <p>Output (First iteration):</p> <pre><code>{{6, 8, 10}, {12, 14, 16}, {18, 20, 22}} </code></pre> <p>matrix3 will be new matrix1</p> <pre><code>matrix1 = ( { {6, 8, 10}, {12, 14, 16}, {18, 20, 22} } ); matrix2 = matrix1*2 - 1; matrix3 = matrix2 + 5; matrix3 </code></pre> <p>Output (Second iteration):</p> <pre><code>{{16, 20, 24}, {28, 32, 36}, {40, 44, 48}} </code></pre> <p>And, It will repeat 100 times.</p> https://mathematica.stackexchange.com/q/187696 3 Working Iterative formula for a system of equations Andrew Bradley https://mathematica.stackexchange.com/users/61856 2018-12-11T12:17:49Z 2018-12-11T13:46:21Z <p>I have been given a project, I need to show the use of a version of Newton's method to solve these non-linear equations. The version of Newton's method I am required to use is: <span class="math-container">$X_{n+1} = X_n - J^{(-1)} F(X_n)$</span>. I have all the values required here, and this works to find the point <span class="math-container">$X_1$</span>. However I need to find a code that inputs the following points <span class="math-container">$X_2, X_3, ..., X_n$</span> automatically. Here are the given values:</p> <pre><code>f[x_, y_] := x^2 + y^2 - 5; g[x_, y_] := x^3 - y^3 - 7; x0 = 2.1; y0 = 0.9; f[x0, y0] g[x0, y0] </code></pre> <blockquote> <p>0.22 </p> <p>1.532</p> </blockquote> <pre><code>M = {{2*x0, 2*y0}, {3*x0^2, -3*y0^2}} </code></pre> <blockquote> <p>{{4.2, 1.8}, {13.23, -2.43}}</p> </blockquote> <pre><code>J = Inverse[M] </code></pre> <blockquote> <p>{{0.0714286, 0.0529101}, {0.388889, -0.123457}}</p> </blockquote> <pre><code>F0 = {{f[x0, y0]}, {g[x0, y0]}} X0 = {{x0}, {y0}} X1 = X0 - J.F0 </code></pre> <blockquote> <p>{{0.22}, {1.532}}<br> {{2.1}, {0.9}}<br> {{2.00323}, {1.00358}}</p> </blockquote> <p>Thank you in Advance!</p> https://mathematica.stackexchange.com/q/187281 0 Plotting Data Iteratively Matthew Jacobson https://mathematica.stackexchange.com/users/61719 2018-12-04T03:44:51Z 2019-08-31T13:02:30Z <p>I have a function of x1 that I can plot in the form y=f(x1). However, I have a relation y=f(x1)=f(x2), where I don't know the form of f(x2), but I can solve for x2 given f(x1) and I want to plot both on the same axis. What I mean is that I have a range of values for y, which I plot for different x1 and x2, which I'd like to have on the same axis. </p> https://mathematica.stackexchange.com/q/186023 1 Question about approximate squaring iteration [closed] Brandon J. https://mathematica.stackexchange.com/users/61370 2018-11-15T09:49:15Z 2018-11-18T15:28:06Z <p>the iteration map <span class="math-container">$x*\left\lceil x \right\rceil$</span>, where the initial term is a non integer rational number <span class="math-container">$b/a$</span> with <span class="math-container">$b&gt;a$</span> and <span class="math-container">$a&gt;1$</span>. the growth rate of this iteration is similar to the growth rate of Fermat numbers(!). If the initial term is (for example) <span class="math-container">$200/199$</span>, then how we know that after <span class="math-container">$1000$</span> steps the number becomes an integer or not ? Because the number at <span class="math-container">$1000$</span> steps is very very HUGE number(!). I tried residues method, but that didn't work at all !. Is there a simple method to check this out ?</p> https://mathematica.stackexchange.com/q/185654 0 Table of convolutes dantopa https://mathematica.stackexchange.com/users/29452 2018-11-09T04:36:54Z 2018-11-09T04:43:10Z <h2>Inputs</h2> <p>Start with a vector <span class="math-container">$x\in\mathbb{R}^{n}$</span>, and a vector-valued function <span class="math-container">$f:\mathbb{R}^{n}\mapsto\mathbb{R}^{n}$</span>.</p> <h2>Output</h2> <p>Given some <span class="math-container">$k\in\mathbb{Z}^{+}$</span>, what are elegant ways to produce the table of convolutes?</p> <pre><code>{x, f(x), f(f(x)), f(f(f(x))), ..., f^{k}(x)} </code></pre> https://mathematica.stackexchange.com/q/184560 8 Simple Fractal square Tom De Vries https://mathematica.stackexchange.com/users/721 2018-10-24T18:23:30Z 2018-10-25T13:54:30Z <p>I am working on a math question about infinite series, and one of the question images is below.</p> <p><a href="https://i.stack.imgur.com/CYBPm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/CYBPm.png" alt="iterations of subdividing a square"></a></p> <p>Each new white square has an area that is 1/4 of the previous square.</p> <p>Always looking to learn elegant ways to create things using Mathematica, and in this case, probably recursion as well?</p> <p>I know it's not complicated, but any help with the process would be appreciated.</p> <p>Having a NICE diagram really helps with creating a better response. (questions about sums of areas of white, black, etc.)</p> https://mathematica.stackexchange.com/q/184354 1 FixedPoint doesn't work Erik Johnsson https://mathematica.stackexchange.com/users/60222 2018-10-21T17:33:47Z 2019-07-29T09:53:41Z <p>Do you know why it doesn't print any result?I set intial value is 0.1,</p> <pre><code>h[x_] = Sin[x]; (*Plot[{x,h[x]},{x,1,50}]*) FixedPoint[h[#] &amp;, 0.1]; </code></pre>