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.

when I input pp = {{2.0, 10}, {4, 19}, {6, 31}, {8, 40}, {10, 51}, {12, 59}, {14, 70}} and press enter I get a matrix output. I want it as an ordered pair to find the best fit line, using Fit[pp,{1,x}x], which it does but when I plot the line and the try to plot the points mathematica interprets my data as a matrix and does not plot it. Why? Before I did this same method and was able to plot both the points and the line on the same graph. Please help anyone.

share|improve this question

closed as off-topic by rm -rf Sep 16 '13 at 14:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – rm -rf
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Have a look at the docs for ListPlot. For example: ListPlot[pp]. –  bill s Sep 16 '13 at 3:29
    
Maybe Plot[Evaluate@Fit[#, {1, x}, x], {x, 0, 14}, Epilog -> {Red, PointSize[Large], Point[#]}, AxesOrigin -> {0, 0}] &@pp –  TomD Sep 16 '13 at 14:19

2 Answers 2

It sounds like you may be having an issue trying to plot the discrete data, pp with a continuous function, i.e. what is returned from Fit. You may consider picking a different title for your post because in Mathematica an ordered pair is always interpreted as a matrix.

First define the data set and extract the max and min x-values to make plotting easier:

pp = {{2.0, 10}, {4, 19}, {6, 31}, {8, 40}, {10, 51}, {12, 59}, {14, 70}};
x0 = Min[pp[[All, 1]]];
xf = Max[pp[[All, 1]]];

Next call Fit on your data to generate a linear fit. Note that in this way we are forced to plot line using the dummy variable x:

line = Fit[pp, {1, x}, x];

Plot the discrete data using ListPlot and the continuous function using Plot. Store the outputs in p1 and p2 respectively such that we can use Show to display them together:

p1 = ListPlot[pp];
p2 = Plot[line, {x, x0, xf}];
Show[p1, p2]

Which produces a plot of your data along with the linear fit.

share|improve this answer
    
Thank you. This is what I get: –  Ali Bautista Sep 16 '13 at 3:51
    
Show::gcomb: Could not combine the graphics objects in Show[Listplot((7x2 matrix)).... –  Ali Bautista Sep 16 '13 at 3:56
    
I understand that mathematica interprets my data as a matrix and not as a pair of points to plot. That is my problem. –  Ali Bautista Sep 16 '13 at 3:56
    
When I input pp = {{2.0, 10}, {4, 19}, {6, 31}, {8, 40}, {10, 51}, {12, 59}, {14, 70}} and press enter a 7x2 matrix form is returned. That is what I do not want. –  Ali Bautista Sep 16 '13 at 4:04
    
Thank you. You were right, my mistake was a typo. Thank you. –  Ali Bautista Sep 16 '13 at 4:10

This should achieve your goal:

f = Fit[pp, {1, x}, x]
Show[ListPlot[pp], Plot[f, {x, 2, 14}]]

If you need model data use LinearModelFit

share|improve this answer
    
the fit is not the issue it is the plotting of the line and the points. The function is generated, no problem but mathematica does not plot the points because it seems to have already defined my points as a 7x2 matrix, which I do not want it to do.Don't know if you understand what I am saying. –  Ali Bautista Sep 16 '13 at 4:02
    
Did you run the above code. The points and the fit are displayed. I apologise but do not understand where the problem arises. –  ubpdqn Sep 16 '13 at 4:10

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