Tag Info

Hot answers tagged

28

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]); ...


23

The following is based on the fact that the determinant of a matrix is equal to zero when two rows are the same. Thus, if you plug any of the points in, you get a true statement. SeedRandom[3]; pts = RandomReal[{-1, 1}, {5, 2}]; row[{x_, y_}] := {1, x, y, x*y, x^2, y^2}; eq = Det[Prepend[row /@ pts, row[{x, y}]]] == 0 (* Out: ...


17

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


10

The general equation of ellipse (here) is given by: ellipse[x_, y_] = a x^2 + b x y + c y^2 + d x + e y + f == 0; solving using 5 pintos result in: SeedRandom[3]; pts = RandomReal[{-1, 1}, {5, 2}]; sol = Solve[ellipse @@@ pts]; ellipse[x, y] /. sol[[1]] // Simplify (*a (-0.275185 + 1. x^2 + x (0.189022 + 0.566953 y) + 0.1281 y + 0.397124 y^2) == ...


9

Unprotect[Sqrt]; Sqrt[x_] = "blahblah"; Protect[Sqrt]; Sqrt[2] blahblah Unprotect[Sqrt]; ClearAll[Sqrt]; Protect[Sqrt]; Sqrt[2]


8

Your second argument is a function instead of a pattern. Count[{1, 1, 2, 3}, _?(# > 1.5 &)]


8

The problem here is that in Mathematica, parameters of a function are not local variables. So trying to modify a parameter of a function inside it's body will lead to an error. The reason is that function arguments are evaluated, when the function is called so that it is actually the result of this evaluation that's textually substituted for the function ...


7

You can use TrigReduce to do what you want: TrigReduce[-2 Cos[v] g[u] Sin[v] Derivative[1][g][u]] -g[u] Sin[2 v] Derivative[1][g][u]


7

If we are willing to confine ourselves to functions like the example f which: is defined using DownValues only, and has no special attributes such as HoldAll, etc. ... then the following lifting function might be useful: ClearAll[stepper] SetAttributes[stepper, HoldAll] stepper[f_Symbol] := Module[{rules, g} , rules = Rule @@@ ...


7

ClearAll[zeroColumns2,zeroColumns2b,zeroColumns3,zeroColumns3b]; zeroColumns2[mat_, m_ ;; n_] := ReplacePart[mat, {_, Alternatives @@ Range[m, n]} -> 0]; (* or Alternatives @@ Range @@ (m ;; n) if you want to use Span *) list1 = {{1, 2, 3, 4, 4}, {4, 5, 6, 9, 5}, {7, 3, 8, 9, 5}, {14, 3, 1, 5, 6}}; zeroColumns2[list1, 1 ;; 3] (* ...


7

An alternative approach m = RandomInteger[9, {5, 5}]; m // MatrixForm MapAt[0 &, m, {;; , 2 ;; 4}] // MatrixForm There is a pattern-based solution, but it is considerably more diffiluct zeroColumns4[mat_, s_Span] := ReplacePart[mat, {_, j_ /; s[[1]] <= j <= (s[[2]] /. All -> ∞) && (Length@s < 3 || Mod[j - s[[1]], ...


7

dottie = FindRoot[Cos[x] == x, {x, 1}] // Values // First 0.739085 Plot[{Cos[x], x}, {x, -5, 5}, Epilog -> {Red, PointSize[0.02], Point[{dottie, dottie}]}] Convergence can be seen with EvaluationMonitor {res, {evx}} = Reap[FindRoot[Cos[x] == x, {x, 0}, EvaluationMonitor :> Sow[x]]] {{x -> 0.739085}, {{0., 1., 0.750364, 0.739113, ...


6

I was able to speed up your code by a factor of 47,500 times faster than original. First, note that you can get a fairly good speedup just by eliminating the superfluous nested Table and Sum operators: n = 999; ak = RandomReal[{1, 10}, {1000, n}]; pimatk = RandomReal[{1, 10}, {1000, n}]; fikyj = RandomReal[{1, 10}, {1000, n}]; bk = RandomReal[{1, 10}, ...


5

Alhough your function may be an example and you could use Block or Module to build your list using looping and Append, list manipulation offers great advantages. For the example of splitting a string (including WhiteSpaceCharacter) the following are ways to do it (starting with the built-in function Characters). Characters["this is"] (*mapping StringTake ...


5

This works: RootReduce@JordanDecomposition[{{2, -5, 8, 12}, {1, -2, 4, -8}, {0, 0, 2, -5}, {0, 0, 1, -2}}] (* { {{2-I,4/3,2+I,4/3},{1,0,1,0},{0,-1/12-I/6,0,-1/12+I/6},{0,-I/12,0,I/12}}, {{-I,1,0,0},{0,-I,0,0},{0,0,I,1},{0,0,0,I}} } *)


4

The answers are great. I wanted to add that the problem you are trying to solve fits way better with replacement rules than function definitions, so switching to those can provide a more understandable answer Clear[f]; frules = {f[0 | 1] :> 1, f[n_] :> f[n - 1] + f[n - 2]}; f[7] /. frules (* same as f[7]//ReplaceAll[frules] *) f[7] /. frules /. ...


4

Here's something which returns the denominators. Is this what you are looking for? Denominator[(#! SeriesCoefficient[(x^3/6)/( E^x - 1 - x - x^2/2), {x, 0, #}] & /@ Range[0, 25])] which returns {1, 4, 40, 160, 5600, 896, 19200, 76800, 14784000, 19712000, \ 512512000, 186368000, 19568640000, 6021120000, 20889600000, \ 7798784000, 71310131200000, ...


4

The desired representation will automatically simplify when evaluated Exp[5 a]*Exp[5 b] To get and maintain the desired factored form requires any of several methods of holding the evaluation. As suggested by DumpsterDoofus, one approach is to use an unassigned operator. A slightly modified version of this approach: expr = Exp[5 (a + b)]; f = ...


4

Since gh is a tensor, you need to say what rank it is, so replace {gh, _Real} with {gh, _Real, 2} to fix the error. costFxn = Compile[ {{P, _Real}, {Ns, _Integer}, {gh, _Real, 2}, {Kg, _Integer}, {G, _Integer}, {betaGN, _Integer}}, Sum[ -Exp[Kg/(P gh[[g, n]])] (Kg * betaGN)/ Log[2] ...


3

If you're using M10, you could do it with Inactivate (which formats more nicely in Mathematica than it does here in plain text.) Inactivate[f[7] /. #, f] &[DownValues[f]] (* Inactive[f][5] + Inactive[f][6] *) Then Nest it: Clear[step]; SetAttributes[step, HoldFirst]; step[e_] := step[e, 1] step[e_, n_] := Inactivate[Nest[Function[{x}, x /. #], e, n], ...


3

but it is not working The above does not describe the problem you are having. When you say not working, you need to explain how it is not working, and what you tried. I just downloaded it and I see no problem. Using V10.01, on windows. Downloaded it from http://library.wolfram.com/infocenter/MathSource/577/ Here are some examples ...


3

This is my try that is something trick. function[eq_] := CoefficientRules[eq /. Power[a_, b_?(# < 0 &)] -> Power[a, -10^10 b]] /. a_?(# > 10^9 &) -> -a/10^10 function[2 x^3 + 3 x^(-2) y + 4 x y^2 - 5 y^(-3) + 1] {{-2, 1} -> 3, {3, 0} -> 2, {1, 2} -> 4, {0, -3} -> -5, {0, 0} -> 1}


3

dataCol = {{A, 0.1, 0.3}, {B, 0.4, 0.6}, {C, 0.9, 0.9}}; Adding the Attribute HoldRest to your cutoffFuncCol: ClearAll[cutoffFuncColB]; SetAttributes[cutoffFuncColB, HoldRest]; cutoffFuncColB[threshold_, inputlist_, col_] := (inputlist[[All, col]] = inputlist[[All, col]] /. {x_ /; x > threshold -> x, x_ /; x < threshold -> 0}; ...


3

Here is one way to deal with repeated entries in U. One can define a function to compute the coefficient, using one rule when $u_i = u_j$ and the general formula otherwise. One might put extra conditions on the patterns in coeff below, but if the function is called only within NBSpline, then one might assume the conditions are met. ClearAll[coeff]; ...


3

If you want to change the original list "directly" (inplace) you can do it with HoldFirst: SetAttributes[Exchange, HoldFirst]; Exchange[list_, a_, b_] := list[[{a, b}]] = list[[{b, a}]] list1 = {1, 2, 3, 4}; Exchange[list1, 3, 1]; list1 {3, 1, 2, 4} Multiple swaps: list1 = {1, 2, 3, 4}; Exchange[list1, ##] & @@@ {{4, 1}, {2, 3}} // Flatten; ...


3

This could also be a case where the version-10 command Inactive may be useful. Before inserting an inactive version of Exp, I also split up exponents consisting of sums by temporarily replacing powers of E with a function exp that only implements the desired multiplicative property. To cast these replacements in the form of a function expExpand, we have to ...


3

I understand from a comment that the use of pure functions is desired, but I think the broader question will have a broader interest. Here's a way that has a little start-up time, but whose efficiency advantage increases with size. zeroColumnsM[mat_?MatrixQ, m_ ;; n_] := With[{ncol = Last@Dimensions@mat}, mat . SparseArray[Delete[Table[{i, i} -> 1, ...


2

Is this the behaviour you need? Solution Unprotect[Power]; Power[0, -1] = 1 Protect[Power] Examples 0/0 0 Explanation Revert to normal Unprotect[Power]; ClearAll[Power]; Protect[Power];


2

Concerning the follow-up question in a comment: ...just for curiosity, if someone knows how to impose a positive codomain to not yet known function, I would very much like to know. One way is to define the system so that x'[t] is positive whenever x[t] < 0. Then when x[t] reaches 0, it will be stuck at 0, unless x'[t] becomes positive for x[t] > ...


2

There is nice undocumented function {c, v} = GroebnerBasis`DistributedTermsList[2 x^3 + 3 x^(-2) y + 4 x y^2 - 5 y^(-3) + 1] (* {{{{2, 0, 0, 1}, 3}, {{0, 3, 0, 0}, 2}, {{0, 1, 0, 2}, 4}, {{0, 0, 3, 0}, -5}, {{0, 0, 0, 0}, 1}}, {1/x, x, 1/y, y}} *) Then one can simplify the result Transpose@{Transpose[c[[All, 1]].Replace[v, {# -> 1, 1/# -> -1, ...



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