Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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"
share|improve this question
    
could you give an example of the output of Solve and these "ghost"-functions? –  mincos Dec 4 '12 at 8:56
1  
Try Quiet in combination with Check –  kguler Dec 4 '12 at 9:00

1 Answer 1

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_] := 
 Module[
  {solution = Solve[x^2 + y^2 == z^2 && y > x > 0, {x, y}, Integers]},
  If[solution =!= {},
    {x, y} /. solution,
    solution
  ]
]

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

Table[solveMe[z],{z,30}]
(*
{{},{},{},{},{{3,4}},{},{},{},{},{{6,8}},{},{},{{5,12}},{},{{9,12}},
{},{{8,15}},{},{},{{12,16}},{},{},{},{},{{7,24},{15,20}},{{10,24}},{},
{},{{20,21}},{{18,24}}}
*)

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"]]
f[1]
f[0]

(*
  1
  "Hola seniorita"
*)

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

share|improve this answer
    
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. –  Mikhail Dec 4 '12 at 9:03
    
@Mikhail The Check will help you. Btw, you never want to use Rule as variable name! It's an important built-in function! –  halirutan Dec 4 '12 at 9:05
    
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@@. –  m_goldberg Dec 4 '12 at 15:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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