Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica output seems incorrect

  1. Nov 25, 2011 #1
    I am using Mathematica to create a formula to solve for (Vs) in an equation shown at the top of the attached image. Using the assumptions shown, Mathematica outputs three possible solutions, all of which appear to be incorrect when I check them using actual values.

    I get this error when Mathematica generates the three possible solutions :

    "Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result."

    ...and I believe this may be the cause of the problem, but I have no idea how to get around this.

    If I plug sample values in to the original equation (as shown at the bottom of the attached jpg) it will produce a valid result (596.704) as the third possible solution.

    If I plug these same values in to the second of the three long equations Mathematica has given me it will produce a value for (Vs) that is very close to correct (596.7044277254138 - 3.67761*10^-15 I), but includes an additional small imaginary number

    Does anyone have experience with Mathematica that may help me produce a valid equation to solve for (Vs)?

    (Vs) is peak velocity in a trapezoidal move, while (Vi) is initial velocity, which is always greater than zero for my purposes. (Ma) is counts of acceleration, (Md) is counts of deceleration, (d) is counts of total distance , (t) is total time. Final velocity is always assumed to be zero, so it is not shown in the original formula.

    Original formula:

    Assuming[Vs \[Element] Reals; d > 0; t > 0; Vi > 0; Vs > 0; Ma > 0;
    Md < 0;, Solve[ d == ((Vs + Vi)/2)*((Vs - Vi)/(.5*Vs^2/Ma)) + ((Vs/2)*(-Vs/(.5*Vs^2/Md))) + (Vs*(t - (-Vs/(.5*Vs^2/Md)) - ((Vs - Vi)/(.5*Vs^2/Ma)))), Vs]]



    Attached Files:

    Last edited: Nov 25, 2011
  2. jcsd
  3. Nov 25, 2011 #2
    I used the Reduce function on my on my source equation the output gives three solutions for (Vs) that use the Root[] function.

    The first solution gives the correct answer for my purposes when I refine it using a set of actual values in place of the variables.

    I don't really understand what Root[] is doing enough to know what to do with this information, or how to rewrite this as a formula that I can use to solve for (Vs). Can anyone explain Root[] to me or re-write this in another format?


    Attached Files:

  4. Nov 25, 2011 #3
    If you attach a small Mathematica notebook that is simplified down to where it clearly shows what your difficulty is and just what calculation you want to have done or what result is incorrect then I think you are much more likely to get some good help than if you post some jpg file that anyone trying to help will likely have to type all back in to verify their efforts and to triple check to make sure they didn't introduce any new errors by typing all your code back in.

    If, unless you are specifically dealing with a numerical analysis problem, you stop inserting decimal points and instead use exact fractions I think you will far more likely get sensible results from a number of different Mathematica functions. Only in a recent version update of Mathematica did Reduce stop crashing Mathematica or failing to give an answer when it was given some equations including decimal points. Calculators use decimal points, FORTRAN uses decimal points, Mathematica will often be much more understanding when you use 1/2 instead of 0.5.

    With a small clean notebook pointing out exactly where the problem is and what all the values of your parameters are and what you want the answer to be I will be happy to take a few minutes to try to track down what when wrong.
  5. Nov 26, 2011 #4

    Thanks so much, I should have done that to start with. I have attached a notebook showing the original formula I was using(the formula using 0.5 instead of a fraction).

    I want Mathematica to solve for Vs so I can write out this new formula as part of programming a human-machine-interface that commands a motion controller.

    This notebook shows the three resulting formulas, none of which worked when I plugged in test values. The formula that came closest is shown in the second input in the notebook.

    The third input in the notebook shows the same sample values plugged in to the original formula, showing a valid result for Vs as the third possible solution.

    A couple rules: Vi is always greater than zero, and Md is always a negative number. (Md) and (Ma) added together must not total more than (d).

    Thanks again. This is my first time using Mathematica so I'm still just figuring out the basics.


    Attached Files:

  6. Nov 26, 2011 #5
    Ok, that's better.

    1: Some Mathematica users believe "Assuming" has more power than it often has. Assuming is only a hint that is used by some functions in Mathematica and ignored by other other functions. So don't assume that Assuming even has any common sense. In particular that often doesn't force Solve to throw away complex solutions just because you tell Assuming that the variable of interest is in the Reals.

    2: Mathematica uses semicolons differently from almost all other programming languages and it is completely intolerant of different uses. So what you did was Assuming[d>0;t>0...,Solve[]]. That needs to be Assuming[d>0&&t>0&&...,Solve[]]. I don't remember at the moment, but you might be able to also use Assuming[{d>0,t>0...},Solve[]]. But you cannot use semicolons the way you did.

    3. Let's make more sense of your problem. You have a complicated expression you are trying to solve with lots of common terms and fractions. Can this be any simpler and thus let us see what is going on?

    Expand[((Vs + Vi)/2)*((Vs - Vi)/(1/2*Vs^2/Ma)) + ((Vs/2)*(-Vs/(1/2*Vs^2/Md))) + (Vs*(t - (-Vs/(1/2*Vs^2/Md)) - ((Vs - Vi)/(1/2*Vs^2/Ma))))]

    gives me

    -Ma + Md - (Ma*Vi^2)/Vs^2 + (2*Ma*Vi)/Vs + t*Vs

    Ah, much simpler and more understandable. You want to Solve for Vs, you have that squared in a denominator and not squared in a numerator. That whispers "cubic equation in Vs" to me. That explains why you are getting three solutions, every cubic has three of those.

    Vs/.Solve[d==-Ma + Md - (Ma*Vi^2)/Vs^2 + (2*Ma*Vi)/Vs + t*Vs,Vs]

    gives me the three solutions. Then

    %/. {d -> 10000, t -> 20, Vi -> 20, Ma -> 1000, Md -> -1000}

    gives me the three solutions with your parameters. Then


    gives me decimal approximations. Then


    gives me the decimal approximations with tiny roundoff complex parts trimmed.

    And the real answer is 596.704. The other two are complex conjugates you don't want.

    Does this explain it?
  7. Nov 26, 2011 #6
    Thank you so much, that clears up quite a lot. The formula I need is much cleaner now and I've got a better handle on how to input what I want. I do have one more problem...perhaps you might have some advice on.

    I need to write up the correct formula in a programming language that doesn't have provisions for imaginary numbers, which are used by the correct formula. Is there an alternate method for describing the "i" imaginary number mathematically that I might use to write up the formula?

    Thanks much,

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Mathematica output seems incorrect