Obtain equation for a curve of best fit

3 views (last 30 days)
stayfrosty
stayfrosty on 11 May 2016
Answered: dpb on 12 May 2016
I'm trying to use the Matlab function "fit" to obtain a curve of best fit for some experimental data. However the x-axis has shifted to to zero, when the data actually starts at 225.
Does anyone know why this is the case?
%%Extinction coefficient models
close all; clear all; clc;
%%1) STRATUM CORNEUM
load beta_carotene_extinction.txt
wavelength1 = beta_carotene_extinction(1:end,1); % Wavelength (nm)
extinction1 = beta_carotene_extinction(1:end,2); % Molar Extinction (cm-1/M)
% Find fitting function
fitobject1 = fit(wavelength1, extinction1, 'poly9');
% Comparison plots
figure, subplot(2,1,1), plot(wavelength1, extinction1)
xlabel('Wavelength (nm)'), ylabel('Molar Extinction cm^-^1/M')
title('Experiment data')
subplot(2,1,2), plot(fitobject1(wavelength1))
xlabel('Wavelength (nm)'), ylabel('Molar Extinction cm^-^1/M')
title('Curve of best fit')
Also, is there a better way than using polynomials for this kind of data? I'm just using polynomials because Matlab readily gives the you the polynomial coefficients and equation, so I can use it in another file.
EDIT: Totally forgot that no one is going to have access to the data. I've embedded the plot below.

Answers (1)

dpb
dpb on 12 May 2016
subplot(2,1,2), plot(fitobject1(wavelength1))
is plotting against the ordinal number of the array; to just plot over the range use (I think)
subplot(2,1,2), plot(fitobject1)
You can get comparison in the same plot which is often easiest to evaluate from as
subplot(2,1,2), plot(fitobject1,wavelength1,extinction1)
As for the second question, I'd suggest the 'cubicinterp' piecewise cubic interpolation would be better than high-order polynomial. The thing with using the cfit object is that the internals of the model are pretty much hidden so it's no harder to pass one type over another.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!