7
$\begingroup$

This is for a physics report that I have to hand in. I have a list of two-dimensional data points that approximately form a linear line.

I want to:

  1. calculate the best linear fit.
  2. put both data points and the linear fit line on a plot
  3. put names and units on x and y axis
  4. (if possible) put slope and intersection of best fit on plot

Is it possible to do this in Mathematica and if so how?

Example data:

Time, Displacement

    t (s)   d (m)
    0       0
    1       1.9
    2       3.8
    3       5.9
    4       8.1
    5       10.1
$\endgroup$
1
  • 8
    $\begingroup$ It's all possible. Have a look at Fit, ListPlot, Plot, Show, AxesLabel and Epilog. $\endgroup$
    – wxffles
    Commented Jul 30, 2012 at 20:16

3 Answers 3

13
$\begingroup$

Alternatively you might consider using LinearModelFit which allows for extraction of properties such as residuals, influence measures, etc.

data = {{0, 0}, {1, 1.9}, {2, 3.8}, {3, 5.9}, {4, 8.1}, {5, 10.1}};

lm = LinearModelFit[data, x, x];

Another small change from Mr. Wizard's solution is to add the graphics options to Show rather than the individual plots. I find this cleaner in some cases.

Show[ListPlot[data], Plot[lm[x], {x, 0, 5}], 
   AxesLabel -> {"x-name", "y-name"}, PlotLabel -> lm["BestFit"]
]

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ Another option is to use only a Plot and add the data points with Epilog->Point@data $\endgroup$ Commented Jul 30, 2012 at 21:28
  • 3
    $\begingroup$ @Simon Yes. I almost used that but I decided it was more advanced than necessary in answer to a beginner's question. $\endgroup$
    – Mr.Wizard
    Commented Jul 30, 2012 at 23:28
  • $\begingroup$ Thanks :) How can I fit with "y = ax" instead of "y = ax + b"? $\endgroup$ Commented Jul 31, 2012 at 21:29
  • $\begingroup$ @LucyBrennan check out the help for the option IncludeConstantBasis. $\endgroup$
    – Andy Ross
    Commented Jul 31, 2012 at 23:18
7
$\begingroup$

Basically:

data = {{0, 0}, {1, 1.9}, {2, 3.8}, {3, 5.9}, {4, 8.1}, {5, 10.1}};

Block[{x}, 
 f[x_] = Fit[data, {1, x}, x]
]

-0.119048 + 2.03429 x

Show[
 ListPlot[data, AxesLabel -> {"X data", "Y data"}], (* arbitrary labels *)
 Plot[f[x], {x, Min@#, Max@#}] &@data[[All, 1]]
]

Mathematica graphics

$\endgroup$
0
$\begingroup$

I hope that this will help. The Excel table has the columns titles. I use the following code:

Clear["`*"];
data = Import[SystemDialogInput["FileOpen"], "XLSX"][[1]];
dataDRO = Drop[data, {1}];
xlab = data[[1, 1]];
ylab = data[[1, 2]];
xcol = dataDRO[[All, 1]];
ycol = dataDRO[[All, 2]];

(* Linear model weighted *)
weight = 1/xcol^2;
model = LinearModelFit[dataDRO, x, x, Weights -> weight];
equation = Normal[model];
slope = Select[equation[[2]], # Epsilon Reals &, 1];
intercept = equation[[1]];
predicted = (ycol - intercept)/slope;

(* The table *)
accuracy = predicted*100/xcol;
qwerty = Transpose[Flatten /@ {xcol, ycol, predicted, accuracy}];
Grid[Prepend[qwerty, {xlab, ylab, "Predicted", "% Accuracy"}], 
 Alignment -> Right, Background -> None, 
 Dividers -> {All, {1 -> True, 2 -> True, -1 -> True}}]
Print["y = ", model["BestFit"]]
Print["R2 = ", model["RSquared"]]

(* The plot *)
Show[
 ListPlot[dataDRO, PlotStyle -> PointSize[0.01]],
 Plot[model["BestFit"], {x, 0, Max[xcol]}],
 Frame -> {{True, False}, {True, False}},
 FrameLabel -> {xlab, ylab},
 AspectRatio -> 1/GoldenRatio,
 ImageSize -> 500]
$\endgroup$
2
  • 2
    $\begingroup$ I see you've been posting not a few answers lately; you might want to consider registering, since that makes it more convenient for you to keep track of your answers and comments to them. $\endgroup$ Commented Oct 24, 2012 at 12:11
  • 2
    $\begingroup$ I'd also encourage you to learn Markdown (the markup language for the posts), especially the code formatting parts. $\endgroup$
    – rcollyer
    Commented Oct 24, 2012 at 12:20

Your Answer

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

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