Substituting solution from Solve into the equation and making a plot

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

 

You’re missing a space between b and x in the definition of a. Works fine if you fix that.
– Eric Thewalt
Mar 21 ’13 at 7:23

  

 

Is the code you gave a minimized example? If so, was the above the problem with your actual code as well?
– Eric Thewalt
Mar 21 ’13 at 10:13

  

 

Yes it is the minimized example. I tried to make it as simple as possible. Yes, it is working in my real code. Thx
– Martin Wijaya
Mar 21 ’13 at 11:07

=================

1 Answer
1

=================

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.