# Tag Info

44

Let you have a function and an initial point f[x_] := Cos[x] x0 = 0.2; Then you can calculate a sequence seq = NestList[f, x0, 10] (* {0.2, 0.980067, 0.556967, 0.848862, 0.660838, 0.789478, \ 0.704216, 0.76212, 0.723374, 0.749577, 0.731977} *) and vizualize it with a so-called Cobweb plot p = Join @@ ({{#, #}, {##}} & @@@ Partition[seq, 2, 1]); ...

37

UPDATE I thought it would be neat to try and animate the thing, so I let the $a$ parameter run between $-\pi$ and $\pi$. I generated 600 images and put them together using ffmpeg. Check it out on youtube. It might not be in the spirit of Mathematica Stack Exchange, but allow me an objection - stuff that is slow in Mathematica should be kept out of it. To ...

36

As @RahulNarain says, forming the image point by point saves significant memory because the number of image pixels is typically much smaller than the hundreds of millions of iterations that compose it. Therefore, iterate the attractor equations, and for each point generated, find its location within the image matrix. Colour coding of the number of hits in ...

26

str = First@ SubstitutionSystem[{"A" -> "AB+BA+B", "B" -> "B+AAB"}, "A", {7}]; asc = <|"A" -> {1, 0}, "B" -> {0, Pi/2}, "+" -> {0, -Pi/2}|>; Here {1,0} means go forward 1 step and turn 0 radians. The turtle graphics substitute in Mathematica is AnglePath. Graphics[ Line@AnglePath@Lookup[asc, Characters[str]] ] Thanks to @Pillsy, ...

25

I revisited this problem - this time in pure Mathematica. The trick to any kind of performance is the Compile[] function, which in itself can be a bit moody - so you need to set global options to warn you when it refuses compilation and work around that. The performance I'm seeing is on the order of magnitude slower than that I get from C++, and two orders ...

25

What you need is something like this: patt = Null | (x_ /; MatchQ[x, {_Integer, patt}] ) The trick is to delay the evaluation for the recursive part, until run-time (match attempt), and Condition is one way to do it. So: MatchQ[#, patt] & /@ {Null, {4, Null}, {3, {4, Null}}, {2, {3, {4, Null}}}, {1, {2, {3, {4, Null}}}}} (* {True, True, True, True, ...

22

One way is to use an extra argument that acts as a switch. Clear[f]; f = 1; f = 1; f[n_, True] := f[n - 1] + f[n - 2] Example: f7 = f[7, True] (* Out= f + f *) To proceed another step, can do a replacement. f7 /. f[aa_] :> f[aa, True] (* Out= f + 2 f + f *) Can use Nest to repeat this n times. Nest[# /. f[aa_] :> ...

20

What you have is what's known in some circles as a linear recurrence. That is, you have a sequence where the general term can be expressed as an appropriate combination of previous terms. The simplest way of going about it, as has been previously noted, is to use either of Nest[]/NestList[], like so: NestList[Append[#, Total[Take[#, -2]]] &, {0, 2, 2, ...

17

I'm pretty sure this is a duplicate but I spent 15 minutes looking for it and couldn't find it, so I'm just going to answer for now. Instead of using Listable you can manually map over non-vector lists: f[v_?VectorQ] := oper[v] f[ls_List] := f /@ ls; f[{{1, 2}, {3, 4}, {5, 6, 7}}] {oper[{1, 2}], oper[{3, 4}], oper[{5, 6, 7}]} Extension In an attempt to ...

16

If you can rewrite the recursion to be tail recursive, you will not run into recursion limits. Here is an example of a tail-recursive implementation of factorial. factorial[1, val_: 1] = val; factorial[k_Integer /; k > 1, val_: 1] := factorial[k - 1, k val] factorial /@ Range {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800} Block[{$... 16 The kernel crashes due to stack overflow. It is not safe to recurse too deeply. Increasing$RecursionLimit to values that are too great (and actually recursing that deep) risks a crash. (So yes, in a way it's due to insufficient memory, but it has nothing to do with memoization. It is due to insufficient stack space.) From the documentation: On most ...

15

Is Tribonacci defined? First you should notice that Tribonacci is not already defined by Mathematica. Compare the defined Fibonacci ?Fibonacci with ?Tribonacci You could have guessed by the color of the function in the front-end display, black for defined and blue for undefined. Fibonacci n-Step Number Now we can define the even more general ...

15

Suggested solution If I understood the question right, then the simplest solution here would probably be to define a helper function like the following: vv[n_] := Internal`InheritedBlock[{v}, v /@ Range[n]]; Then, you get vel = vv[m] and every run of vv would result in different set of values, while the values in the set will all come from the same ...

15

Using Annuity: pmt /.Solve[TimeValue[Annuity[pmt, 52, 1], .02, 0] == 5000, pmt] 155.545 Exercise left for the reader...

15

Here's a starting point for you: With[{n = 6}, Graphics[MapIndexed[{ColorData @@ #2, #1} &, NestList[MapAt[Composition[ TranslationTransform[AngleVector[2 π/5]/ GoldenRatio], ...

14

You are using the same dummy variable for all integrals. Extended answer Modify your code slightly and note that all integrals use the same dummy: BallVolume[dimension_, radius_] := If[dimension == 0, 2*radius, Assuming[radius > 0, testIntegrate[ BallVolume[dimension - 1, Sqrt[radius^2 - x^2]], {x, -radius, radius}]]]; BallVolume[2, ...

14

It is so-called Rabbit sequence. One can notice that at each step $$0 \to 1, \quad 1 \to 10.$$ The substitution $0\to1$ corresponds to young rabbits growing old, and $1\to10$ corresponds to old rabbits producing young rabbits. fib[n_] := Nest[Flatten[# /. {0 -> {1}, 1 -> {1, 0}}] &, {1}, n] fib (* {1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0} *) ...

14

Since you are using this as an excuse to learn to code in Mathematica, I'll try to help with that in mind. Sometimes I find it nice to design "from the top down", document from the top down, split in many small functions with no state, and go testing them "from the bottom up". As you gain confidence, you will perhaps use coarser functions, and not test every ...

14

A number of points: First, if you need to compute several values of your sequence, your intial memo-ized implementation will NOT run into recursion limit problems. Second, if you need to compute very few values, this method is extremely inefficient - the $n$-th Fibonacci number (or the $n$-th term of a linear recurrence in general) can be computed in $\... 14 Not sure if this is what you are looking for, but let´s see (wash, rinse, repeat): test = {Null, {4,Null}, {3, {4,Null}}, {2, {3, {4,Null}}}, {1, {2, {3, {4, Null}}}}, {}, {Null}, {Null, Null}, {3,4, Null}}; MatchQ[Null, # //. {_Integer, Null} -> Null] & /@ test (*{True, True, True, True, True, False, False, False, False}*) 14 Given asso = <|a -> tr, b -> <|c -> <|d -> 12, e -> del|>|>, f -> g|> then directly Level[asso, {-1}] {tr, 12, del, g} See this post where you can find clear illustrations of "how work levels". 13 flip = True; var := flip = Not[flip] var (*False*) var (*True*) var (*False*) 13 Using NestWhile seems to work well z[n_, c_] := NestWhile[(#^2 + c) &, c, Abs[#] <= 2 &, 1, n]; ContourPlot[Abs[z[iter, x + I*y]] == 2, {x, -.00001, .00001}, {y, .99999, 1.00001}, MaxRecursion -> 5] // AbsoluteTiming producing the plot in the question in about 10 seconds, as opposed to 180 seconds for the code in the question. 13 Recursive Function Let's start with simple recursive function provided by @corey979: ClearAll[fRecursive] fRecursive = 2; fRecursive[n_] := fRecursive[n] = Count[Table[fRecursive[k], {k, 1, n-2}], fRecursive[n - 1]] It works as expected: Array[fRecursive, 15] (* {2, 0, 0, 1, 0, 2, 1, 1, 2, 2, 3, 0, 3, 1, 3} *) but it's a bit slow: Table[... 13 You were correct. NestList is exactly the function you want to use. NestList[Dot[A, #]&, x0, 5] (* {{2, 0}, {1., 1.5}, {-0.4, 2.4}, {-1.64, 2.34}, {-2.224, 1.344}, {-1.9184, -0.1896}} *) Note that the first argument of NestList must be a function. 12 In this answer, I will use the Functional Paradigm to deal with triangular recursive formula in a uniform manner. For the triangular recursive formula $$T_k^{(n)}=f(T_{k-1}^{(n)},T_{k-1}^{(n+1)})$$ In general,$f(x)=a x+b\$, so the triangular recurisive formula can be denoted as below: $$T_k^{(n)}=\alpha(k,n) T_{k-1}^{(n)}+\beta(k,n)T_{k-1}^{(n+1)}$$ ...

12

Why not just use a recursive definition like you would for a regular Fibonnaci function? ClearAll[fibjoin] fibjoin = {1}; fibjoin = {1, 0}; mem : fibjoin[n_] := mem = Join[fibjoin[n - 1], fibjoin[n - 2]] fibjoin (* {1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0} *)

12

Defining the "Collatz"-Function like you did is straight-forward, but in the sense of Mathematica not optimal. When computing the length of a Collatz-Sequence a lot of duplicate calculations are done. So defining: collatz[n_] := collatz[n] = If[EvenQ[n], n/2, 3*n + 1] prevents Mathematica from doing duplicate evaluation. This is more efficient than ...

12

LinearRecurrence is useful here: LinearRecurrence[{1, 1, 1}, {1, 1, 2}, 9] {1, 1, 2, 4, 7, 13, 24, 44, 81} Related: Fibonacci Sequence Generator How to deal with recursion formula in Mathematica?

12

coords = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; tf = Composition[TranslationTransform[{1/2, 0}], ScalingTransform[{1/2, 1/2}]] n = 4; rects = NestList[tf /@ # &, coords, n]; Graphics[{EdgeForm[Black], Rectangle[], White, Polygon[Most/@rects], Polygon@Last@rects}] Row @ Table[With[{rects = NestList[tf /@ # &, coords, n]}, Graphics[{ EdgeForm[Black], ...

Only top voted, non community-wiki answers of a minimum length are eligible