Re: NestWhile and FindRoot[Integrate]
- To: mathgroup at smc.vnet.net
- Subject: [mg42411] Re: [mg42398] NestWhile and FindRoot[Integrate]
- From: Selwyn Hollis <selwynh at earthlink.net>
- Date: Sat, 5 Jul 2003 03:10:57 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On Friday, July 4, 2003, at 01:33 AM, Mukhtar Bekkali wrote: > I have two questions: > > I have function f(x,t), where 0<x<1 and trying to find the value of x > which > sets Integrate[f(x,t)g(t),{t,0,1}]=0 for some p.d.f. g(t). So, I > programmed > FindRoot[Integrate[f(x,t)g(t),{t,0,1}]==0,{x,0.5}], however, this > head-on > approach works only for simple functions f(x,t) and g(t). It seems > that > Mathematica is trying to integrate first symbolically and then solve > it. Is > there other way to attach the problem? Since you're using FindRoot, why not use NIntegrate? > > Another question relates to loops (totally unrelated to the above > problem). > I am trying to find a maximum of function q(y) using Newton's method > (I want > to do it manually for my own reasons and do not want to use any > built-in > functions). Newton's method essentially substitutes different values > of y > into q(y), but in its special way, and then checks if this is the > maximum. > So my code is as follows (for some specific functions q(y) and > parameter z): > > y0=0; > m=D[q(y),y]/D[q(y),{y,2}] > y1=y0-m/.y->y0; > NestWhile[{y0=y1,y1=y0-m/.y->y0},{y0,y1},Abs[(q(y)/.y->y1)-(q(y)/.y- > >y0)]>z] > > However, instead of updating y0 and y1 with the old values of y0 and y1 > (i.e. set y0new=y1old and y1new=y0new-m/.y->y0new) it returns the > initial > values of y0 and y1 and that's it. > You're doing a few things wrong. First, you need q[y], not q(y). Next, your stopping criterion needs to use q'[y], not q[y]. Finally, the first and last arguments to NestWhile have to be functions. Try this: q[y_]:=Sin[y]; y0 = 1.; z = 10^(-6); m = q'[y]/q''[y]; y1 = y0 - m /. y -> y0; step[{y0_, y1_}] := {y1, y1 - m /. y -> y1}; stop[{y0_, y1_}] := With[{dq=q'[y]}, Abs[(dq/.y->y1)-(dq/.y->y0)] > z]; NestWhile[step, {y0, y1}, stop] ----- Selwyn Hollis Applied Symbols http://www.appliedsymbols.com