Re: problems with FindRoot: what worked with 4.2 does not work with 5.0
- To: mathgroup at smc.vnet.net
- Subject: [mg47380] Re: problems with FindRoot: what worked with 4.2 does not work with 5.0
- From: "Gareth J. Russell" <gjr2008 at columbia.edu>
- Date: Wed, 7 Apr 2004 03:16:40 -0400 (EDT)
- Organization: Columbia University
- References: <c4u3gg$9l6$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In <c4u3gg$9l6$1 at smc.vnet.net> Edgar Dachs wrote: > > I have written a Mathematica program called PET (Petrological > elementary tools) for Mathematica (Dachs, 1998, Computers & > Geosciences, 24/3: 219-235) und worked out a new version that I > tested with Mathematica version 4.2. With 5.0, I now find problems > that several of my functions do not work any more, obviously do to > changes that have been employed between 4.2 and 5.0 concerning > FindRoot. Below is a little test-function that illustrates the problem: > the return-value of the function depends on the value of x, as in the > much more complex petrological functions that I have written, where > calculations depend e.g. on the value of the temperature. > > test[x_] := Module[{y}, > If[x > 0, y = x + 1]; > If[x <= 0, y = x - 1]; > Return[y] >] > With 4.2 FindRoot (using two startin values for x) could have been > used to solve for a special value, e.g. > > FindRoot[test[x] == 5, {x, {8, 9}}] > > with the obvious result: {x -> 4.} > > With 5.0 the call > > FindRoot[test[x] == 5, {x, 9}] > > (only one starting value can be specified with 5.0) yields the error > message: FindRoot::nlnum "the function value {..} is not a list of > numbers with dimension {1} at {x} = {9} > > I would be grateful if someone could help me solving this problem, at > the moment I don't know what to do, but it looks like a compatibility > problem between Mathematica versions 4.2 and 5.0. > > Thanks in advance, > > Dr. Edgar Dachs > Department of Mineralogy > University Salzburg > Austria Edgar, I'm not sure why it worked in v4 and failed in v5, but your coding of the If statement is a bit unorthodox. If you rewrite thus: test[x_] := Module[{y}, y = If[x > 0, x + 1, x - 1]; Return[y] ] ] It works. In[34]:= FindRoot[temp[x]==5,{x,8}] Out[34]= {x->4.} This particular piece of code could, of course, be written test[x_] := If[x > 0, x + 1, x - 1] Gareth Russell Columbia University