Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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!

share|improve this question

marked as duplicate by Ajasja, Sjoerd C. de Vries, ssch, Artes, Simon Woods May 20 '13 at 12:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Did you try Manipulate[Plot[a*x + b, {x, 0, 1}], {a, 0, 1}, {b, 0, 1}] ? –  Rod May 20 '13 at 0:21
    
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... –  Rod May 20 '13 at 0:24
    
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}] –  Rod May 20 '13 at 0:29

3 Answers 3

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.

share|improve this answer

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}]
share|improve this answer
    
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. –  krokodil May 20 '13 at 4:46
    
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... –  Rod May 20 '13 at 9:58

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.

share|improve this answer

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