FindRoot question
- To: mathgroup at smc.vnet.net
- Subject: [mg29877] FindRoot question
- From: klaus at biosci.cbs.umn.edu (Chris Klausmeier)
- Date: Mon, 16 Jul 2001 00:28:50 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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