1

I have the notebook below which produces the correct numerical output but the CForm function doesn't appear as though it is working correctly (or I don't know how to interpret the Slot function within Out[4]/CForm.) I'm trying to get something I can stick into c++. How should this be parsed and/or is there a way to get Mathematica to output a more straightforward CForm (complex numbers are fine if needed.)

enter image description here

Edit: Different from that suggested.

CoryG
  • 111
  • 2
  • 2
    Related: http://mathematica.stackexchange.com/questions/13767/how-do-i-work-with-root-objects -- Since the solution is basically an implicit equation solve by an internal Mathematica function, I'm not sure what's best to do in C. – Michael E2 Jan 21 '17 at 02:52
  • 2
    You might want to look into ToRadicals[] to get a "more traditional" expression, which may or may not be numerically sound when you finally embed it in C++. Those coefficients look huge, so you might also want to do a preliminary rescaling. Anyway, as it stands, you don't have anything C++-ready even with that Mathematica result, and you have more work to do. – J. M.'s missing motivation Jan 21 '17 at 04:39
  • @J.M. ToRadicals[] doesn't seem to get there - even just running that through with the variables d and v defined comes in pretty significantly off of the correct value. – CoryG Jan 21 '17 at 07:55
  • 1
    Hence "which may or may not be numerically sound". To repeat: your coefficients are huge, and rescaling seems to be warranted. – J. M.'s missing motivation Jan 21 '17 at 08:42
  • @J.M. How do I go about doing that properly? – CoryG Jan 21 '17 at 08:46
  • @J.M. I'm not exactly sure how it worked (I'm sure it's what you meant by "rescaling") but dropping the x10^7 off all the coefficients worked. Are you able to explain why that worked? I'm not typically into math beyond getting through it for engineering issues but this has me a bit curious of the logic behind it. Is it just a rounding issue? – CoryG Jan 21 '17 at 09:30
  • Maybe an analogy would be appreciated: you could express the height of a building in inches, and you could express the width of a virus in miles, but that doesn't mean they're the appropriate units to use for those quantities. If stuff's going to be large in any computation you're doing, they should be large because it naturally came up, and not because you messed up your initial choice of units. – J. M.'s missing motivation Jan 21 '17 at 13:13
  • 1
    Try Solve[(Pi/3) (y^2) (((3/2) d) - y) == v && d > 0 && y > 0 && v > 0 && v > y && v > d && y <= d, {y}, Reals] // ToRadicals -- Exact 3/2 vs. approximate 1.5 -- People here generally like users to post code as Mathematica code instead of images or TeX, so they can copy-paste it. It makes it convenient for them and more likely you will get someone to help you. You may find this this meta Q&A helpful -- which is why I was slow to try it, what with a regular job and all.... :) – Michael E2 Jan 21 '17 at 14:08
  • @MichaelE2 That results in the same issue however I was able to get it via Roots[1/3 Pi (1.5 d - y) y^2 == v, y] and then taking the equation for the third root. – CoryG Jan 22 '17 at 04:01

0 Answers0