Re: Help with Loop to Rule Based Algorithm

*To*: mathgroup at smc.vnet.net*Subject*: [mg114802] Re: Help with Loop to Rule Based Algorithm*From*: Daniel Lockhart <daniel.lockhart at gmail.com>*Date*: Fri, 17 Dec 2010 03:31:01 -0500 (EST)*References*: <201012161048.FAA11817@smc.vnet.net>

Murray, Thanks for the reply. Yes, the pseudocode is a bit inconsistent. I intended alpha to a function of arrays x and u, from 1 < n < i, if i is the current step. Your notation x[i_] := x[i] = x[i-1] + f[x[i-1], u[i-1]] (u[i] - x[i-1]) completely helped me understand how to do this in Mathematica. I think this does an example of what I am looking for: Clear[dat, datFilt, datFiltnon, x, z, alpha] dat = Evaluate[Sin[2 Pi omega t] /. {omega -> 0.5, t -> Table[n, {n, 0, 10, 0.05}]}]; u[n_] := dat[[n]] x[1] = u[1]; z[1] = u[1]; alpha[f_, n_] := If[n == 1, f[n], 0.5 Abs[f[n] - f[n - 1]]] x[i_] := x[i] = x[i - 1] + 0.15 (u[i] - x[i - 1]) z[i_] := z[i] = z[i - 1] + alpha[u, i] (u[i] - z[i - 1]) datFilt = Table[x[n], {n, 1, Length[dat]}]; datFiltNon = Table[z[n], {n, 1, Length[dat]}]; ListPlot[{dat, datFilt, datFiltNon}] Here, x is the linear filter output of u, and z is the non-constant gain filter of input u. Thanks! On Thu, Dec 16, 2010 at 8:31 AM, Murray Eisenberg <murray at math.umass.edu> wrote: > Your pseudo-code seems to be using three different notations involving u: > > In first line, u{1} [braces, no subscript] > > In third line, u [ I presume alpha should be a number; but your x is, it > would seem an array you're defining in the loop and your u seems to be an > array, too; so is alpha a constant depending on the entire array u and the > entire array x, or what ??] > > In fourth line, u_{i}, [ with a subscript] > > So first let me ask if the following, modified, pseudo-code is what you > meant: > > x_{1} = u_{1}; > for i = 2 to N > alpha = f(x_{i-1}, u_{i-1}); > x_{i} = x_{i-1} + alpha * (u_{i} - x_{i-1}); > end for; > > If not, what DID you mean? > > If so, then in Mathematica: > > x[1] = u[1]; > x[i_] := x[i] = x[i-1] + f[x[i-1], u[i-1]] (u[i] - x[i-1]) > > This is a recursive definition, of course, which may or may not suit your > purposes. It gives two rules, the particular one for x at 1 (which as a > particular rule takes precedence over a general rule about x), and the > general, recursive rule. > > The latter uses the "x[i_] := x[i] = ... " expression in order to make > Mathematica "remember" a value of x[i] as soon as it calculates it, in other > words, to add a rule for, say, x[2] = .... and x[3] = ..., etc., as soon as > they are first calculated, so the recursion doesn't have to descend to the > base each time a new value is calculated. > > > On 12/16/2010 5:48 AM, Daniel Lockhart wrote: >> >> Hi all, >> >> I have been a long time Mathematica user, but have always had problems >> writing Mathematica code using rules from existing algorithms that utilize >> loops. >> >> Suppose I want to write a simple smoothing filter, but not use any >> built-in Mathematica algorithm. Just to make the point, I want the smoothing >> gain/coefficient to be a nonlinear function. So, I want this pseudocode: >> >> x_{1} = u{1}; >> for i = 2 to N >> alpha = f(x, u); >> x_{i} = x_{i-1} + alpha * (u_{i} - x_{i-1}); >> end for; >> >> if u is the input data, and f(x, u) is a nonlinear function of x and u. If >> alpha was constant and 0< alpha< 1, x would be some sort of moving= average >> of u. >> >> How can I do this in Mathematica without writing an explicit loop? I am >> guessing some from of recursive Map, but I am at a loss on how to start. >> >> Thanks! >> > > -- > Murray Eisenberg murray at math.umass.edu > Mathematics & Statistics Dept. > Lederle Graduate Research Tower phone 413 549-1020 (H) > University of Massachusetts 413 545-2859 (W) > 710 North Pleasant Street fax 413 545-1801 > Amherst, MA 01003-9305 >

**References**:**Help with Loop to Rule Based Algorithm***From:*Daniel Lockhart <daniel.lockhart@gmail.com>

**Re: FindRoot failing in v8 on a system easily solvable in v7**

**Re: Obtaining information about function definitions**

**Re: Help with Loop to Rule Based Algorithm**

**how to debug existing notebook in workbench?**