Re: Testing the 'type' of a root returned by Solve
- To: mathgroup at smc.vnet.net
- Subject: [mg54671] Re: [mg54626] Testing the 'type' of a root returned by Solve
- From: "David Park" <djmp at earthlink.net>
- Date: Fri, 25 Feb 2005 01:19:41 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Mike, sols=N[Solve[20*Feet^3 == (4*Pi*r^3)/5, r]] (If[FreeQ[#1, Complex], Real, Complex] & ) /@ % {Complex, Complex, Real} The following is a little beyond the question. I am always a little dubious about mixing data values and units into equations before they are solved. If is often better to obtain a general and exact solution if possible. So for your problem we could use... sols = Solve[V == (4*Pi*r^3)/5, r] {{r -> -(((-(5/Pi))^(1/3)*V^(1/3))/2^(2/3))}, {r -> ((5/Pi)^(1/3)*V^(1/3))/2^(2/3)}, {r -> ((-1)^(2/3)*(5/Pi)^(1/3)*V^(1/3))/2^(2/3)}} Since V is positive we can test with... If[FreeQ[#, Complex], Real, Complex] & /@ N[sols] {Complex, Real, Complex} Then we can substitute the 'data' into the the second real solution and obtain an exact answer. Simplify[sols[[2]] /. V -> 20*Foot^3, Foot > 0] {r -> (5^(2/3)*Foot)/Pi^(1/3)} David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Mike Witt [mailto:mwNOSPAM at mu.uoregon.edu] To: mathgroup at smc.vnet.net If I solve an equation in which the solutions turn out to be functions of some variable, I can't figure out how to pick out one of the roots based on whether the root is real or complex. The problem is that Head[] reports that the roots are all "Times" because of the variable in them. The following notebook demonstrates. Can someone tell me the right way to do this (or point me to the right place in the book or help pages?) For private email remove the NOSPAM. -Mike filename="foo.nb" (************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.0' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info at wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. *******************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 2544, 85]*) (*NotebookOutlinePosition[ 3174, 107]*) (* CellTagsIndexPosition[ 3130, 103]*) (*WindowFrame->Normal*) Notebook[{ Cell["\<\ I want to be able to select the 'real' root using the 'Head' \ function, like this:\ \>", "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[ \(Head /@ \((r /. \ \((Solve[20 == \(4\ \[Pi]\ r\^3\)\/5, \ r] // N)\))\)\)], "Input"], Cell[BoxData[ \({Complex, Complex, Real}\)], "Output"] }, Open ]], Cell["\<\ But when I add a variable, then the 'type' of every root becomes \ 'Times' because of the product of the variable and the number. Is 'Head' the \ wrong way to do this?\ \>", "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[ \(Head /@ \((r /. \((\ Solve[20 Feet\^3 == \(4\ \[Pi]\ r\^3\)\/5, \ r] // N)\))\)\)], "Input"], Cell[BoxData[ \({Times, Times, Times}\)], "Output"] }, Open ]] }, FrontEndVersion->"5.0 for X", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{520, 600}, WindowMargins->{{Automatic, 24}, {69, Automatic}} ] (******************************************************************* Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. *******************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[1754, 51, 123, 4, 52, "Text"], Cell[CellGroupData[{ Cell[1902, 59, 119, 2, 51, "Input"], Cell[2024, 63, 58, 1, 27, "Output"] }, Open ]], Cell[2097, 67, 207, 5, 71, "Text"], Cell[CellGroupData[{ Cell[2329, 76, 141, 3, 51, "Input"], Cell[2473, 81, 55, 1, 27, "Output"] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)