Hot answers tagged

10

Here is how you would do it using the standard add-on package VariationalMethods, which is meant for calculations like this: Clear[m, k, c, x, t]; T = 1/2 m x'[t]^2; V = 1/2 k x[t]^2; L = T - V; Needs["VariationalMethods`"] hamiltonianEq = h == FirstIntegral[t] /. Last[FirstIntegrals[L, {x[t]}, t]] (* ==> h == 1/2 (k x[t]^2 + m ...


9

Function - just feed it the 5 x 5 array, returns updated array: chg[mat_] := ReplacePart[mat, {x_ /; 2 <= x <= 4, y_ /; 2 <= y <= 4} /; mat[[x, y]] === 0 && mat[[2 x - 3, 2 y - 3]] === mat[[3, 3]] -> mat[[3, 3]]]; Personally, I find even that messy, and prefer (as in this is how ...


7

Here are a couple of options. Given: poly = Array[# a[#] x^# &, 5] (* x a[1] + 2 x^2 a[2] + 3 x^3 a[3] + 4 x^4 a[4] + 5 x^5 a[5] *) We can do c = CoefficientRules[Expand[poly], x] c /. HoldPattern[a_ -> b_] :> (a -> b^2) (* {{5} -> 5 a[5], {4} -> 4 a[4], {3} -> 3 a[3], {2} -> 2 a[2], {1} -> a[1]} *) (* {{5} -> 25 a[5]^2, ...


7

Append and Prepend and possibly ReplacePartare most likely slowing your code down substantially. I could not recode your stuff to work without these constructs withing the limited time I had available, nor do I understand competely why you approach the issue this way. Regardless, there is a good and clear demonstration by Philip Gregory that gives an ...


7

To be very explicit and related to the situation in your question, consider this simple function f and the different fs that all might be ways to compute the derivative at a first glance. ClearAll[f, fs, fs2, fs3, fs4]; f[x_] := x^2; fs[x_] := D[f[x], x]; fs2[x_] := f'[x]; fs3[x_] = D[f[x], x]; fs4[x_] := Block[{t}, D[f[t], t] /. t -> x]; Let's check ...


6

This version works: rl = {ρ[z_, ϕ_] :> 1/5 Sqrt[25 - 25 z^2 + 10 Sin[5 ϕ] + Sin[5 ϕ]^2]}; Manipulate[PolarPlot[Evaluate[ReplaceAll[ρ[z, ϕ], %]], {ϕ,0, 2 Pi}], {z, -1, 1}] Manipulate[PolarPlot[Evaluate[ReplaceAll[ρ[z, ϕ], rl]], {ϕ,0, 2 Pi}], {z, -1, 1}] The main change is in the way the rule is defined as a $RuleDelayed$ instead of $Rule$.


5

You can make a set of replacement rules out of the duplicates list: rules = ({#[[1]]} -> # & /@ duplicates) (* {{1} -> {1, 4, 7}, {5} -> {5, 6}} *) then apply it to any list like the ones you have, {{3}, {1}, {2}, {4}} /. rules (* {{3}, {1, 4, 7}, {2}, {4}} *) {{3}, {1}, {2}, {4}, {5}, {8}} /. rules (* {{3}, {1, 4, 7}, {2}, {4}, {5, 6}, ...


5

genCoords = {x[t]}; ke = 1/2 m x'[t]^2; v = 1/2 k x[t]^2; q = -c x'[t]; l = ke - v; Solve for x'[t] in terms of p[t]: rule = First@Solve[p[t] == D[l, x'[t]], x'[t]] (* {x'[t] -> p[t]/m} *) and then replace this expression into the Hamiltonian: x'[t] D[l, x'[t]] - l /. rule (* p[t]^2/(2 m) + 1/2 k x[t]^2 *) Note that I have used lower-case symbols ...


4

Others have explained why the version with rl does not work. But this does not explain the very weird phenomenon that the version with % does work. Why is % (which is just a notation for Out) special? It seems that Manipulate singles out Out (i.e. %) for special treatment. Observe: In[1]:= x Out[1]= x In[2]:= Manipulate[Hold[%1], {x, 0, 1}] Out[2]= ...


4

Thanks to ciao's and Kuba's explanation, I have thought of some little code to exemplify the scoping behaviour of Manipulate; I hope it will be helpful to people who are still not very familiar with the concept: a), Manipulate[Hold@x, {x,0,1}] b), p=x; Manipulate[2 p-x,{x,0,1}] c), rpl=q->x; Manipulate[(2 q/.rpl)-x,{x,0,1}]


4

f = # -> #2^2 & @@@ # &; poly = Plus @@ Array[# a[#] x^# &, 5]; c = CoefficientRules[poly, x] {{5} -> 5 a[5], {4} -> 4 a[4], {3} -> 3 a[3], {2} -> 2 a[2], {1} -> a[1]} f@c {{5} -> 25 a[5]^2, {4} -> 16 a[4]^2, {3} -> 9 a[3]^2, {2} -> 4 a[2]^2, {1} -> a[1]^2}


4

The following is an expansion of the explanation given by Mr.Wizard. The pattern-matcher works on the base of the assumption that Orderless attribute is already applied and the arguments of the Orderless function are already sorted in the canonical order: ClearAll[o] SetAttributes[o, Orderless] MatchQ[Hold[o[y, x, a]], Hold[o[_, x, a]]] (* unsorted ...


3

When dealing with a list of rules ({a->b, c->d, ...}) you might also be interested in converting it first into an Association, which allow efficient treatment of its elements and with some more simple syntax. For example in your case: taking @March example: poly = Array[# a[#] x^# &, 5] (* x a[1] + 2 x^2 a[2] + 3 x^3 a[3] + 4 x^4 a[4] + 5 x^5 ...


3

You could use NonCommutativeMultiply. For instance: Unprotect[NonCommutativeMultiply]; o_ ** 1 := o; 1 ** o_ := o; o_ ** p_Plus := Plus @@ (o ** # & /@ p); p_Plus ** o_ := Plus @@ (# ** o & /@ p); o1_ ** Times[k : Except[_a | _b], o2_] := k o1 ** o2; Times[k : Except[_a | _b], o1_] ** o2_ := k o1 ** o2; o1_b ** o2_a := o2 ** o1; ...


2

In Mathematica everything is an expression. subs = {mu -> 0.01, bet -> 0.001, d -> 0.1, m -> 0.3, alph -> 0.4, fc1 -> (c1/(c1 + 8)), fc2 -> ((c1 + inc)/(c1 + inc + 8))}; enteeigenexpf = eigenexp /. subs; etc.


2

While it really depends on what you are trying to do, your construction may well cause you problems. You are sort of trying to define an approximation to the derivative of f as a function of f and its second and third derivatives. Here's an alternative approach. Say you define the approximation as fder[x_, h_] := 1/h (f[x + h]-f[x])-h/2 D[f[x], {x, 2}]- ...


2

Use SetDelayed (:=) a = x^2; b := a^2 ?b b:=a^2 As you can see, b is still a^2. http://reference.wolfram.com/language/ref/SetDelayed.html


1

This may not be as clear as 8 Rules, but hey, it works! ToMatch[input_] := Module[{center = input[[3, 3]], temp}, temp = input; temp[[2 ;; 4, 2 ;; 4]] = MapIndexed[ If[#1[[2, 2]] === 0 && #1[[Delete[#2, 0]]] === center, center, #1[[2, 2]]] &, Partition[input, {3, 3}, {1, 1}], {2}]; temp]


1

See ReplaceAll and What are the most common pitfalls awaiting new users?. Say you want to find a root, you would code something like rule = FindRoot[x^3 - Tanh[x], {x, 1.0, 1.2}] and get as result: {x -> 0.893395} Next step ist (as proposed by @Alexei Boulbitch) expr/.rules applies a rule or list of rules in an attempt to transform each ...



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