Re: Iterating List
- To: mathgroup at smc.vnet.net
- Subject: [mg77137] Re: Iterating List
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 4 Jun 2007 03:59:01 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f3ltcf$5df$1@smc.vnet.net><f3og49$p9d$1@smc.vnet.net> <f3u4rb$37j$1@smc.vnet.net>
R.G wrote: > On Jun 1, 2:59 pm, Jean-Marc Gulliet <jeanmarc.gull... at gmail.com> > wrote: >> Gobiithasan wrote: >>> Hi, it would be a great help if Mathsgroup members can >>> show me how to code the following problem in >>> mathematica: >>> Say I have a function defined as: >>> f[a_, b_] = Integrate[Sin[x] + 3, {x, a, b}] >>> I can find b with given constant=2 and initial >>> value=0: >>> In[1]:= >>> Initialvalue = 0; >>> s1 = FindRoot[f[0, unknown] == 2, {unknown, >>> Initialvalue}]; >>> x1 = Part[s1, 1, 2] >>> Now, using the x1 value obtained from FindRoot, I can >>> find x2: >>> s2 = FindRoot[f[x1, unknown] == 2, {unknown, >>> Initialvalue}]; >>> x2 = Part[s2, 1, 2] >>> How can i do it for n times (to get xn in a list form) >>> with given initial value as: >>> initialvalue= {initialvalue1,initialvalue2, >>> ....,initialvalue3} >>> whereby each xi will get initialvalue[[i]]. >>> Thanking you in advance >>> ~gobiithasan >> One possible way of doing what you are looking for is the use a >> functional construct such as NestList and a pure function (that why you >> can see #1 and &). For instance, >> >> In[1]:= f[a_, b_] = Integrate[Sin[x] + 3, {x, a, b}]; >> With[{Initialvalue = 0, n = 5}, >> Rest[NestList[FindRoot[f[#1, unknown] == 2, >> {unknown, Initialvalue}][[1, 2]] & , 0, n]]] >> >> Out[1]= {0.607102, 1.13938, 1.64271, 2.15069, 2.69881} >> >> Regards, >> Jean-Marc > > Hi Jean-Marc, > Thank you for your answer. The code you wrote works for > Initialvalue=0. > However, If I have initial values in terms of a list, say, > initialvalues=Table[t,{0,1/n}], How do i modify it? > Best Regards, > R.G Among many other ways, In[1]:= f[a_, b_] = Integrate[Sin[x] + 3, {x, a, b}]; With[{n = 3}, Module[{initialvalues = Table[i, {i, 0, 5}], g}, g = Function[{initval}, Rest[NestList[FindRoot[f[#1, unknown] == 2, {unknown,initval}][[1, 2]] & , 0, n]]]; g /@ initialvalues]] Out[2]= {{0.6071016481031226, 1.139384548290925, 1.6427145767770854}, {0.6071016481031226, 1.1393845482909248, 1.6427145767770859}, {0.6071016481031226, 1.1393845482909244, 1.642714576777085}, {0.6071016481031226, 1.1393845482909244, 1.6427145767770854}, {0.6071016481031227, 1.1393845482909244, 1.6427145767770854}, {0.6071016481031227, 1.139384548290925, 1.6427145767770859}} Regards, Jean-Marc