Data extrapoliation, how to remove nonsense data?

157 views Asked by At

Consider some data in the following form

Sample = [1 2 3 4 5 6 7 8 9 10]';
Data = [10 10 10 10 8 5 2 1 0.6 0.4]';

Now I know for a fact this data should form a straight line when plotted with a semilog axis:

example plot

The problem is sample no. 1, 2, 3 and 4 all max out at 10 due to a limitation of an instrument used. So what I would like to do is fit a straight line to samples 5:10, and then use that to extrapolate back so I can estimate the values of 1, 2, 3 and 4.

I am confused as to how to do this in MATLAB, my attempt is as follows:

Sample = [1 2 3 4 5 6 7 8 9 10]';
Data = [10 10 10 10 8 5 2 1 0.6 0.4]';

Sample(1:4) = [];
Data(1:4) = [];

x = Sample;
y = Data;

p = polyfit(x,y,0);
x1 = Sample;
f1 = polyval(p,x1);

figure;
semilogy(Sample,Data,'ro');
hold on
plot(x1,f1,'b--')

which produces this plot...

plot2

1

There are 1 answers

1
Nras On BEST ANSWER

You are fitting a straight line to exponential data, hence you must apply the polyfit on the logarithm of that data. Then apply exp() on the result of polyval. In the end, also use semilogy() to add the line to the semilogy plot. Also I removed unneeded variables. I keep it so that you see all of the Data/Sample values, but the fit ignores the first 4 values, as you desired.

Sample = [1 2 3 4 5 6 7 8 9 10]';
Data = [10 10 10 10 8 5 2 1 0.6 0.4]';

% // Data for fit: indx 5 to end
x = Sample(5:end);
y = Data(5:end);

p = polyfit(x,log(y),1); % // fit of logarithmic data
f1 = exp(polyval(p,x)); % // apply exp() function

figure;
semilogy(Sample,Data,'ro');
hold on
semilogy(x,f1,'b--') % // also semilogy!

The result looks like this:

enter image description here

If you dont want to plot the original data, then just use

figure;
semilogy(x,y,'ro');
hold on
semilogy(x,f1,'b--') % // also semilogy!

instead.