I can't figure how to replace a failed evaluation with a null value.

I am very new to Mathematica.

I have an equation that are being processed using the Solve[] function and I then need to forward the results to other steps in my process. Some of the equations don't have solutions. This is desired and I want to eliminate those possibilities.

After generating a list with all the solutions I process that list like this:

SillySyntax[Rule_] = Quiet[x /. Rule];
Quiet[Curve = Table[SillySyntax[Curves[[i]]], {i, 5}]]

This works fine, but sometimes when errors such as the following happen they break my workflow by not putting a null in there. Instead leaving a ghost of an equation. This is not desired because I know a priori that these errors mean that the system is not physical. How can I remove these aberrations and ensure that SillySyntax will output only numbers or empties.

Greater::nord: "Invalid comparison with 0.347148 -1.5103810834566311` I"
  • $\begingroup$ could you give an example of the output of Solve and these "ghost"-functions? $\endgroup$ – mincos Dec 4 '12 at 8:56
  • 1
    $\begingroup$ Try Quiet in combination with Check $\endgroup$ – kglr Dec 4 '12 at 9:00

I'm pretty sure your error message comes from something different. Anyway, what happens if you use for instance such a solve function?

solveMe[z_] := 
  {solution = Solve[x^2 + y^2 == z^2 && y > x > 0, {x, y}, Integers]},
  If[solution =!= {},
    {x, y} /. solution,

If the solution is empty, it just returns the {}. Otherwise, it extracts the numbers of the solution. Therefore:


Since you mentioned Quiet: If you want a function which returns something else when a message is generated, you can use Check.

f[e_] := Quiet[Check[1/e, "Hola seniorita"]]

  "Hola seniorita"

Here we catch the message that is generated when we divide by 0 and return a different result.

| improve this answer | |
  • $\begingroup$ The problem is that the solution is not empty, the Solve[] routine fails, with some error I don't want to deal with. I will try this and let you know, thanks. $\endgroup$ – Mikhail Dec 4 '12 at 9:03
  • $\begingroup$ @Mikhail The Check will help you. Btw, you never want to use Rule as variable name! It's an important built-in function! $\endgroup$ – halirutan Dec 4 '12 at 9:05
  • 1
    $\begingroup$ I know halirutan wanted to show the empty solutions in this case, but in the normal case, where only the actual solutions are wanted, I remind readers that the empty solutions can be removed from the output of Table[...] by prefixing Table[...] with Join@@. $\endgroup$ – m_goldberg Dec 4 '12 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.