Message Boards Message Boards

Plot a solution of Solve

Posted 5 years ago

Being relatively new, I do not understand the problem shown in the notebook attached and I do not find anywhere a solution: trying to plot a solution of "Solve". I either get no or different results. Any support appreciated.

POSTED BY: Andrej Pustisek
7 Replies
Posted 5 years ago

For the last plot you have to add brackets on (2a):

Manipulate[Plot[(-b - Sqrt[b^2 - 4 a c])/(2 a), {a, 1, 4}], {b, 1, 5}, {c, 1, 5}]
POSTED BY: Alan SAILLET
Posted 5 years ago

After a few tests, the following code works:

s2[a_, b_, c_] = x /. Solve[a x^2 + b x + c == 0, x][[1]]
Manipulate[Plot[s2[a, b, c], {a, 1, 4}], {b, 1, 5}, {c, 1, 5}]

Nevertheless, I am new on Mathematica, and I do not have the explanation

POSTED BY: Alan SAILLET

The explanation is that Manipulate only reacts to changes of variables that appear literally in its body expression (the first argument). But in

Manipulate[Plot[s[[1]],{a,1,4}],{b,1,5},{c,1,5}]

the variables b and c don't appear in Plot[s[[1]],{a,1,4}], so the display isn't updated when they change.

This is a bit of a gotcha and hence it's described as a Possible Issue in the Manipulate documentation.

So your suggestion of introducing a function s2[a_, b_, c_] is the right idea. You might just want to change its definition to

s2[a_, b_, c_] := Module[{x}, x /. Solve[a x^2 + b x + c == 0, x][[1]]]

i.e. (1) localize the variable x using Module and (2) use := (SetDelayed) to defer evaluation of the right-hand side until the function is actually called (which is usually what you want when defining functions). Your suggestions works just fine as-is, but there will be surprises if you ever redefine a, b, c or x globally. By localizing variables, your code remains self-contained and isolated from global changes. That being said, sometimes it can be beneficial to solve an equation symbolically ahead of time (deliberately relying on global symbols such as a, b, c) and substitute symbols for numeric values later.

POSTED BY: Jan Poeschko

Thank you - this was a stupid mistake

POSTED BY: Andrej Pustisek

Thank you very much for your help - it works!

POSTED BY: Andrej Pustisek

Thank you very much for your help - it works!

POSTED BY: Andrej Pustisek

This will also work.

Manipulate[Plot[(s[[1]] /. {a -> ap, b -> bm, c -> cm}), {ap, 1, 4}], {bm, 1, 5}, {cm, 1, 5}]

enter image description here

POSTED BY: Martijn Froeling
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract