4
$\begingroup$

I want to plot series of slopes:

x, 2x, 3x, 4x, 5x, 6x...

I'm trying to do this:

a = Range[10]; c = 0; Clear[b]
f := ToExpression[ToString[b] <> ToString[a[[c = c + 1]]]]

So everytime I input f, it gives me:

b1
b2
b3
b4
...

Now I want to assign every bn to one of the plots:

b1=Plot[x,{x,1,10}]
b2=Plot[2x,{x,1,10}]
b3=Plot[3x,{x,1,10}]
b4=Plot[4x,{x,1,10}]
...

I've tried to do this:

f=Plot[x,{x,1,10}]

But It will only switch the variable from the first fuction to the Plot function. How can I do to the fbecome b1 first and then the b1 be assigned to the Plot function? I guess it has something to do with evaluation control, something like that.

NOTE:. The ? in the title means that I'm not very sure If it's about evaluation control.

$\endgroup$

3 Answers 3

9
$\begingroup$

I thought a minimal code can make a nice visualization of it:

Manipulate[
 Plot[Evaluate[{Range[n] x, n x}], {x, -2, 10}, PlotStyle -> Thick, 
  PlotRange -> {{-2, 10}, {-20, 50}}, Filling -> 0]
 , {{n, 0, "slope"}, 0, 10, Appearance -> "Labeled"}]

enter image description here

If on the other hand you would like to plot all slopes separately, this could do:

GraphicsGrid[Partition[Plot[# x, {x, -2, 5}, Frame -> True, 
     AxesStyle -> Directive[Gray, Thick], PlotStyle -> Thick, 
     PlotRange -> {{-2, 5}, {-30, 80}}, 
     PlotLabel -> "Slope = " <> ToString[#]] & /@ Range[16], 4], 
 ImageSize -> 900]

enter image description here

$\endgroup$
3
  • $\begingroup$ Oh, It's because I'm still a little afraid of using the #'s and && and all that pattern stuff. I'm reading Leonard Shinfrin's book now. $\endgroup$
    – Red Banana
    Jul 3, 2012 at 1:53
  • 4
    $\begingroup$ Leonid Shifrin :) $\endgroup$ Jul 3, 2012 at 1:57
  • 2
    $\begingroup$ Although Leonard evokes associations with actors that play pointy-eared highly competent aliens, which somehow resonates (in a very positive, awe-inspiring way) with my impression of Leonid ;-) $\endgroup$
    – Yves Klett
    Jul 3, 2012 at 12:46
21
$\begingroup$

Once again I shall recommend a different form for evaluating the first argument of Plot:

Plot[x Range[9], {x, 1, 10}, Evaluated -> True]

Mathematica graphics

Though the option Evaluated is undocumented it is superior because it works even if x has a global value, whereas Plot[Evaluated[ . . . ], . . .] will not. That is, the Plot variable is correctly localized.

$\endgroup$
4
  • 5
    $\begingroup$ I always thought you would recommend (x Range@9)~(Plot[#1, #2, Evaluated -> True] &)~{x, 1, 10} :D $\endgroup$ Jul 3, 2012 at 2:54
  • $\begingroup$ @belisarius no, of course not! ... it doesn't localize x. ;^p $\endgroup$
    – Mr.Wizard
    Jul 3, 2012 at 3:17
  • 1
    $\begingroup$ I don't understand the meaning of "correctly localized". $\endgroup$
    – Red Banana
    Jul 3, 2012 at 6:29
  • 12
    $\begingroup$ @Gustavo when you enter something like x = 7; Plot[Sin[x], {x, 0, 2 Pi}] it still works because x is localized by Plot. If you use Evaluate this breaks; e.g. x = 7; Plot[Evaluate@Sin[x], {x, 0, 2 Pi}] you get a single horizontal line because you are effectively doing Plot[Sin[7], {x, 0, 2 Pi}]. On the other hand, Evaluated -> True localizes x before evaluation: x = 7; Plot[Sin[x], {x, 0, 2 Pi}, Evaluated -> True] $\endgroup$
    – Mr.Wizard
    Jul 3, 2012 at 6:34
5
$\begingroup$

It's slightly simpler if you're willing to use b[1] instead of b1:

Do[b[i] = Plot[i x, {x, 1, 10}],{i, 10}]

b[3]

enter image description here

If you're really keen on using b1, you can do something like:

Do[
   Set[Evaluate[Symbol["b" <> ToString[i]]], 
       Plot[i x, {x, 1, 10}]], 
   {i, 10}]

b3

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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