Re: Help with Loop to Rule Based Algorithm
- To: mathgroup at smc.vnet.net
- Subject: [mg114789] Re: Help with Loop to Rule Based Algorithm
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Fri, 17 Dec 2010 03:28:39 -0500 (EST)
- References: <201012161048.FAA11817@smc.vnet.net>
On Dec 16, 2010, at 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! You didn't specify u_{i}, I assume u_{i} = u_{1}, otherwise just use Nest Nest[#+f[#,u] (u-#)&,u,n] Note, don't use capital N as a variable name, in general don't use variable names with initial capital letters to avoid name collision with predefined functions. Regards, Sseziwa PS - If u_{i} /= u_{1} use Fold, Fold[#+f[#,#2] (#2-#)&,First[u],Rest[u]] Assuming u is a list of the u_{i}.
- References:
- Help with Loop to Rule Based Algorithm
- From: Daniel Lockhart <daniel.lockhart@gmail.com>
- Help with Loop to Rule Based Algorithm