4

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.

Red Banana
  • 5,329
  • 2
  • 29
  • 47

3 Answers3

21

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 5
    I always thought you would recommend (x Range@9)~(Plot[#1, #2, Evaluated -> True] &)~{x, 1, 10} :D – Dr. belisarius Jul 03 '12 at 02:54
  • @belisarius no, of course not! ... it doesn't localize x. ;^p – Mr.Wizard Jul 03 '12 at 03:17
  • 1
    I don't understand the meaning of "correctly localized". – Red Banana Jul 03 '12 at 06:29
  • 12
    @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] – Mr.Wizard Jul 03 '12 at 06:34
9

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

Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355
  • 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. – Red Banana Jul 03 '12 at 01:53
  • 4
    Leonid Shifrin :) – Andreas Lauschke Jul 03 '12 at 01:57
  • 2
    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 ;-) – Yves Klett Jul 03 '12 at 12:46
5

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

Brett Champion
  • 20,779
  • 2
  • 64
  • 121