Wrong result by FindRootNewton-Raphson Method and the Van der Waal Equation Coding questionNumerical solution of two coupled transcendental equations with three variablesIssue in ParallelTable after evaluating another function using NDSolve and FindRoot. What is wrong with this inverse?NSolve gives an empty solution at some valuesThird degree equation resolutionFind roots of transcendental function in regional complex planeHow can I solve this transcendental equation in Mathematica?Error messages for FindRoot and NIntegrate

Where are they calling from?

What are these pixel-level discolored specks? How can I fix it?

Was there a trial by combat between a man and a dog in medieval France?

What is the lowest voltage that a microcontroller can successfully read on the analog pin?

Which museums have artworks of all four Ninja Turtles' namesakes?

Resolving moral conflict

What are these ingforms of learning?

To what extent is it worthwhile to report check fraud / refund scams?

Is it true that, "just ten trading days represent 63 per cent of the returns of the past 50 years"?

Is there a scenario where a gnoll flesh gnawer can move at least 45 feet during its Rampage bonus action?

What is the meaning of "heutig" in this sentence?

How is the problem, G has no triangle in Logspace?

Do all creatures have souls?

Why is the missed-approach course for the "RNAV (GNSS) - A" approach to runway 28 at ENSB shaped all funny?

A drug that allows people to survive on less food

Where does an unaligned creature's soul go after death?

What benefits does the Power Word Kill spell have?

How to make interviewee comfortable interviewing in lounge chairs

How to conditionally load a package only if shell-escape (write18) is passed

An Algorithm Which Schedules Your Life

What is the meaning of word 'crack' in chapter 33 of A Game of Thrones?

Is there any reason nowadays to use a neon indicator lamp instead of an LED?

Norwegian refuses EU delay (4.7 hours) compensation because it turned out there was nothing wrong with the aircraft

What can a pilot do if an air traffic controller is incapacitated?



Wrong result by FindRoot


Newton-Raphson Method and the Van der Waal Equation Coding questionNumerical solution of two coupled transcendental equations with three variablesIssue in ParallelTable after evaluating another function using NDSolve and FindRoot. What is wrong with this inverse?NSolve gives an empty solution at some valuesThird degree equation resolutionFind roots of transcendental function in regional complex planeHow can I solve this transcendental equation in Mathematica?Error messages for FindRoot and NIntegrate






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1












$begingroup$


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<pr<1$. 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=frac3v_r,3 - frac3v_r,1 + frac8*0.853, , 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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
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]



enter image description here



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.



Can someone please help with this?










share|improve this question











$endgroup$









  • 3




    $begingroup$
    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[...].
    $endgroup$
    – Carl Woll
    8 hours ago






  • 1




    $begingroup$
    Guess what? It works! Thank you. Why did that happen though?
    $endgroup$
    – Assaad Mrad
    8 hours ago







  • 3




    $begingroup$
    support.wolfram.com/kb/12502
    $endgroup$
    – user6014
    8 hours ago

















1












$begingroup$


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<pr<1$. 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=frac3v_r,3 - frac3v_r,1 + frac8*0.853, , 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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
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]



enter image description here



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.



Can someone please help with this?










share|improve this question











$endgroup$









  • 3




    $begingroup$
    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[...].
    $endgroup$
    – Carl Woll
    8 hours ago






  • 1




    $begingroup$
    Guess what? It works! Thank you. Why did that happen though?
    $endgroup$
    – Assaad Mrad
    8 hours ago







  • 3




    $begingroup$
    support.wolfram.com/kb/12502
    $endgroup$
    – user6014
    8 hours ago













1












1








1





$begingroup$


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<pr<1$. 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=frac3v_r,3 - frac3v_r,1 + frac8*0.853, , 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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
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]



enter image description here



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.



Can someone please help with this?










share|improve this question











$endgroup$




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<pr<1$. 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=frac3v_r,3 - frac3v_r,1 + frac8*0.853, , 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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
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]



enter image description here



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.



Can someone please help with this?







equation-solving






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago







Assaad Mrad

















asked 8 hours ago









Assaad MradAssaad Mrad

756 bronze badges




756 bronze badges










  • 3




    $begingroup$
    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[...].
    $endgroup$
    – Carl Woll
    8 hours ago






  • 1




    $begingroup$
    Guess what? It works! Thank you. Why did that happen though?
    $endgroup$
    – Assaad Mrad
    8 hours ago







  • 3




    $begingroup$
    support.wolfram.com/kb/12502
    $endgroup$
    – user6014
    8 hours ago












  • 3




    $begingroup$
    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[...].
    $endgroup$
    – Carl Woll
    8 hours ago






  • 1




    $begingroup$
    Guess what? It works! Thank you. Why did that happen though?
    $endgroup$
    – Assaad Mrad
    8 hours ago







  • 3




    $begingroup$
    support.wolfram.com/kb/12502
    $endgroup$
    – user6014
    8 hours ago







3




3




$begingroup$
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[...].
$endgroup$
– Carl Woll
8 hours ago




$begingroup$
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[...].
$endgroup$
– Carl Woll
8 hours ago




1




1




$begingroup$
Guess what? It works! Thank you. Why did that happen though?
$endgroup$
– Assaad Mrad
8 hours ago





$begingroup$
Guess what? It works! Thank you. Why did that happen though?
$endgroup$
– Assaad Mrad
8 hours ago





3




3




$begingroup$
support.wolfram.com/kb/12502
$endgroup$
– user6014
8 hours ago




$begingroup$
support.wolfram.com/kb/12502
$endgroup$
– user6014
8 hours ago










1 Answer
1






active

oldest

votes


















7














$begingroup$

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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
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.






share|improve this answer











$endgroup$

















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "387"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206513%2fwrong-result-by-findroot%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    $begingroup$

    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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
    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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
    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.






    share|improve this answer











    $endgroup$



















      7














      $begingroup$

      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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
      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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
      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.






      share|improve this answer











      $endgroup$

















        7














        7










        7







        $begingroup$

        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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
        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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
        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.






        share|improve this answer











        $endgroup$



        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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
        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[[1]]; vr2 = sol[[2]]; vr3 = sol[[3]];
        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 8 hours ago

























        answered 8 hours ago









        Carl WollCarl Woll

        90.2k3 gold badges117 silver badges229 bronze badges




        90.2k3 gold badges117 silver badges229 bronze badges































            draft saved

            draft discarded















































            Thanks for contributing an answer to Mathematica Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206513%2fwrong-result-by-findroot%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單