New answers tagged

1

It was clarified in comments that expressions are supposed to be read into Mathematica from a text file. To achieve what OP wants one can read the file as list of held expressions, perform appropriate replacements, then release hold. If we have a test.txt file with following contents: a * b * c * d a^dag We can read it in following way: ...


0

Simply, make Mathematica automatically create variables when they are called (Memoization). You don't need a loop for that. x[i_, j_] := x[i, j] = Subscript[r, i, j]/1 If you need a loop, use Do: n = 2; m = 6; Do[x[i, j] = Subscript[r, i, j]/1, {i, n}, {j, n+1, m}] x[2, 5] (* Subscript[r, 2, 5] *)


0

If I understood your question correctly, please see implementation below. Example: x[n_, m_] := Outer[f, Range[n], Range[n+1, m]] x[2,6] Note: f is an arbitrary function which takes two arguments Output: {{f[1, 3], f[1, 4], f[1, 5], f[1, 6]}, {f[2, 3], f[2, 4], f[2, 5], f[2, 6]}}


9

I recently needed to do something like this. Here's a simplified version of what I came up with: DynamicModule[{elem = "Hydrogen"}, Panel[Row[{(ColorData["Atoms", "Panel"] /. HoldPattern["MouseClicked" :> rhs_] :> ("MouseClicked" :> (elem = CanonicalName[ElementData[Cases[Unevaluated[rhs], _Rule, ∞][[1, ...


2

I learned a lot from WReach's and Leonid's answers and I'd like to make a small contribution: It seems worth emphasizing that the primary intention of the list-valued second argument of Flatten is merely to flatten certain levels of lists (as WReach mentions in his List Flattening section). Using Flatten as a ragged Transpose seems like a side-effect of ...


1

