0
$\begingroup$

I have an expression assigned to a variable. For example:

exp1 = a*x+b

Now I would like to plot it using something like this

Manipulate[
 Plot[Evaluate[ex99], {x, 0, 1}],
 {a, 0, 1},
 {b, 0, 1}]

I can see 'a' and 'b' being properly substituted by 'Evaluate', but Plot does not work as expected. I suspect it has something to do with 'x' substitution. How can I fix that? Thanks!

$\endgroup$
3
  • $\begingroup$ Did you try Manipulate[Plot[a*x + b, {x, 0, 1}], {a, 0, 1}, {b, 0, 1}] ? $\endgroup$
    – Rod
    Commented May 20, 2013 at 0:21
  • $\begingroup$ You could also try Manipulate[Plot[a*x+b,{x,0,1},AxesOrigin->{0, 0}],{a,-1,1},{b,-1,1}] in order to better see the results... $\endgroup$
    – Rod
    Commented May 20, 2013 at 0:24
  • $\begingroup$ Even better IMO: Manipulate[Plot[a*x+b,{x,0,1},AxesOrigin->{0,0},PlotRange->{{0,1},{-2,2}}], {a,-1,1},{b,-1,1}] $\endgroup$
    – Rod
    Commented May 20, 2013 at 0:29

3 Answers 3

2
$\begingroup$

Another alternative is to define the function using set delayed.

ex99[a_, b_, x_] := a*x + b;
Manipulate[Plot[ex99[a, b, x], {x, 0, 1}], {a, 0, 1}, {b, 0, 1}]

Now it works fine.

$\endgroup$
1
$\begingroup$

You could try this:

Manipulate[Plot[a*x+b,{x,0,1},AxesOrigin->{0,0},PlotRange->{{0,1},{-2,2}}], {a,-1,1},{b,-1,1}]
$\endgroup$
2
  • $\begingroup$ Of course in this simple example I can copy and paste the expression. In real world my exp1 is result of Integrate and quite complex. $\endgroup$
    – krokodil
    Commented May 20, 2013 at 4:46
  • $\begingroup$ When you said "Plot does not work as expected" I thought you we're interested in showing how $a$ and $b$ would influence your plot... $\endgroup$
    – Rod
    Commented May 20, 2013 at 9:58
1
$\begingroup$

Using CompoundExpression (a.k.a ";"), you can include expressions within Manipulate:

Manipulate[
     ex99 = a x + b;
     Plot[ex99, {x, 0, 1}],
     {a, 0, 1},
     {b, 0, 1}
   ]

The semicolon ";" is postfix notation for the CompoundExpression command, which has amongst other things the effect of evaluating the command in the kernel without producing output in the front end.

$\endgroup$

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