I want to solve an equation and substitute it into a Plot expression. My code looks something like this

a[b_, x_] := bx + 1;
sol = Solve[(1 + b)x + 1 == 0, b]

I want to use the solution of b and substitute it to get the value of a and then plot it. I expect the result to be -x and I expected to see a plot of y = -x.

Plot[a[b /. sol, x], {x, 1, 1000}]

However, I don't get anything. Can anyone please help?

  • 8
    $\begingroup$ You're missing a space between b and x in the definition of a. Works fine if you fix that. $\endgroup$ – Eric Thewalt Mar 21 '13 at 7:23
  • $\begingroup$ Is the code you gave a minimized example? If so, was the above the problem with your actual code as well? $\endgroup$ – Eric Thewalt Mar 21 '13 at 10:13
  • $\begingroup$ Yes it is the minimized example. I tried to make it as simple as possible. Yes, it is working in my real code. Thx $\endgroup$ – Martin Wijaya Mar 21 '13 at 11:07

The problem here is simply that Mathematica sees bx as a (new, undefined) variable and not as the product of b and x. If we switch to a[b_, x_] := b x + 1; the plot appears as intended.

Although it doesn't cause any trouble in this case, it's important to be aware of the output of the functions you're using. For instance, in order to allow for multiple multivariate solutions, Solve[] outputs a list of lists of Rules. This leads to the following behaviour:

a[b /. sol, x]

(* ==> {-x} *)

We can perform algebra with (and plot) lists like this just fine, but in more complicated array-based work you might end up with a hard-to-track-down bug.


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.