4
$\begingroup$

This code makes my computer crash, every time I run it.

Manipulate[
    g[x_] := Piecewise[{{2 - x, 1 <= x}, {x, x < 1}}];
    Solve[g[x] == g[m x], x],
    {{m, 2, "Times"}, -10, 10, 0.1}
]

What's wrong with this? After a time, I receive this error message: enter image description here

I use Mathematica 12.0

If I click Continue Waiting, it will end up crashing.

$\endgroup$
2
  • $\begingroup$ When you say your "Computer" crashes do you really mean that? As in the OS crashes and you have to reboot? $\endgroup$
    – Michael
    Commented Mar 10, 2020 at 21:06
  • $\begingroup$ @Michael cpu and memory is so demanding, it cost almost all resources. Finally OS crashes. $\endgroup$
    – kile
    Commented Mar 11, 2020 at 9:19

1 Answer 1

11
$\begingroup$

I'm not exactly sure what's causing the problem, but you generally want to avoid re-defining functions inside of dynamics like these. Every time the manipulate re-calculates the result, it redefines g and this seems to cause some sort of infinite update loop. It's better to do this:

Manipulate[
 Solve[g[x] == g[m x], x],
 {{m, 2, "Times"}, -10, 10, 0.1},
 Initialization :> (
   g[x_] := Piecewise[{{2 - x, 1 <= x}, {x, x < 1}}]
   )
 ]

edit

It seems like the problem persists for the OP, so here's 2 more suggestions. First of all, try using the TrackedSymbols option:

Manipulate[
 Solve[g[x] == g[m x], x],
 {{m, 2, "Times"}, -10, 10, 0.1}, 
 Initialization :> (g[x_] := Piecewise[{{2 - x, 1 <= x}, {x, x < 1}}]),
 TrackedSymbols :> {m}
]

Failing that, another good trick it to encapsulate the output in Dynamic with a TrackedSymbols option:

Manipulate[
 Dynamic[
  Solve[g[x] == g[m x], x],
  TrackedSymbols :> {m}
 ],
 {{m, 2, "Times"}, -10, 10, 0.1}, 
 Initialization :> (g[x_] := Piecewise[{{2 - x, 1 <= x}, {x, x < 1}}])
]

If that doesn't work, I suspect there might be a hardware issue or a problem with your Mathematica installation.

$\endgroup$
5
  • $\begingroup$ The message still keeps popping up even I used yours instead $\endgroup$
    – kile
    Commented Mar 11, 2020 at 9:29
  • $\begingroup$ Hmmm, strange. What version are you using? I'll add another suggestion to the answer. $\endgroup$ Commented Mar 11, 2020 at 9:31
  • $\begingroup$ But it works like a charm after I reset steps du from 0.1 to 1/10. Anyway, thank you very much. $\endgroup$
    – kile
    Commented Mar 11, 2020 at 9:37
  • $\begingroup$ "But it works like a charm after I reset steps du from 0.1 to 1/10." That honestly makes no sense to me. If anything, I'd expect the reverse to work. Oh well... $\endgroup$ Commented Mar 11, 2020 at 9:41
  • $\begingroup$ Everything works great after you edit your answer. $\endgroup$
    – kile
    Commented Mar 11, 2020 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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