MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Help with Loop to Rule Based Algorithm

  • To: mathgroup at
  • Subject: [mg114780] Re: Help with Loop to Rule Based Algorithm
  • From: Murray Eisenberg <murray at>
  • Date: Fri, 17 Dec 2010 03:27:00 -0500 (EST)
  • References: <>
  • Reply-to: murray at

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 

    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
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

  • Prev by Date: Re: VectorPlot on a Circle
  • Next by Date: Re: Placing the "&"
  • Previous by thread: Re: Help with Loop to Rule Based Algorithm
  • Next by thread: Re: Help with Loop to Rule Based Algorithm