# Tag Info

10

When there is no built-in function, it is often straightforward to build one. Here's a way to calculate the median value of an integrable function: f[x_] := x^2; a = 1; c = 3; FindInstance[Integrate[f[x], {x, a, b}] == Integrate[f[x], {x, b, c}], b] // N {{b -> 2.41014}} You already have a formula for the mean: Integrate[f[x], {x, a, c}]/(c - a) 13/3 ...

9

I don't know why no one mentioned this, but all you have to do is to use a special form of OptionsPattern: pfunc[x0_, plotopts : OptionsPattern[{Plot, pfunc}]] := your-code where inside OptionsPattern go all sub-functions you need, in a list. Now everything is fine and dandy. There might be a downside of this, in case when several sub-functions can take ...

9

It gives those errors because you explicitly specified that pfunc only has "test" as an option. OptionValue is finicky and will complain if it sees options that it doesn't recognize. There are a couple of alternatives that I can think of: 1: Use FilterRules everywhere instead of OptionValue ClearAll@pfunc2 pfunc2[x0_, plotopts : OptionsPattern[]] := ...

8

The general way to construct functions with partially evaluated pieces is to use With, which is a general device for injecting evaluated pieces in otherwise unevaluated or held expressions. In your case, it would look like f[a_] := With[{sqa = a^2}, sqa * # &] The method based on Evaluate is generally less powerful, since evaluation of the entire body ...

8

The simplest solution is to rescale the data. Suppose we have a distance limit of $r_x = 2$ and $r_y = 1$ and a point set data = {{x1,y1}, {x2,y2}, ...} Instead of working with data and these two radii, work with a single radius $r=1$ and the dataset dataScaled = {{0.5 x1, y1}, {0.5 x2, y2}, ...} Finally, transform the results back to the original ...

5

I took a rather shotgun approach to the question and got a range of behaviors. The range is rather confusing, so I agree with Szabolcs's conclusion that using Hold this way is not supported. First, NMinimize[Hold[Print["hi"]; x^2], x] prints "hi" and then crashes the kernel, while NMinimize[Print["hi"]; x^2, x] prints "hi" and returns {0., {x -> 0.}}. ...

5

I believe you need to add all of Plot's options to pfunc as well, like this: Options[pfunc] = Join[ Options[Plot], {"test" -> True, ...} ] I'd like to note that this is what builtins do as well. For example, Plot also carries all possible Graphics options. The downside is that any changes to the default options of Plot won't affect pfunc. The upside ...

4

MeshFunctions is useful for discrete changes in color in Plot: Show[ Plot[2.9*Tanh[5 x] + 0.3, {x, -2.5, 2.5}, PlotRange -> {-3, 3.5}, Axes -> False, Frame -> True, FrameTicks -> None, PlotStyle -> {Blue, Thick}], Plot[-x^3 + 3.5*x + 0.5, {x, -2.5, 2.5}, Mesh -> {{-1, 1}}, MeshShading -> {Darker@Green, Black}, PlotStyle -> ...

4

I think Mathematica is correct here: $$\int_{-2}^2 \delta(x^2+y^2+z^2-1)~ dx dy dz = \int_0^\infty dr \int_0^\pi d\theta \int_0^{2\pi}d\phi ~r^2\sin\theta~ \delta(r^2-1)$$ $$= \frac{1}{2}\int_0^\infty d(r^2) \int_0^\pi d\theta \int_0^{2\pi}d\phi ~r\sin\theta~ \delta(r^2-1) = \frac{1}{2}\int_0^\pi d\theta \int_0^{2\pi}d\phi ~ \sin\theta= 2\pi$$

4

You said you tried using even[ff, x_] := (ff[x] + ff[-x])/2 but I guess you forgot to put the underscore on the first argument. If you do even[ff_, x_] := (ff[x] + ff[-x])/2 instead, then it works. g[x_] := x + x^2; even[g, x] x^2 P.S. No SetAttributes necessary using this method.

4

Given that the word "quickly" is in your title and that you use NIntegrate in your example, you might try the following, purely numerical approach to your problem. findMedian[f_, {x_, a_, b_}] := Module[ {y, interpolatingFunction}, interpolatingFunction = y /. First[ NDSolve[{y'[x] == f, y[a] == 0}, y, {x, a, b} ]]; ...

3

DistanceFunction is one way to go. For example, the code below generates a random set of points in the box with all elements drawn from -1 to 1 and then selects the 20 points that are closest to the point {0.5, 0} (in the sense of an ellipse defined by the matrix a). In the example, a is tall and skinny, but could have any orientation and scale. data = ...

3

Leonid already named the big one, With, but there are a few more approaches I'd like to outline. First of all you can use Evaluate if you wish to evaluate the entire body of the Function, and if the Function isn't part of some larger held expression. I fully agree with Leonid however that With is more general and less prone to surprises here. Nevertheless ...

2

If I understand your question, you want something like: f[1] = 1; f[x_] := f[x] = Sum[Boole[Mod[x + 1, t + 1] == 1] f[t], {t, 1, x - 1}] ListLinePlot[f[#] & /@ Range[1, 100], InterpolationOrder -> 0, PlotRange -> {{1, 100}, All}, AxesOrigin -> {1, 0}] Note the use of the :=...=... construct: this creates "memoization" of results (since ...

2

As b.gatessucks writes in his comment, you want make two plots and combine them with Show. Also you need to modify your color function a little. p1 = Plot[-x^3 + 3.5*x + 0.5, {x, -2.5, 2.5}, PlotRange -> {-3, 3.5}, Axes -> False, Frame -> True, FrameTicks -> None, PlotStyle -> Thick, ColorFunction -> (If[Abs[#] > 1, ...

2

Please examine this and determine if it is giving the result that you desire: m = 3; mem : mult[i_, j_] := mem = (tstar[Abs[i - j]] + tstar[i + j - 2])/2 ptab = Table[Expand @ Sum[a[i - 1]*mult[i, j], {i, m}], {j, m}] Table[Coefficient[j, tstar[i - 1], 1], {j, ptab}, {i, m}] // Transpose If you include the definitions of a and tstar I may be able to ...

2

In these situations we use pattern matching instead of conditionals in Mathematica. You just need to make two definitions: f[x_?NumericQ, y_?NumericQ, s_] := ... f[x_, y_, z_] := ... The first, more specific one will be used if both of the first two arguments are numeric. The second one will be used otherwise. Personally I consider doing this very bad ...

2

Having f[x_] := x^1.1 - 2.5 x^.5 And also knowing that the formula of the tangent line is f'[x](x-a) + f[a] You could just make a Plot with it. With a=1, as you requested: Here is the code of it: With[ {a = 1}, Plot[ { f[x], f'[a] (x - a) + f[a] }, {x, 0, 10}, PlotRange -> {-4, 4}, PlotStyle -> Thick, Epilog ...

2

This is really easy if you understand the internal form of {a,b,c,d}. Let's look at it: p={a,b,c,d}; FullForm[p] (* List[a,b,c,d] *) as you see what you want is not really far away because basically, you only need to replace List with f. This is exactly what Apply (or as operator @@) does: f @@ p (* f[a, b, c, d] *)

1

You question is not very clear. a = {p[1], p[2], p[3]} f[x__] := 2 x f[a] (* or *) f /@ a (* or *) Thread[f[a]] yields, e.g. {2 p[1], 2 p[2], 2 p[3]} Note also I used a for the vector, else recursion... If you just want to transmute the vector into an argument list: f[Sequence @@ p]

1

I think Mathematica is padding one extra position on the left to preserve space for a possible minus sign. NumberForm[#, {6, 4}, NumberPadding -> {"*", "0"}] & /@ {1234.567, -1234.567, 12.34567, -12.34567, 1.234567, -1.234567} {*1234.5700, -1234.5700, *12.3457, -12.3457, **1.2346, *-1.2346}

1

Is this what you want? ClearAll@ExactVarsQ; ExactVarsQ[func_, vars__] := MatchQ[ Sort@DeleteDuplicates@Cases[func, x_ /; (AtomQ[x] && ! NumericQ[x]), {0, Infinity}], Sort@DeleteDuplicates@{vars} ] test1 = (1 - Exp[I*(x - 5.6)*23.4])*Log[Abs[x - 4.3]]/(1.7 + 3.2*I - x^2) test2 = (1 - Exp[I*(x - y)*23.4])*Log[Abs[x - 4.3]]/(1.7 + 3.2*I - ...

1

Since this question has become an exposition of alternatives to Accumulate here is another: list = {a, b, c, d, e}; x = 0; Table[x += i, {i, list}] {a, a + b, a + b + c, a + b + c + d, a + b + c + d + e} The same method but with integrated scoping of x: First @ Table[x += i, {x, {0}}, {i, list}] (Module is more readable but since we're exploring ...

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