0
$\begingroup$

I have plotted the following function using the following code:

integers = Table[Integer[n], {n, 20}];
ListPlot[Table[{x, 
x^(-1)*Sum[((6.674*(10^(-11))*(10^17) (4))/((100^2 - 
       400 (2 Cos[(2 Pi k)/x] - 3 Sin[(2 Pi k)/x]) + 
       29)^1.5)), {k, 0, x - 1}]}, {x, integers}]]

The function oscillated because it takes values that are not integers. I have searched around and the above does not work. I'm new to mathematica. How can I change the code to make it only use integer values of x?

$\endgroup$
6
  • $\begingroup$ Try it with integers = Range[20] . $\endgroup$
    – LouisB
    Sep 21, 2017 at 3:50
  • $\begingroup$ thanks, another question-where do I put this: AxesOrigin -> {0, 0} $\endgroup$
    – fidafa123
    Sep 21, 2017 at 3:55
  • $\begingroup$ @OmkarVaidya at the end of ListPlot after a comma: ListPlot[...., here] $\endgroup$
    – Alucard
    Sep 21, 2017 at 3:57
  • $\begingroup$ @Alucard what if I want to change the range of the function? Perhaps from 3 to 100? $\endgroup$
    – fidafa123
    Sep 21, 2017 at 4:08
  • $\begingroup$ @OmkarVaidya do you mean the points or the range of the listplot? in the first case write Range[3,100], otherwise add PlotRange->{{yourxmin,yourxmax},{yourymin,yourymax}} after AxesOrigin. don't forget the comma between the 2 options. $\endgroup$
    – Alucard
    Sep 21, 2017 at 4:40

2 Answers 2

3
$\begingroup$

You can useDiscretePlot:

f[x_] := Sum[((6.674*(10^(-11))*(10^17) (4))/((100^2 - 
         400 (2 Cos[(2 Pi k)/x] - 3 Sin[(2 Pi k)/x]) + 29)^1.5)), {k, 
    0, x - 1}]/x
DiscretePlot[f[n], {n, 1, 20}, PlotRange -> Full]

enter image description here

$\endgroup$
2
  • $\begingroup$ Note that the original post has a factor of x^(-1) in front of the Sum. $\endgroup$
    – LouisB
    Sep 21, 2017 at 9:34
  • $\begingroup$ @LouisB thank you. I missed that. I guess it still illustrates use of DiscretePlot. Will change if/when I get time. $\endgroup$
    – ubpdqn
    Sep 21, 2017 at 9:36
2
$\begingroup$

There is a lot to be said for making code easier to read. In this case we can write

data = Table[{x, x^(-1)*Sum[((6.674*(10^(-11))*(10^17) (4))/((100^2 - 
       400 (2 Cos[(2 Pi k)/x] - 3 Sin[(2 Pi k)/x]) + 
       29)^1.5)), {k, 0, x - 1}]}, {x, 20}];

ListPlot[data]
ListPlot[data, AxesOrigin->{0,25}]

Notice that we do not define integers because Table takes care of that for us. One advantage of the above is that we can examine our data with

data // Dataset

and we can add options to our ListPlot to build up the command we want. The optional arguments are added in any order after the required arguments. They always have the form of rules, like PlotRange->{All,{25,30}}

We can see the coordinates of a plotted data point by using ListPlot[Tooltip[data]] as the basic command and hovering the mouse pointer over the point whose coordinates we want.

$\endgroup$
3
  • $\begingroup$ I also wanted to know how to check the coordinates of a point on the graph. I used the drawing tool, but the y-values of the points are too close to see a difference. $\endgroup$
    – fidafa123
    Sep 21, 2017 at 4:25
  • $\begingroup$ @OmkarVaidya I add a paragraph that shows a basic example of how to display the coordinates of a plot data point. If you search this site for "Tooltip" you will see some clever examples of how to use this command. $\endgroup$
    – LouisB
    Sep 21, 2017 at 5:02
  • $\begingroup$ Thanks for that! The problem is that it only shows 4 decimal places, and all the points show the same value (27.1063). How can i change this? $\endgroup$
    – fidafa123
    Sep 21, 2017 at 5:42

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.