We should not try to hide that it can be done by indexing over a, and since the OP specifically asked for such a solution, I think he deserves to get one. This is what occurred to me. progressiveMean[a_List] := Module[{prev = a[[1]], next}, Rest @ Table[ next = (a[[i]] + (i - 1) prev)/i; prev = next, {i, 2, Length[a]}]] a = {5, ...


3

Here is a SparseArray solution (as prompted by BlacKow): vecs = Table[ SparseArray[ConstantArray[1/i, i], Length[a]], {i, 2, Length[a]}] a.# & /@ vecs


6

No need to loop, and in general you'll want to use built-in, functional idioms for readability and performance, e.g.: Rest@Accumulate[a]/Range[2, Length@a] will give the desired result efficiently.


2

Understand the difference between Set (or =) and Equal (or ==) Suppose you want to solve the system of equations $x^2 + y^2 = 1$ and $x = 2y$ in Mathematica. So you type in the following code: Solve[{x^2 + y^2 = 1, x = 2 y}, {x, y}] You then get the following output: Set::write: Tag Plus in x^2+y^2 is Protected. >> Solve::ivar: 2 y is not a ...


6

I did some time ago. tv = 225; te = 365.25; rv = 0.72; re = 1; e[t_] := {Cos[2 Pi t/te], Sin[2 Pi t/te]}; v[t_] := 0.72 {Cos[2 Pi t/tv], Sin[2 Pi t/tv]}; vis[t_, s_] := Graphics[ {Yellow, PointSize[0.05], Point[{0, 0}], White, Circle[{0, 0}, 1], Circle[{0, 0}, 0.72], Blue, PointSize[0.03], Point[v[t]], Red, Point[e[t]], White, ...


2

Dynamic inside Manipulate A tip that I would like to contribute is to use Dynamic with controls such as sliders, checkboxes and input fields inside Manipulate. This enables one to place the control anywhere (even inside a figure) and to tie controls together. I typically initialize the controls by placing them in the normal spot for Manipulate but setting ...


10

Might as well... eorb = PlanetData["Earth", "OrbitPath"]; vorb = PlanetData["Venus", "OrbitPath"]; dl = DateRange[{2010, 1, 1}, {2015, 12, 31}, "Week"]; epos = Table[QuantityMagnitude[UnitConvert[ PlanetData["Earth", EntityProperty["Planet", "HelioCoordinates", {"Date" -> dates}]], ...


4

See for a start Cardioid or Cardioid, Wiki. If i'm not mistaken this stuff is called Line Art, String Art and Curve Stitching. (Code Intellectual property of Matt Henderson of http://blog.matthen.com) Manipulate[Graphics[{Table[Line[{{Sin[a], Cos[a]}, {Sin[a + 2 Pi/3 + t], Cos[a + 2 Pi/3]}}], {a, 0, 2 Pi - 0.001, Pi/50}]}, PlotRange -> {{-1, 1}, {-1, ...


5

With the caveat that it has zero physical meaning whatsoever, here you go: \[Omega]v = 365.25/225; rv = 0.72; lines = Table[Line[{{Cos[t], Sin[t]}, rv {Cos[\[Omega]v t], Sin[\[Omega]v t]}}], {t, 0, 20 \[Pi], \[Pi]/40}]; Graphics[{White, Opacity[0.4], lines, Opacity[1], Red, Circle[], Circle[{0, 0}, rv]}, Background -> Black] And if you want an ...


10

Here's something that is nowhere near the sophistication of the original, but might get you started. The following assumes an orbital period for Venus of 225 days (thanks Michael!), and an average orbital distance from the sun of 0.72 AU (from very superficial Google searches). Table[ Module[ {venus, earth}, venus = 0.72 AngleVector[2 Pi/225 d]; ...


2

Example: (*Arbitrary data*) list = RandomInteger[{1, 10}, {4, 4}] (*Apply list to custom function*) f @@@ list Output: {f[2, 8, 6, 5], f[3, 1, 6, 7], f[9, 2, 10, 5], f[9, 2, 5, 9]} EDIT If you are loading an Excel spreadsheet, to apply row by row you could do something similar as described above. In order to load up an Excel spreadsheet, see ...


0

The correct Mathematica code (v.10.2.0.0.) is: A = N[{{4, 1, -2, 2}, {1, 2, 0, 1}, {-2, 0, 3, -2}, {2, 1, -2, -1}}]; (*A=N[{{-42,43,-2,28},{43,-98,72,-26},{-2,72,-96,53},{28,-26,53,54}}];*) n = Length[A[[1]]]; zeroVector = {}; For[i = 1, i <= n, i++, zeroVector = Append[zeroVector, {0}]]; Alist = {A}; Hlist = {}; For[j = 1, j <= n - 2, j++, ...


0

Use Column[{"ERROR", Row[{N[(2^(n + 1)*eps*maxofy)/(E*n*Log[n])], " THEORETICAL" }], Row[{ N[FindMaximum[{Abs[poly[t]-polyy[t]], t >= 0 && t <= 2}, t][[1]]], " PRACTICAL"}]}] or Column[{StringForm["ERROR\n `` THEORETICAL\n" , N[(2^(n + 1)*eps*maxofy)/(E*n*Log[n])] ], StringForm["\n `` PRACTICAL", N[FindMaximum[{Abs[poly[t] - ...


7

ClearAll[fuzzyLCS]; fuzzyLCS[strings__List] := Module[ {subsets, aligned, intersections}, subsets = Subsets[strings, {2, Length@strings}]; aligned = Select[SequenceAlignment[#[[1]], #[[2]]], StringQ[#] &] & /@ subsets; intersections = Intersection @@ (Subsets[#, {1, Length@#}] & /@ (Flatten[Characters[#]] & /@ ...


1

you have it close, just need an extra {} on the x term: ( making up a simple example..) n = 4; a = 0; b = 1; f[x_] := 10^12 Sin[2 x] ; f2[x_] := 10^12 Cos[x] ; poly = InterpolatingPolynomial[ Table[{{xi = a + (b - a)/n i}, N@Round[ f[xi], 10^7], N@Round[f2[xi], 10^7]}, {i, 0, n}], x] // Simplify 0.+ 1.*10^12 x + 6.76392*10^13 x^2 - ...


0

I tend to do manipulate for lightweight applications because it will auto refresh and I think it looks better. The auto refresh also helps with the user interface because I think it's more intuitive than having to press enter or even a button. Dynamic may be a better choice for applications where the user would want to set multiple parameters to a specific ...


1

Try this: f[y_] := D[y^3*D[y - D[y, x, x], x], x]; g = f[y0[x] + eps*z[x]] // Expand; Coefficient[g, eps] It returns the following: Here z stays instead of your y'. Have fun!


7

You can use Interpolate to interpolate between the polygon vertices: First make sure the polygon is closed, by appending the first vertex: cyclic = Append[manualPolyPoints, First[manualPolyPoints]]; then accumulate the distance from one vertex to the next: accumulatedDistance = Rescale@Prepend[Accumulate[Norm /@ Differences[cyclic]], 0.]; then ...


2

This may be an answer. Your code is ill-formed. It does not conform to Mathematica syntax. I have rewritten it to be valid and to better correspond to your pseudocode. There is no guarantee that it actually corresponds to the process you want to simulate, but at least it prints output and terminates. I hope it will help you to move forward. With[{n = 2}, ...



Top 50 recent answers are included