New answers tagged

11

dat = {0.71, 0.685, 0.16, 0.82, 0.73, 0.44, 0.89, 0.02, 0.47, 0.65}; Module[{t = 0}, Split[dat, (t += #) <= 1 || (t = 0) &] ] {{0.71, 0.685}, {0.16, 0.82, 0.73}, {0.44, 0.89}, {0.02, 0.47, 0.65}} Credit to Simon Woods for getting me to think about using Or in applications like this. Performance I decided to make an attempt at a higher ...


4

f[x_, y_] := Module[{new}, If[Total[new = Append[x, y]] >= 1, Sow[new]; {}, new]] Reap[Fold[f, {}, dat]][[2, 1]]


6

The problem is that during the evaluation process it attempts to numerically integrate using the symbol a. That is the source of the warning message. However, if you persist (and ignore the warning), ArgMin will eventually switch over to using numerical values and output the correct value. ArgMin[{NIntegrate[(Tanh[x] - Erf[x/a])^2, {x, -5, 5}], 0.5 ...


2

Example: LCM[37, #[[1]], #[[2]]] & /@ Transpose[{list1, list2}] or MapThread[LCM[37, ##] &, {list1, list2}] Output: {370, 1110, 1110, 2590} Reference: & # /@ etc. MapThread Transpose


2

You can use Outer along with Map. Map[LCM[37, Sequence @@ #] &, Outer[List, list1, list2], {2}] (* {{370, 370, 1110, 2590}, {555, 1110, 1110, 7770}, {185, 370, 1110, 2590}, {370, 370, 1110, 2590}} *) Hope this helps. Or as @SimonWood has pointed out in the comments. Outer[LCM[37, ##] &, list1, list2]


3

If you wish to find a formula for a set of data, you could use the FindFormulafunction: values = {{0.3, 360}, {0.4, 315}, {0.5, 280}, {0.6, 250}, {0.7, 225}, {0.8, 200}, {0.9, 180}, {1.0, 165}, {1.1, 150}, {1.2, 135}, {1.3, 125}, {1.4, 115}, {1.5, 105}, {1.6, 97}, {1.7, 93}, {1.8, 88}, {1.9, 85}, {2.0, 84}}; ...


3

Reduce[expr, x, Reals] will be your friend here, but it can take a bit of work to parse its result. Here's a solution that should work for any expression, not just polynomials (at least for the small set of examples I tried). RealInverse[a_. x_^q_Integer?Positive + b_., x_] /; FreeQ[{a, b}, x] := Surd[(x-b)/a, q] RealInverse[expr_, x_] := Module[{y, ...


1

There is really no need to gather the parameters into a list. You can do it with this simple approach. DynamicModule[{f, g}, f[q_, a_, b_, c_] := q + 2 a + 3 b + 4 c; g[q_, u_, v_, w_] := q - u/2 - v/3 - w/4; Manipulate[ Plot[f[q - 5, a, b, c]^2 + g[q + 5, a, b, c]^2, {q, 0, 10}], {{a, 0}, -1, 1}, {{b, 0}, -1, 1}, {{c, 0}, -1, 1}]] ...


2

One problem you're having is that because Manipulate localizes its variables, while the symbols in par are global (in the "Global`" context), the letters a, b, c inside the Manipulate do not refer to the variables in par. Your workaround seems reasonable. There's also the option LocalizeVariables, which can be set to False to make the Manipulate parameters ...


0

What about this? inverseFunc[x_] = x /. Solve[x^3 == y, x, Reals][[1]] /. y -> x; N@inverseFunc[3] N@inverseFunc[-1] (*1.44225*) (*-1.*)


0

One way to find and plot the real root of x^3 == y is x /. Solve[x^3 == y, x, Reals] (* {Root[-y + #1^3 &, 1]} *) Plot[%, {y, -10, 10}, AxesLabel -> {y, x}] This works for any polynomial f[x] == y that has a real root, as requested in the question. Is this what you had in mind? Addendum In response to the OP's comment below, a do-it-yourself ...


2

f[n_] = 351 n^(-0.7); f /@ {2, 9, 22} (* {216.066, 75.3941, 40.3284} *) Or, using a pure function 351 #^(-0.7) & /@ {2, 9, 22} (* {216.066, 75.3941, 40.3284} *) In WolframAlpha use the pure function WolframAlpha["351 #^(-0.7)&/@{2,9,22}"]


3

SeedRandom[1]; pts = RandomReal[{-10, 10}, {10, 2}]; Clear[if, ip] If the InterpolationOrder is set to Length[pts] - 1 then the InterpolatingFunction is the InterpolatingPolynomial if[x_] = Interpolation[pts, InterpolationOrder -> Length[pts] - 1][x]; ip[x_] = InterpolatingPolynomial[pts, x] // HornerForm (* -18.7844 + x (6.55948 + x ...


5

I do not know how to extract that information from an InterpolatingFunction object, but perhaps you could make your own Piecewise function using InterpolatingPolynomial: pts = RandomReal[{-10, 10}, {10, 2}]; piecewise = Piecewise[ {InterpolatingPolynomial[#, x], #[[1, 1]] <= x < #[[2, 1]]} & /@ Partition[SortBy[First][pts], 2, 1] ] ...


4

I couldn't figure out a quick way to import the data how you had your paste formatted (with curly brackets for each element, but no outer curly brackets) so I reformatted it and repasted it. data = Import["http://pastebin.com/raw/V8807EsY", "Table"]; You say you'd like to average the duplicate points, so using Mean in combination with GatherBy should ...


6

Yes, they are equivalent. _ is a pattern that matches any expression. x_ is the same thing, with a name, x. The pattern name can be used in two ways. The most common way is to refer to it on the right hand side of the definition or rule. For example, f[x_] := x^2. If we are not using the matched expression in the right hand side, there's no reason to ...


0

And the third variant, note the transformation fun1[x_] := a1*x + b1*y - 4 fun2[x_] := a2*x + b2*y - 6 a1 = 2; a2 = 5; b1 = -4; b2 = 2; solP = {x, y} /. FindInstance[fun1[x] == 0 && fun2[x] == 0, {x, y}] {{4/3, -(1/3)}} ContourPlot[{fun1[x], fun2[x]}, {x, -5, 5}, {y, -5, 5}, Epilog -> {Red, PointSize[Large], Point[solP]}]


1

In response to your comment, assign an intermediate variable With[{a1 = 2, a2 = 5, b1 = -4, b2 = 2}, solxy = Solve[ {a1*x + b1*y == 4, a2*x + b2*y == 6}, {x, y}][[1]]; p = a1*x + b2*y /. solxy] (* 2 *) The values of x and y are solxy (* {x -> 4/3, y -> -(1/3)} *)


0

One way is Bob Hanlon's solution; here is another way: params = {a1 -> 2, a2 -> 5, b1 -> -4, b2 -> 2}; sol = First@FindInstance[{a1*x + b1*y == 4, a2*x + b2*y == 6} /. params, {x, y}] {x -> 4/3, y -> -(1/3)} p = a1 x + b2 y /. params /. sol // Simplify 2 In your code replace Solve[{a1*x + b1*y == 0; a2*x + b2*y == 0}, {x, y}] with ...


3

Your code can be transformed in a function definition as follows: find[img_] := Module[{centerOne, bin, components}, centerOne = ImageTransformation[src, img, {200, 200}, DataRange -> Full, PlotRange -> {{0, 1}, {0, 1}}]; bin = Dilation[EdgeDetect[centerOne], 3]; components = ComponentMeasurements[ bin, ...


2

You need to make sure there are no active definitions on the constituent parameters. You can do this with ClearAll as E.Doroskevic proposes, though I imagine you will not want to clear all Global` definitions. You can Block the Symbols in use as described in Scoping in assigning a derivative. To make this convenient I propose using the function localSet ...


1

Try: ClearAll["Global`*"] c[theta_, xi_] = theta*xi + theta^xi^2; xbar[theta_, xi_] = theta/xi + xi^2 + theta^3; f[x_, y_, theta_, xi_] = x*y*c[theta, xi]*theta*xi + xbar[theta, xi];


3

Some of these could be implemented differently, of course, but I've gone the way of making all of them pure functions (in the Mathematica sense). Every single one takes a Sequence of arguments as the inputs, but some of them accept function names as inputs first, and the projection function accepts an integer for which argument is chosen (I have chosen to ...


4

The sequence $f[k]$ you are looking for is Sloane's A038664. There is Mathematica code given there by Harvey P. Dale. With[{d=Differences[Prime[Range[50000]]]}, Flatten[Table[Position[d, 2n, 1, 1], {n, 50}]]] which returns {2,4,9,24,34,46,30,...}.


7

Assume that you have your lists of unique resistor values (resistors) and of unique capacitor values (capacitors). For now, I generate two such lists as follows: resistors = Flatten@ Outer[ Times, PowerRange[1, 10000], {100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, ...


1

This answer suggested by @Hubble07 works. subsetQ[list1_, list2_] := WolframAlpha[ "is " <> ToString[list1] <> "a subset of " <> ToString[list2], {{"Result", 1}, "ComputableData"}]


2

In general, when you see a function prototype with a formal argument of the form oneflag_ : 0, it means the actual argument can be of any type (or more precisely have any head), but if it is omitted, then the value 0 will be used. Look up Optional in the documentation.


5

We can transfer the phase function from Matlab to Mathematica: phase[vec_List] := Module[{phi, df, len, i}, phi = Arg @ vec; df = Differences @ phi; len = Length @ phi; i = Flatten @ Position[df, x_ /; Abs[x] > 3.5]; Do[phi = phi - (2 Pi*Sign[df[[j]]]*UnitStep[# - (j + 1)] & /@ Range[len]), {j, i}]; phi] data = ...


1

a = 3; b = 2; c = 4; Plot[a TriangleWave[b x + c], {x, 0, 10}]


9

I think it's possible to find the shape automatically, but I can't say how reliable this will be. If you can post more sample images, I can try to improve this. Using your image: img = Import["http://i.stack.imgur.com/kL6cd.jpg"]; I would use watershed segmentation to find the particle. The idea is this: Imagine the image gradient strength as a 3d ...


8

edit (30 Jan 2016) : one error corrected, rotation (§4) added,result slightly higher (1.3%) I propose the following solution : 1) interactively mark the frontier of the object by points 2) interactively mark the center of the object 3) use polar coordinates (r,theta) with the origin at the center. Thus r[theta] is symetric around a angle theta0, ...


3

Default is another way to specify optional arguments, which allows for this: Default[f, 2] = Sequence[1, 2, 3] f[x_, y_.] := {x, y} f[1, 2] {1, 2} f[1] {1, 1, 2, 3}


2

f[x_, y_] := {x, y} f[x_] := {x, Sequence[1, 2, 3]}


1

Sometimes I do it this way: Block[{NIntegrate, x, y, z}, intfunc[z_] = NIntegrate[integrand, {x, -5, 5}, {y, -10, 1000}]; ]; intfunc[7.] (* 3.5009*10^7 *) It's not a great general programmatic way to go, but in the middle of solving a problem, in which I have constructed an expression integrand, this is to me an easy way. Blocking x, y, and z ...


1

integrand is a function and functions should be defined and called with explicit arguments. However, there is no need to use PatternTest with its arguments. integrand[x_, y_, z_] = z (Exp[-x^2] + y - Cos[y]); Since intfunc employs a numeric technique (NIntegrate) it should be defined with a PatternTest intfunc[z_?NumericQ] := NIntegrate[integrand[x, ...


3

I think this is only a dislay issue: If you set x=3231.432 you also get 3231.42 as a return. However, if you evaluate, say, x-3000 the missing digit (2) will appear again. I guess Mathematica only displays five significant figures (per default). Hope this helps.


1

The Simplify goes inside the Assuming Assuming[x > 0, Simplify[-((2 Sqrt[x^2/(1 + x^2)^2])/x)]] (* -(2/(1 + x^2)) *) Or put the assumption inside the Simplify Simplify[-((2 Sqrt[x^2/(1 + x^2)^2])/x), x > 0] (* -(2/(1 + x^2)) *)


4

If test is a list of numbers, your code works fine: f[x_] := (If[x > 0, x*n1, 0]) test = {1, 2, 3, -1}; f /@ test (* {n1, 2 n1, 3 n1, 0} *) If you import test from an Excel file, Mathematica might give you a 2D array (Excel sheets are like matrices). You can use Flatten to convert it to a flat list, or alternatively use Part with All (please check ...


14

This is a good example of why one should never blindly trust the numerical results of systems like Mathematica, without thinking about numerical methods that these systems use. Mathematica won't ever make numerical analysis courses obsolete. Most interpolation methods use piecewise polynomials, and assume slowly varying smooth functions. Your data has ...


1

Note that any function satisfies an infinite number of ODE's, so the answer to your question is non-unique. The following code will do what you want for some specific cases (it works for your example): f[x_] := Exp[10 x^2] ansatz = Inactivate[D[f[x], x] + a x f[x] + b f[x] + c, f]; ...


4

f[x_]:= Exp[10*x^2] D[f[x], x] /. f[x] :> HoldForm@f[x] Update B[t_, T_] := 1/a*(1 - E^(-a*(T - t))); A[t_, T_] := 1/a^2*(a*b - 1/2*sigma^2)*(B[t, T] - T + t) - (sigma^2*(B[t, T])^2)/(4*a); P[t_, T_, r_] := Exp[A[t, T] - B[t, T]*r]; D[P[t, T, r], {r, 1}] /. B[t, T] -> HoldForm[B[t, T]] /. P[t, T, r] -> HoldForm[P[t, T, r]] gives the ...



Top 50 recent answers are included