Tag Info

15

Integrate the zero-order interpolation of the data: f[x_] = Integrate[Interpolation[dat, InterpolationOrder -> 0][x], x]; Plot[f[x], {x, 0, 110}, AspectRatio -> Automatic, GridLines -> {{18, 70, 90}, None}] It can efficiently plot piecewise functions with thousands of transition points in milliseconds: dat = {Accumulate@RandomReal[{0, 1}, ...

10

Some built-in functions use strings as option names: Options[f] = {"AnObscureOption" -> 1}; f[x_, OptionsPattern[]] := {x, OptionValue["AnObscureOption"]} Advantages of this are that the global namespace is not cluttered with extra symbols and they do not have to be write protected but there is a disadvantage in that they are not amenable to usage type ...

8

It is normal for option variables to be global and to be given the property Protected, which will prevent assigning to them. In your example, you would write Protect[a, b]; Options[f] = {a -> a0, b -> b0}; then a = 1; Set::wrsym: Symbol a is Protected. >> a a Attributes[a] {Protected} and similarly for b.

6

Define L = (1/2) (D[#, x] + D[#, y]) & We see that L works as desired. For instance: Simplify[Nest[L, f[x, y], 3]] (* (Derivative[0, 3][f][x, y] + 3*Derivative[1, 2][f][x, y] + 3*Derivative[2, 1][f][x, y] + Derivative[3, 0][f][x, y])/8 *) And the Sum can be constructed in a similar manner. For instance: Simplify[Sum[Nest[L, f[x, y], n], {n, ...

5

Instead of protecting option variables that is sacrifice useful names, I generally use meaningful strings: Options[f] = {"a" -> a0, "b" -> b0}; f[x_, OptionsPattern[]] := {x, OptionValue["a"]} Any objections are very welcomed but this worked for me so far.

4

Here is a function makeOperator that takes any polynomial together with a replacement rule that maps the desired variable onto the desired operator. It outputs the result as a new operator: Clear[makeOperator]; makeOperator[poly_, Rule[x_, op_]] /; PolynomialQ[poly, x] := Module[{f}, Function[#1, #2] & @@ {f, Expand[poly]} /. Power[x, n_: 1] ...

4

In Mathematica, Element[1,Reals] returns True since integers are subset of the reals. But Head[1] is Integer. So, since you need to check for Head of each element. One way might be realVector[x_List] := VectorQ[x, NumericQ] && (AllTrue[x, (Head[#] === Real) &]) Now realVector[{1., 2., 3.}] (*True*) realVector[{1, 2, 3}] (*False*) ...

3

I suspect that a somewhat different structure will ultimately benefit you, and I shall attempt to recommend one if and when I better understand your needs. However for the moment these examples might be useful to you: data = {"apple", "banana", "kiwi"}; models = {"model1", "model2", "model3"}; result[m_List][par_] := result[#][par] & /@ m ...

3

The obvious way to modify your code is Clear[f, r] r[x_, n_] := If[x > 0, Print[n]; x*r[x - 1, n + 1], 1] f[k_Integer /; k > 1] := r[k, 1] For small values of x, this works fine. f[5] But it is very inefficient and also limited by $RecursionLimit. Block[{$RecursionLimit = 20}, f[24]] Both these issues can be addressed by using a less ...

3

Although I like DumpsterDoofus's answer a lot more, now that I am properly awake I realize this works: dat = {{0, 0}, {18, 1}, {70, 1/4}, {90, -1}, {110, 2}}; g[{x_, y_}, {X_, Y_}] := {X, y + (X - x) Y} f2 = Interpolation[FoldList[g, dat], InterpolationOrder -> 1]; Plot[f2[x], {x, 0, 110}, AspectRatio -> Automatic, GridLines -> {{18, 70, 90}, ...

3

funny I just worked this up for this answer here : http://mathematica.stackexchange.com/a/71427/2079 dat = {{0, 0}, {18, 1}, {70, 1/4}, {90, -1}, {110, 2}}; xmap[x_] = Piecewise[ Fold[Append[#, {(#[[-1, 1]] /. x -> (Last@Last@Last@#)) + #2[[2]] (x - (Last@Last@Last@#)), x < #2[[1]]}] &, {{x dat[[2, 2]], x < ...

2

I propose this: SetAttributes[myPlot, HoldAll] myPlot[x_, args__, opts : OptionsPattern[Plot]] := Plot[{Re@x, Im@x}, args, opts, PlotRange -> All] Test: myPlot[Sin[t] + I*Cos[t], {t, 0, 10}] HoldAll is used to mimic the evaluation behavior of Plot. OptionsPattern[Plot] is used to define the valid options as being those of Plot. opts is ...

2

Perform the line integral over the straight-line path from the origin to $(x,y)$ via the parametrization $u(t)=(tx,ty)$ for $0\le t\le1$. Then $\mathrm du=(x,y)\,\mathrm dt$, so the integral can be computed as Integrate[b[t x, t y].{x, y}, {t, 0, 1}] Note that if $b = \nabla a$, from $b$ one can only recover $a$ up to a constant. In this case, since we ...

2

It is easiest when each options controls something that is more or less independent of other options. If as in your example each combination of options results in (the need for) a different subroutine things do get complicated. A basic strategy is to look for repetitions segments of code an replace them with a single copy. For example in your ...

2

This is a typical Finite Difference Method. x = dat[[;; , 1]]; y={#}~Join~(# + Accumulate[Differences[x] dat[[2 ;;, 2]]]) &@dat[[1, 2]]; Now f = Interpolation[Transpose[{x, y}], InterpolationOrder -> 1]; Plot[f[x], {x, 0, 110}, AspectRatio -> Automatic,GridLines -> {x, None}]

2

You can identify the problem by performing the steps in your function one at a time for theta = .1 and phi = 0.. {c1, c2, c3} = 1 - 3 Sin[0.1]^2 Cos[0 - 2 Pi (# - 1)/3]^2 & /@ {1, 2, 3} {* {0.9701, 0.992525, 0.992525} *} Phi = Sqrt[((c2 + c3)^2 - c1^2)/(4 c2 c3)] {* 0.87245 *} PhiPr = ((c3 - c2)/c1) Phi {* 0. *} Because 0 <= Phi^2 <= 1 is ...

2

Does this fit your needs? RealVector = MatchQ[#, {__Real}] &

2

Although you did not provide copyable code for your final input if I run a rough facsimile I get an informative series of errors: dot @@ Map[mat, RandomInteger[9, {2, 4, 4}], {3}] $IterationLimit::itlim: Iteration limit of 4096 exceeded. >>$IterationLimit::itlim: Iteration limit of 4096 exceeded. >> \$IterationLimit::itlim: Iteration limit of ...

1

As written in the Question, Mathematica does not equate the a in y with the argument of g. This is a common issue. Use this instead, so that a appears explicitly. y[a_] = x^2 - a; g[a_] := x /. FindRoot[y[a] == 0, {x, 1}] g[3] (* 1.73205 *)

1

ClearAll[foo]; foo = Plot[{Re[#], Im[#]}, #2, PlotRange -> All, ##3] &; foo[Sin[t] + I*Cos[t], {t, 0, 10}, PlotStyle -> Thick]

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