% Sensor (non-)linearity plot
%
% v0.1 - 2013/09/17
% Developed and tested with Matlab 7.11.0 (R2010b)
%
% Sander Paalvast
% Janssen Precision Engineering
% Azielaan 12
% 6199AG  Maastricht-Airport NL
%
% VERSIONS
% VER           RELEASE DATE    COMMENT
% v1.0 	        2013/Sep/10     Creation

%% -- INIT --
close all                       % Close all figures
clear all                       % Clear memory
clc                             % Clear command window

%% -- PARAMETERS --
xmin = -1;
xmax = 1;
dx = 0.1;
P = [0.3 -0.1 0.8 0.05];                  % Sensor polynomial

%% -- DATA --
x = xmin:dx:xmax;               % Position
y = polyval(P,x);               % Sensor output
Pl = polyfit(x,y,1);            % Fit sensor output
l = polyval(Pl,x);              % Linear fit
e = y-l;                        % Fit error
emin = min(e);                  % Minimal error
emax = max(e);                  % Maximal error


%% -- PLOT --
figure('Position',[-1080 700 500 200])
plot(x,[y; l],'linewidth',2);
hold on;
plot([xmin xmax],[l(1) l(end)]+emin,'k--','linewidth',2);
plot([xmin xmax],[l(1) l(end)]+emax,'k--','linewidth',2);
grid on;
axis([-1 1 -1.2 1.2]);
legend
set(gca,'xtick',-1:0.5:1);
set(gca,'ytick',-1:0.5:1);
xlabel('Position [mm]');
ylabel('Sensor output [unit]');
legend('Signal','Linear fit' ,'Non-linearity' ,'Location','NorthWest');
