Re: Another basic (?) question about RecurrenceTable and replacement
- To: mathgroup at smc.vnet.net
- Subject: [mg122429] Re: Another basic (?) question about RecurrenceTable and replacement
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Fri, 28 Oct 2011 05:35:32 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 10/26/2011 04:39 PM, victorphy wrote: > I kind of got confused with my notations : I want to solve > > x_0 = -2, y_0 = 0 > > (x_{n+1},y_{n+1}) = sol(n/10,x_n,y_n) for n = 1..10 > > where *sol* cannot be defined explicitely but is given by something > like > > f[x_, a_] := -(x - a)^3 + (x - a) > sol[a_?NumericQ, b_?NumericQ,c_?NumericQ] := FindRoot[{f[x, a], x +y}, > {x, b}, {y, c}] Here is something that works, although it is hardly elegant. I made a few changes so that the functions would be not quite so simple. ALso moved the y[0] value to evade a singular Jacobian for the modified functions. f[x_, a_] := -(x - a)^3 + Sin[(x - a)] sol[a_?NumericQ, b_?NumericQ, c_?NumericQ] := sol[a, b, c] = {x, y} /. FindRoot[{f[x, a], x + Cos[y]}, {x, b}, {y, c}] In[123]:= first[{x_?NumericQ, y_?NumericQ}] := x last[{x_?NumericQ, y_?NumericQ}] := y In[125]:= RecurrenceTable[{x[n + 1] == first[sol[n/10, x[n], y[n]]], y[n + 1] == last[sol[n/10, x[n], y[n]]], x[0] == -2, y[0] == 1}, {x, y}, {n, 0, 10}, DependentVariables -> {x, y}] Out[125]= {{-2, 1}, {-0.928626, 0.380103}, {-0.828626, 0.594147}, {-0.728626, 0.754482}, {-0.628626, 0.891011}, {-0.528626, 1.01381}, {-0.428626, 1.12782}, {-0.328626, 1.23595}, {-0.228626, 1.34013}, {-0.128626, 1.44181}, {-0.0286263, 1.54217}} Alternatively this might be constructed via a NestList. In[167]:= recur[n_] := Module[{j = -1}, NestList[(j++; sol[j/10, #[[1]], #[[2]]]) &, {-2, 1}, n]] In[168]:= recur[10] Out[168]= {{-2, 1}, {-0.928626, 0.380103}, {-0.828626, 0.594147}, {-0.728626, 0.754482}, {-0.628626, 0.891011}, {-0.528626, 1.01381}, {-0.428626, 1.12782}, {-0.328626, 1.23595}, {-0.228626, 1.34013}, {-0.128626, 1.44181}, {-0.0286263, 1.54217}} The latter method has the advantage of not needing to store values for sol[]. Strictly speaking that's not necessary for the former either, but it avoids a recomputation in RecurrenceTable. Daniel Lichtblau Wolfram Research