Re: FindRoot question
- To: mathgroup at smc.vnet.net
- Subject: [mg29883] Re: [mg29877] FindRoot question
- From: Mianlai Zhou <lailai at nikhef.nl>
- Date: Tue, 17 Jul 2001 01:00:29 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hello, Chris The problem is because the function FindRoot has an attribute HoldAll by default. Namely, it will not evaluate any of its parameters unless you specify it to do so. That's the reason accounting for the fact your second version didn't work. Just put an Evaluate[] on your Table and it will work. Good luck! Mianlai Zhou Theory Group, NIKHEF Amsterdam, The Netherlands On Mon, 16 Jul 2001, Chris Klausmeier wrote: > Dear Mathgroupies -- > > I'd like to set up FindRoot to solve an arbitrary number of > equations. It works when I hardcode the equations in, but it fails > when I use a Table to set up the equations. > > Here's the code that works and its output: > > In: > > nsp=2; > > z=0; > > xopti={0.4,0.6}; > evunks=Table[{xopt[i],xopti[[i]],xopti[[i]]+0.001},{i,1,nsp}]; > > FindRoot[ > z++;Print[z," ",Table[xopt[i],{i,1,nsp}]]; > > ecsol=neq[Table[xopt[i],{i,1,nsp}],Table[0.5,{x,1,xmax},{i,1,nsp}]]; > > {ev[xopt[1]+e,Table[xopt[i],{i,1,nsp}],ecsol]==ev[xopt[1]-e,Table[xopt[i],{i,1,nsp}],ecsol], > ev[xopt[2]+e,Table[xopt[i],{i,1,nsp}],ecsol]==ev[xopt[2]-e,Table[xopt[i],{i,1,nsp}],ecsol]} > > ,Evaluate[Sequence@@evunks]]; > > Out: > > 1 {0.4, 0.6} > 2 {0.401, 0.6} > 3 {0.401, 0.601} > 4 {0.442737, 0.553871} > 5 {0.455686, 0.553536} > 6 {0.45058, 0.549369} > 7 {0.451775, 0.548223} > 8 {0.451856, 0.548144} > 9 {0.451856, 0.548144} > > Here's the code that doesn't work and what happens: > > In: > > nsp=2; > > z=0; > > xopti={0.4,0.6}; > evunks=Table[{xopt[i],xopti[[i]],xopti[[i]]+0.001},{i,1,nsp}]; > > FindRoot[ > z++;Print[z," ",Table[xopt[i],{i,1,nsp}]]; > > ecsol=neq[Table[xopt[i],{i,1,nsp}],Table[0.5,{x,1,xmax},{i,1,nsp}]]; > > Table[ev[xopt[j]+e,Table[xopt[i],{i,1,nsp}],ecsol]==ev[xopt[j]-e,Table[xopt[i],{i,1,nsp}],ecsol],{j,1,nsp}] > > ,Evaluate[Sequence@@evunks]]; > > Out: > > 1 {xopt[1], xopt[2]} > > 2 > FindRoot::frnum: Function {0.5 + <<1>> - 5. (-0.05 - 0.5 (0.5 - > 0.31831 ArcTan[20. (-1. (0.1 + <<1>>) + <<1>>)])), <<8>>, > 0.5 + <<2>>} is not a length 10 list of numbers at > {n[1][1], n[2][1], n[1][2], n[2][2], n[1][3], n[2][3], n[1][4], > n[2][4], n[1][5], n[2][5]} = > {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5}. > > ... and so on ... > > ev[] and neq[] are functions I defined earlier. I put this amount of > programming within the FindRoot because neq[] is computationally > expensive, so I want to do it once per iteration and recycle the > result. > > How can I get the second version to work (for an arbitrary number of > equations, nsp)? And why are the xopt's defined the first time > through the FindRoot in the first example, but undefined in the > second? > > Also, is there a way to reject a full step in the middle of a > FindRoot? I want to reject a large step in the xopts if it causes > part of my neq to become zero. > > thanks much -- Chris >