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:
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...
You are fitting a straight line to exponential data, hence you must apply the
polyfit
on the logarithm of that data. Then applyexp()
on the result of polyval. In the end, also usesemilogy()
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.The result looks like this:
If you dont want to plot the original data, then just use
instead.