# 15.4: Sketch of how to present data in laboratory (i.e. graph with errors)


This is notes (yes it is actual just a bunch of random notes) on how to present data for your laboratory report. This is just one example and the student is encouraged to investigate how to present data beyond this (there are many examples on the web).

The main idea of this lab is to take data at different intervals (of length, time, speed, or something else that can be done in intervals). Your could image that this would give you some sort of line or curve on an xy graph, but for an experiment it is desirable (and might even be demanded) to have error associated with your data.

So how do we get a numerical error? One possibility and the method normally used in academic exercises is to take data multiple times for each interval. From this you can calculate a mean and a standard deviation (square root of the variance). This method of calculating error is in general conservative and should suffice for simple lab experiments.

Here is an example (in octave style) for calculating the mean and standard deviation:

% Ok, let us say we have some measured data y at x.

%
% The data is x which is a distance (for example) that we will assume is errorless (not completely correct,
% but good enough here). The test data is y which is time (for example). We will repeat the experiment at
% the same distance at least five times in order to get a good representative
% value (the mean). Associated with this value will be an error (standard deviation).
%

%
% So at distance 1 feet we measured times of 2.200 sec, 2.000 sec, 2.400 sec, 1.600 sec, 1.065 sec,
% 1.800 sec,and 2.935 sec. %

y1 = [2.200,2.000,2.400,1.600,1.065,1.800,2.935];
mean(y1)
% ans = 2
std(y1)
% ans = 0.59839 -- which for clarity purposes I used as 0.6 for sy.

%
% So at distance 2 feet we measured times of 4.0500 sec, 4.000 sec, 4.1500 sec, 3.6900 sec, 3.8500 sec,
% 3.9500 and 4.3100 sec. %

y2 = [4.0500,4.0000,4.1500,3.6900,3.8500,3.9500,4.3100];
mean(y2)
% ans = 4
std(y2)
% ans = 0.200991 -- which for clarity purposes I used as 0.2 for sy.
%
% Repeat for all the other x values...how many x's you use will depend on what type of fit you wish
% to achieve. For a linear fit you need at least 3 values of x, for a quadratic fit
% you need at least 4 values of x, for a cubic fit you need at least 5 values of x, and for
% a quartic at least 6 values of x, and so on. Obviously more data points would always be preferable
% to get the best fit possible. This assumes polynomial fits, if it is an exponential fit (or sinusoidal)
% then you should have even more values of x.
%
% x = [1,2,{other values}];
% y = [2,4,{other values}];
% sy =[0.6,0.2,{other values}];
%

On paper you would write the first data point calculated above as $$y_1 = 2 \pm 0.6$$

Assuming we have already calculate the mean and standard deviation of a lab with five data points, here is the presentation of the data for you to run.

%
% Script
% Author: Dr. Scott D. Johnson
% Date: 8/2008
% The data is x,y, and sy ("error" -- that is the standard deviation)
%
x = [1,2,3,4,5,6];
y = [2,4,6,8,9,12];
sy =[0.6,0.2,0.4,0.3,0.5,0.4];
%
% Now to plot this data we should use the errorbar function (note this function is slightly different
% in MATLAB, but it should work similarly).
%
h1 = errorbar(x,y,sy);
%
% The above statement is good for most older Octaves, but the recent version
% of Octaves have the annoying "feature" of connected the lines of the
% error bars (something that no researcher would ever want -- after all,
% then why have error bars?). The FMT should handle this, but errobar()
% doesn't use the FMT correctly, so the following two lines will fix this.
%
set(h1,"marker","+");
set(h1,"linestyle","none");

%
% Of course this doesn't work unless you returned handle h1
%
%
% wpolyfit will be used which is a special optional package (optim) for Octave.
% wpolyfit allows for the error to be used as a weight. There are better ways to do this, but for this
% course, this is sufficient. The function wpolyfit is similar to polyfit where a vector is returned
% with the coefficients of the polynomial and a structure is returned that includes the Cholesky
% factors of the Vandermonde matrix plus other information that is used to create the errorbar on the fit
% (as opposed to the error on the data itself).
%
[p,s] = wpolyfit(x,y,sy,1);
%
% polyconf is used to get the values of y and sy for the MODEL (not the data).
% ci is confidence interval method which is
% appropriate for error bars in the physical sciences.
%
[yn,syn] = polyconf(p,x,s,'ci');
%
% Now we continue the graphing. Plotting the model over the data. And making the graph pretty.
% You can do different things here depending on YOUR data.
%
hold on;
plot(x,y,'b+;The data;');
plot(x,yn,'r.-;Linear model;');
xlabel('X-data (Size of the {\alpha} tiger)');
ylabel('Experiment data with error {\sigma}');
title('x versus y showing that y is linearly dependent on x');
%
% Return handle h2 so you can change the color of the model errorbars, to be different then from
% the data errorbars
%
h2=errorbar(x,yn,syn);
set(h2,"color","red");
set(h2,"marker","+");
%
% Let's neaten up a bit
%
axis([0,7,0,15]);

Finally we have a graph with the data, the error on the data, the model of the data (linear fit in this case), and the error on the model data (note the error bar is different for different model data points just like for different points of the actual data).

You will note (if you run the octave program above) that the error bars on the model (the fitted line) have different values (which they should have though most algorithms do not do that). If you believe your model is a polynomial curve then you should change the 1 to the degree polynomial you believe would best fit your data in wpolyfit (which is from the optim package which if you have octave you may have to go get). In the help for wpolyfit is a description of how to use a chi-squared test to see which polynomial fit might be better.

From the data that wpolyfit and polyconf provide an equation for the line (or polynomial) with error can be formulated to be part of your laboratory report.

15.4: Sketch of how to present data in laboratory (i.e. graph with errors) is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.