# Wrong result by FindRoot

Slight background: I am trying to find the coexistence reduced pressure ($$p_r$$) of a Van der Waals fluid at a reduced temperature of 0.85, this requires finding the solution to the following system of equations where $$p_r$$ is the only control variable:

1. $$p_r = -(3/v_r^2) + (8 * 0.85)/(-1 + 3 v_r)$$

Of course, the above equation is a cubic in vr, and you get three positive roots for certain values of $$0. I label the smallest of these roots $$v_{r,1}$$ up to $$v_{r,3}$$. Then, the second equation to be satisfied is the following integral equation:

1. $$\int_{v_{r,1}}^{v_{r,3}} \left[-(3/v_r^2) + \frac{(8 * 0.85)}{(-1 + 3 v_r)}\right] dv_r = p_r(v_{r,3}-v_{r,1})$$

The integral turns out to have an analytical solution, so to make things easier, I write:

$$\int_{v_{r,1}}^{v_{r,3}} \left[-(3/v_r^2) + \frac{(8 * 0.85)}{(-1 + 3 v_r)}\right] dv_r=\frac{3}{v_{r,3}} - \frac{3}{v_{r,1}} + \frac{8*0.85}{3}\, \, Log[\frac{-1 + 3 v_{r,3}}{-1 + 3 v_{r,1}}]$$

To do that on Mathematica, I introduce the following module:

f[pr_, tr_] := Module[{vr1, vr2, vr3},
sol = Sort[vr /. NSolve[pr == -(3/vr^2) + (8*tr)/(-1 + 3 vr), vr]];
vr1 = sol[]; vr2 = sol[]; vr3 = sol[];
Re[(3/vr + 8/3 tr Log[-1 + 3 vr] /.
vr -> vr3) - (3/vr + 8/3 tr Log[-1 + 3 vr] /. vr -> vr1) -
pr*(vr3 - vr1) ]
]


Then I plot f[pr,0.85] to find a graphical solution:

Plot[f[pr, 0.85], {pr, 0.4, 0.55}] So it is clear that the solution is pr = 0.505 (also independently checked).

However, watch what happens when I use FindRoot:

FindRoot[f[pr, 0.85], {pr, 0.4, 0.1, 0.6}, Method -> "Newton"]


I get:

{pr -> 0.530736}


I've tried changing the Method and the search intervals to no avail.

• The difference is that Plot holds it's first argument, while FindRoot does not. The simplest solution is to add an argument restriction, i.e., f[pr_?NumericQ, tr_] := Module[...]. – Carl Woll Sep 19 '19 at 16:14
• Guess what? It works! Thank you. Why did that happen though? – Assaad Mrad Sep 19 '19 at 16:16
• support.wolfram.com/kb/12502 – user6014 Sep 19 '19 at 16:20

The problem is that FindRoot evaluates f[pr, .8] where pr is just a symbol, it is not a number. Then, it replaces pr with a number. However, the order of the roots when pr is a symbol differs from the order of the roots when pr is a number. Here is a function that just solves your first equation:

g[pr_, tr_] := Sort[vr/.NSolve[pr==-(3/vr^2)+(8*tr)/(-1+3 vr),vr]];


Now, let's evaluate g with a symbolic pr:

g[pr, .85] //Short


{-((0.0222222 (-34.-<<1>>))/pr)+((<<21>> -<<1>>) <<1>>)/(pr <<1>>^<<1>>)-((<<1>>) <<1>>)/pr,<<1>>,<<1>>}

It's rather complicated, so I truncated the output. Now compare substituting a value for pr into the above expression versus just evaluating g with a numeric pr:

g[pr, .85] /. pr -> .5 //Chop
g[.5, .85]


{1.13734, 0.553756, 3.17557}

{0.553756, 1.13734, 3.17557}

The order is different. This is why your FindRoot code doesn't work. Defining f so that it doesn't evaluate for symbolic pr fixes your problem:

Clear[f]
f[pr_?NumericQ, tr_] := Module[{vr1, vr2, vr3},
sol = Sort[vr /. NSolve[pr == -(3/vr^2) + (8*tr)/(-1 + 3 vr), vr]];
vr1 = sol[]; vr2 = sol[]; vr3 = sol[];
Re[
(3/vr + 8/3 tr Log[-1 + 3 vr] /. vr -> vr3) -
(3/vr + 8/3 tr Log[-1 + 3 vr] /. vr -> vr1) -
pr*(vr3 - vr1)
]
]
FindRoot[f[pr, .85], {pr, .5}]


{pr -> 0.504492}

Another possibility is to use Solve with the Cubics->False option instead of NSolve. This is because Solve will return Root objects, and Mathematica knows how to sort Root objects even for symbolic arguments:

Clear[f]
f[pr_, tr_] := Module[{vr1, vr2, vr3},
sol = vr /. Quiet @ Solve[pr == -(3/vr^2) + (8*tr)/(-1 + 3 vr), vr, Cubics->False];
vr1 = sol[]; vr2 = sol[]; vr3 = sol[];
Re[
(3/vr + 8/3 tr Log[-1 + 3 vr] /. vr -> vr3) -
(3/vr + 8/3 tr Log[-1 + 3 vr] /. vr -> vr1) -
pr*(vr3 - vr1)
]
]
FindRoot[f[pr, .85], {pr, .5}]


{pr -> 0.504492}

(the Quiet is to avoid messages related to using exact methods with inexact coefficients)

Let's examine the output of Solve:

Solve[pr == -(3/vr^2) + (8*tr)/(-1 + 3 vr), vr, Cubics->False]


{{vr -> Root[-3 + 9 #1 + (-pr - 8 tr) #1^2 + 3 pr #1^3 &, 1]}, {vr -> Root[-3 + 9 #1 + (-pr - 8 tr) #1^2 + 3 pr #1^3 &, 2]}, {vr -> Root[-3 + 9 #1 + (-pr - 8 tr) #1^2 + 3 pr #1^3 &, 3]}}

Notice how each solution is just a Root object with an index. By default, the indices correspond to roots with increasing real part. This is what you were trying to achieve with your Sort, so it isn't necessary in the above code.
{"ft_lang_label":"__label__en","ft_lang_prob":0.68505585,"math_prob":0.9991322,"size":1770,"snap":"2019-51-2020-05","text_gpt3_token_len":675,"char_repetition_ratio":0.109852776,"word_repetition_ratio":0.07457627,"special_character_ratio":0.44858757,"punctuation_ratio":0.1880734,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9994929,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-18T05:56:26Z\",\"WARC-Record-ID\":\"<urn:uuid:8d0e2df5-c535-4b14-9819-15a6e0dff3d5>\",\"Content-Length\":\"144047\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5201bcef-25a3-4ae7-ab45-3800c09b5585>\",\"WARC-Concurrent-To\":\"<urn:uuid:fd32d9e0-4e18-49d9-a1ff-659f2861188e>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/206513/wrong-result-by-findroot\",\"WARC-Payload-Digest\":\"sha1:TYHR6CWL2CXUMAQZCXG4SBKP76DZZNS3\",\"WARC-Block-Digest\":\"sha1:4JTQWYHS55MDLYYQBINBPU6GU663NRHK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250592261.1_warc_CC-MAIN-20200118052321-20200118080321-00212.warc.gz\"}"}