1
$\begingroup$

I want to plot the function $y=2^x$ at the $x$-values given by Table[Prime[n], {n,20}]

How should I write the plot function? Like so?

list = Table[Prime[n], {n,20}]
Plot[y = 2^x, Evaluate[list]]
$\endgroup$
  • $\begingroup$ ListPlot. To connect the dots: Joined->True or ListLinePlot. $\endgroup$ – swish May 10 '13 at 2:03
  • $\begingroup$ I'm sorry. Maybe I didn't make myself clear. I'd like to pass the result of a Table[] as the Plot[] range "{x,min, max}" $\endgroup$ – Ehsan Irannejad May 10 '13 at 2:11
  • $\begingroup$ How can ListPlot be used to solve this? $\endgroup$ – Ehsan Irannejad May 10 '13 at 2:12
  • $\begingroup$ I think you need to look at the syntax structure for Plot. You simply need a function, and then the variable range in the form {x,xmin,xmax}. $\endgroup$ – Jonathan Shock May 10 '13 at 2:13
  • $\begingroup$ Ok. Then let me reword the question. How can I tell Mathematica to plot y=2^x for x =Table[Prime[n],{n,20}] $\endgroup$ – Ehsan Irannejad May 10 '13 at 2:15
4
$\begingroup$

All the solutions so far have plotted {n, 2^Prime[n]} for integer values of n, which means that the points will be evenly spaced along the horizontal axis. Here's how to do what was actually asked, plotting {x, 2^x} for prime values of x.

Since 2^x grows so quickly, I'll demonstrate instead with Sqrt[x] so that it's easier to see the uneven distribution of primes along the horizontal axis.

  • Using ListPlot, you want to specify the horizontal position using {x,y} pairs, rather than just a list of heights:

    primes = Table[Prime[n], {n, 20}];
    ListPlot[Table[{x, Sqrt[x]}, {x, primes}]]
    

    enter image description here

  • Using DiscretePlot, you want to provide the horizontal positions using the {x, {x1, x2, ..., xn}} variable specification:

    primes = Table[Prime[n], {n, 20}];
    DiscretePlot[Sqrt[x], {x, primes}]
    

    enter image description here

$\endgroup$
6
$\begingroup$

Alternatively, you can use a function dedicated to plotting discrete data:

DiscretePlot[2^Prime[n], {n, 1, 7}, Filling -> None, Frame -> True, Joined -> True]

exponential at prime values

$\endgroup$
3
$\begingroup$
x = Table[Prime[n], {n, 20}];
y = 2^x;
ListLinePlot[y]
$\endgroup$
  • $\begingroup$ OMG!! you're the god and I really need to learn the mathematica basics here!! Thank you a lot!! $\endgroup$ – Ehsan Irannejad May 10 '13 at 2:17
  • $\begingroup$ No problem. You can start from here and here. $\endgroup$ – swish May 10 '13 at 2:24
  • $\begingroup$ Awesome!! Thanks a bunch! $\endgroup$ – Ehsan Irannejad May 10 '13 at 2:25
3
$\begingroup$

Exponential functions increase too fast:

ListPlot[2^Table[Prime[n], {n, 20}], Joined -> True]

exponential at prime values

Instead, it is better to work with ListLogPlot (plotting a given function in the logarithmic scale) or just DiscretePlot of the Log :

GraphicsRow[
    { ListLogPlot[2^Table[Prime[n], {n, 20}], Joined -> True, PlotStyle -> Thick], 
      DiscretePlot[Log[2^Prime[n]], {n, 20},  PlotMarkers -> {Automatic, Medium}]}]

plots of logarithms

$\endgroup$
  • $\begingroup$ Thank you very much for the explanation. $\endgroup$ – Ehsan Irannejad May 10 '13 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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