New answers tagged

0

All credit goes to Simon Woods. Simple mathematical modelling of a sound with graphical representation and playback. Sound synthesis done in plain Wolfram Language. Wu! I suggest to leave "Envelope lenght" slider where is at. Don't take the "Frequency" values seriously; my impression is there's something there to be fixed yet. segment[t_, t1_, t2_, a1_, ...


12

First let me observe that your coding style makes debugging difficult, I highly recommend breaking giant expressions into manageable pieces. Second, in the code below I have used a different definition for the segments. Your version: $y=(x-x_1)^{curvature}\frac{y_2-y_1}{x_2-x_1}+y_1$ does not give an amplitude of $y_2$ at $x=x_2$ if $curvature\neq1$. I ...


4

A few points to make here: Always use Listable attributes of functions, that will speed things up. When unnecessary, do not use symbolic processing, use numeric processing instead. Thus, I'll first change the data to N form, then use Listable attributes of Mean and StandardDeviation to get the result in a shorter and faster code. imgd = N@imageData; ...


2

A Copy-Paste of (or a shameless plagiarism to mark the question answered) link suggested by m_goldberg http://reference.wolfram.com/language/example/SimulateABouncingBall.html h = 20; a = 0.7; t0 = 10; (*time of flight*) ball[t_] = y[t] /. NDSolve[{y''[t] == -9.81, y[0] == h, y'[0] == 0, WhenEvent[y[t] == 0, y'[t] -> -a y'[t]]}, y, {t, 0, t0}][[1]...


2

If I am not mistaken your final expression may be reduced to: Table[ { ggr[ randomList[[1]], randomList[[1 + 2 j]] ] }, {j, mm*nn} ] {{-7.01292 - 10.5223 I}, {-8.64938 - 9.92198 I}, {-10.2158 - 9.0987 I}, {-8.64938 - 9.92198 I}} Or equivalently: Array[{ggr[randomList[[1]], randomList[[1 + 2 #]]]} &, mm*nn]


4

Probably this should be a comment to JasonB's post, but I have too little reputation to comment... Here is another version using Scan: Scan[If[f@# == 10, Return@#]&, Range[10]] (* 5 *) Returning the index of an arbitrary list: l = Range[1, 21, 2] (* {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21} *) Scan[If[f@l[[#]] == 10, Return@#] &, Range@Length@l] (*...


3

I hesitate to add this because it's so ugly, but it works as requested. It will give the last number meeting the requirement, and will stop the first time it encounters a number that doesn't nn = 0; Scan[If[f@# <= 10, nn++, Return@nn] &, Range@10] (* 5 *)


4

TakeWhile[Range@10, f@# <= 10 &] // Length Alternative answer that might be helpful depending on the issue at hand If your list is sorted and your test is monotonic, instead of checking every list entry from the beginning you could use a binary search (divide and conquer approach) Clear@f f[x_] := (Pause@2; 2 x) (* your costly condition*) Needs["...


10

1 + LengthWhile[Range@10, f@# != 10 &] 5


13

SelectFirst would work: SelectFirst[Range[10], f[#] == 10 &] You can tell that it only executes f on indexes 1-5 if you add a Print statement to the definition: f[x_] := (Print[x]; 2 x)


2

You can use NestWhile f[x_]:=x^2; (*your function.*) NestWhile[#+1&,0,f[#+1]<10&] Which returns 3 as the last number satisfying the condition. The #+1 in the expr portion increments the iterator for the NestWhile, and the #+1 in the test tests the next value of the iterator. If the next value fails the test you get the current value of the ...


15

As is demonstrated very well in this post you can use a criteria for your pattern, thereby only applying your function as long as you are searching and not to all elements. Also there is a specific FirstPosition function. f[x_] := Module[{}, Pause[0.5]; 2 x] AbsoluteTiming[ Position[f /@ Range[10], 10, 1, 1] ] AbsoluteTiming[ FirstPosition[f /@ Range[...


3

Maybe you just want a efficient way to do this while retaining the procedural approach. If that is the case, you can consider the following: ok = True; f[x_] := If[ok, 2 x, Null]; For[i = 1, i <= 10, i++, If[f[i] == 10, (ok = False; Return[i])]] (* Return[5] *) ok = True; Position[f /@ Range[10], 10] (* {{5}} *)


5

You need to construct an event for each i from 1 to n: Block[{n = 2, a = 1.1}, vars = Table[x[i], {i, n}]; eqns = Table[x[i]'[t] == a - x[i][t], {i, n}]; initcond = Table[x[i][0] == 0.3*i, {i, n}]; evts = Table[With[{i = i}, WhenEvent[x[i][t] == 1, x[i][t] -> 0]], {i, n}]; sol = NDSolve[{eqns, initcond, evts}, vars, {t, 0, 10}]; ] Plot[Evaluate[...


4

You can use my OptionsValidation framework to add options validation to your functions. We start by loading the package: Import["https://raw.githubusercontent.com/jkuczm/MathematicaOptionsValidation/master/NoInstall.m"] Now "register" tests you want to perform on option values. You do it by defining CheckOption for your function. ClearAll[func] Options[...



Top 50 recent answers are included