Tag Info

Hot answers tagged

8

i = Module[{k = 100000, m = 5, data, dt, t}, data = (dt = .0001 + .0005 #; t = -dt; NestList[{Cos[4 #[[2]]] Cos[t += dt], Sin[#[[1]]] Sin[t]} &, RandomReal[{0, 2 π}, 2], k]) & /@ Range[0, m]; ListPlot[data, AspectRatio -> 1, PlotRange -> {{-1, 1}, .5 {-1, 1}}, Axes->None, ...


7

You can do this without Table: g[k_] := k^2 + 1 Map[If[# != 6, f[#] = g[#], f[#] = 1] &, Range[10]] {2, 5, 10, 17, 26, 1, 50, 65, 82, 101} You can also use Scan in place of Map (it may be slightly faster for large lists). Note that no result will be displayed, but f will still retain the values stored in it. Scan[If[# != 6, f[#] = g[#], f[#] = ...


6

A scan operation doesn't really have an index, but you can get the effect you want by introducing a counter. Module[{i = 0}, Scan[(i++; Print[i, ", ", #]) &, {2, 5, 7}]] You might also consider using Do, which does have an index. With[{data = {2, 5, 7}}, Do[Print[i, ", ", data[[i]]], {i, Length @ data}]]


6

You can use FixedPoint: FixedPoint[Log[3.5 #] &, 3.5] == -ProductLog[-1, -1/3.5] True In general, if $y = f(f(...f(f(...))))$, then $y = f(y)$. Solving for $y$ will give us the formula for the infinitely nested expression. In your case, f == Log[x #]&, which gives sol = Refine[Reduce[y == Log[x y], y], x > 0] Unfortunately ...


6

Based on your code, we can first create a function PoAGen to generate mean PoA values as follows: Clear[PoAGen] PoAGen[nodes_, links_, n_: 1000] := Module[ {an = 10, al = 1, s, M, id, od, wd, x, poa, PoA}, Cases[_?NumericQ]@ Table[s = DirectedGraph[RandomGraph[{nodes, links}], "Acyclic"]; M = al*Transpose[AdjacencyMatrix[s]]; id = an + ...


5

Use With: Table[With[{v = varx[[i]]}, D[#1, v] &], {i, 3}]` (* {D[#1, x1] & , D[#1, x2] & , D[#1, x3] & } *) See the section "Scope" of the documentation page for With. Note that Function (&) has the attribute HoldAll, so that the value of varx[[i]] needs to be inserted into the function. The above gives a list of operators. An ...


5

MapIndexed[Print[Row[{First@#2, #1}, ","]] &, {2, 5, 7}]; 1,2 2,5 3,7 Or simpler: MapIndexed[Print[First@#2, ",", #1] &, {2, 5, 7}] 1,2 2,5 3,7


5

The goal of the question is to solve a system of coupled linear differential equations representing a two-component wave function depending on two spatial coordinates x, y, and one time variable t. The system is governed by a time-independent Hamiltonian that acts on the two components and the spatial coordinates. The question contains a matrix version of ...


4

Of course you can use the If also inside the table: Table[If[k != 6, f[k] = g[k], f[k] = 1], {k, 10}] You can also use the If on the right hand side of the assignment: Table[f[k] = If[k != 6, g[k], 1], {k, 10}] If the whole purpose of the table is the assignment, that is you are not really interested in the list it outputs, you can replace Table with ...


4

In addition to RunnyKine's recommendation of If here are two other methods to consider. 1: Unset or restore the value Simply Unset f[6] afterward: Do[f[k] = g[k], {k, 10}]; f[6] =. Array[f, 10] {g[1], g[2], g[3], g[4], g[5], f[6], g[7], g[8], g[9], g[10]} Note f[6] is returned as it is undefined. Or if f[6] already has a value save and restore it: ...


4

Table has attribute HoldAll. This means its arguments are left unevaluated: Attributes[Table] (* {HoldAll, Protected} *) Using an Evaluate will force the evaluation order to be as you desire: x = {i, 5}; Table[i, Evaluate@x] (* {1, 2, 3, 4, 5} *)


4

There may be a better way to approach this problem. If you transform your function f[t] into the frequency domain ft[w], convolution becomes multiplication, and the iterative convolution of f[t] with itself becomes raising ft[w] to a power. Here is a simple example where f[t_] := Exp[-t^2/2]; ft[w_] := FourierTransform[f[t], t, w] Now you can plot the ...


4

chainTable[expr_, itr : {sym_, __?NumericQ | {__}} ..] := Apply[Join, Map[Function[Null, Table[expr, #], HoldAll], Unevaluated@{itr}]]; SetAttributes[chainTable, HoldAll]; (* As per Jacob's comment *) chainTable[x^2, {x, 1, 9}] {1, 4, 9, 16, 25, 36, 49, 64, 81} chainTable[x^2, {x, 1, 9}, {x, 10, 90, 10}] {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 400, 900, ...


4

Plot has attributes, HoldAll, so the Sum isn't evaluated, try this: Plot[Evaluate[Sum[a^10*(-1)^(x - a), {a, 1, x}]], {x, 1, 10}] Where the sum evaluates to: before plotting: related questions/answers: Difference in Plot when using Evaluate vs when not using Evaluate How and when to use Evaluate? Behavior of expression evaluation in Plot I ...


4

There are two easy approaches that come to mind. The first is to simply use MapThread. MapThread[foo, {Range[0, 4], Range[24, 32, 2]}] {foo[0, 24], foo[1, 26], foo[2, 28], foo[3, 30], foo[4, 32]} If you are collecting all results this is probably a perfectly good approach as memory is unlikely to be a problem. The second is to define the ...


4

functionList = {Sin[x], x^2, Exp[x], x^3}; nodes = {0, 1, 2, 3, 4}; f[x_] = Piecewise[Transpose[{functionList, #1 <= x <= #2 & @@@ Partition[nodes, 2, 1]}]] Plot[f[x], {x, 0, 4}]


4

Here's a way that allows you to specify the colors of each interval. colors={Red, Blue, Green, Orange}; Show[Thread[ p[functionList, Partition[nodes, 2, 1] /. {a_, b_} :> {x, a, b}, Thread[PlotStyle -> colors]]] /. p -> Plot, PlotRange -> Automatic]


4

Using Sjoerd's example: functionList = {Sin[x], x^2, Exp[x], x^3}; nodes = {0, 1, 2, 3, 4}; Plot[Evaluate@MapThread[ConditionalExpression, {functionList,Thread[Most @ nodes < x <= Rest @ nodes]}],{x,0,4}] or Plot[Evaluate@Thread[ConditionalExpression@@ {functionList,Thread[Most @ nodes < x <= ...


4

Here we go using FoldList list = Array[{Subscript[a, #], Subscript[b, #]} &, {3, 1}] // Flatten /@ # & (* or Flatten[#,1]& doing the same, but faster *) FoldList[f[#, Sequence @@ #2]&, x, list] EDIT, according suggestion from Shutao Tang to create the initial list in a more elegant and efficient way: list={Subscript[a, #], ...


3

con[f_, g_, x_, l_, u_] := Integrate[(f /. x -> x ) (g /. x -> (y - x)), {x, l, u}]; func[u_, ll_, ul_] := Nest[con[#, PDF[NormalDistribution[], x], x, ll, ul] &, PDF[NormalDistribution[], x], u]; Using integration limits of -1 and 1: tab = Table[func[j, -1, 1], {j, 5}]; Plot[tab, {y, -3, 3}, PlotLegends -> Range[5]] gives: EDIT ...


3

I'm going to step out on the limb and answer re: what is almost certainly the "problem" - you are repeatedly evaluating to the same result, an aspect of recursive functions, so you're creating excess work. By example of Fibonacci numbers: fib[1] = 1; fib[2] = 1; fib[n_] := fib[n - 1] + fib[n - 2]; fib[5] // Timing fib[30] // Timing (* {0., 5} ...


3

Maybe this will do what you want ClearAll[q] q[1, B_, A_, f_] := f q[n_ /; n > 1, B_, A_, f_] := Block[{\[Omega]}, q[n, B, A, f] = Function[x, Evaluate[ Integrate[ q[n - 1, B, A, f][\[Omega]] f[x - \[Omega]], {\[Omega], B, A}]] ] ] Plotting q2 will now be very fast, as Mathematica will evaluate the integral for q2 to a ...


3

Following belisarius' suggestion you might use something like: SetAttributes[f, HoldFirst] f[body_, n_Integer?Positive, m__:3] := Block[{a}, Do[body, ##] & @@ Thread[{Array[a, n], m}] ] Use: f[Print[a[1], a[2], a[3]], 3] f[Print[a[1], a[2]], 2, 4] f[Print[a[1], a[2]], 2, 5, 7] How are parameters evaluated for a Plot in Manipulate The second ...


3

I figured it out, I need to use Item indexer, but item index starts from 0, not 1: If you write a class in C# and give it an indexer, the compiler creates a public property named Item for you. This is a parameterized property, meaning that it takes an argument like a method call. The indexer syntax is just a shorthand for calling the Item property. See ...


3

If you're only using hundreds of iterations, then Table as @george2079 suggests in a comment is the simplest way to go. If you're looking at millions of iterations, then using the vectorized property of the built-in functions is more efficient. Below, 100 million values are computed in a little over 3 seconds (Mac 2.7GHz 16GB i7): V0 = 1.; ΔV = 2.; T = 5; ...


3

You can use a Table with variable iterator limit. e.g. Table[f[i, r], {i, 0, 5}, {r, {i}}] {{f[0, 0]}, {f[1, 1]}, {f[2, 2]}, {f[3, 3]}, {f[4, 4]}, {f[5, 5]}} Or Table[f[i, r], {i, 0, 5}, {r, 0, i, 2}] // TableForm f[0,0] f[1,0] f[2,0] f[2,2] f[3,0] f[3,2] f[4,0] f[4,2] f[4,4] f[5,0] f[5,2] f[5,4]


3

Ok, here you have it, but in the future this kind of questions will most likely be closed because the main problem arises from very basic errors. You can't expect others do the debugging for you. Take a look at the functions' definitions. {UT1, RA1, MM1, Z, MU, a1, L} = Transpose[RandomReal[{0, 1}, {10, 7}]]; dt = 1; rvsum1[1] = 5320007.301; rvsum1[i_] := ...


3

basically want to create a list of every possible pair of angles Make range, then use Tuples 2 at times ? r = Range[-Pi/2, Pi/2, 1]; (Tuples[r, 2]) // N // MatrixForm Update r = Range[-Pi/2, Pi/2, Pi/3]; (c = Tuples[r, 2]) // MatrixForm


3

I think FoldList alone will do what you want. The output is slightly different from the sequence you show but I am assuming that is an error in the question. FoldList[F, F[T0, t0], {t1, t2}] {F[T0, t0], F[F[T0, t0], t1], F[F[F[T0, t0], t1], t2]} This could also be written: FoldList[F, {T0, t0, t1, t2}] // Rest {F[T0, t0], F[F[T0, t0], t1], ...


3

expr1 @@@ DeleteCases[Tuples[Range @ 4, {2}], {a_, a_}] {C[1] β[2] ϕ[1, 2], C[1] β[3] ϕ[1, 3], C[1] β[4] ϕ[1, 4], C[2] β[1] ϕ[2, 1], C[2] β[3] ϕ[2, 3], C[2] β[4] ϕ[2, 4], C[3] β[1] ϕ[3, 1], C[3] β[2] ϕ[3, 2], C[3] β[4] ϕ[3, 4], C[4] β[1] ϕ[4, 1], C[4] β[2] ϕ[4, 2], C[4] β[3] ϕ[4, 3]} where @@@ is a shortcut for Apply at level {1}. If ...



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