Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Matlab returns wrong coefficients
Replies: 2   Last Post: Jun 20, 2014 5:25 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Dimitrios

Posts: 2
Registered: 6/20/14
Matlab returns wrong coefficients
Posted: Jun 20, 2014 2:35 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi,

I'm trying to fit a Gaussian to spectral data. The problem is that the cftool gives me different coefficients on the GUI from the ones I extract using the coeffvalues(fitresult) command! Even when the fitting code in my script is the same as the one generated by the tool!!!

I attach a screenshot and my script and would appreciate your help.


clear all; close all;

data = load('0V.txt');
z = data(:,end); %1D vector of intensity values
s = data(1:end,1:end-1); %matrix with the spectrums as rows

Ssize = size(s,1); %number of rows of s matrix (number of spectra)

maxValue = max(s(:)); %finds max intensity of s matrix
[srowsOfMaxes, scolsOfMaxes] = find(s == maxValue); %returns row and column that pass through max value

%--------------------------------------------------------------------------
%Parameters to scale the wavelength axis

%The scaling is done by using a lamp of known spectrum and matching known
%peaks to wavelengths. Then, with linear fitting we find the a and b
%constants of the straight line crossing the points, which constitute the
%scale and offset respectively.

scale = 0.1;
offset = 560.049;
%--------------------------------------------------------------------------
%Pixel axis as shown in Labview
pixels = 1:1:1340;
time = pixels;
%--------------------------------------------------------------------------
%Wavelength axis, after applying scaling
lambda = pixels*scale+offset;
%--------------------------------------------------------------------------
%Define frequency and energy axis
for i = 1:1:1340,
freq(i) = 3*10^8/(lambda(1341-i)*10^(-9)); %Hz
freq(i) = freq(i)/10^12; %THz;
energy(i) = 1240/lambda(i); %eV
end;
%--------------------------------------------------------------------------
%Subtract background noise from data and smooth data

for i = 1:1:Ssize,
rowsmin = min(s(i,:)); %find min of each row (background noise)
s(i,:) = s(i,:) - rowsmin; %subtract it from raw data
a(i,:) = sgolayfilt(s(i,:),3,87); %smoothing
end;

%--------------------------------------------------------------------------
%In order to import the file in Origin Lab, we must transpose the matrix,
%so that each spectrum is a column and not a row, as it is here.
%str = transpose(a);

%save 0V.out str -ASCII
%--------------------------------------------------------------------------
%Plot all raw spectra minus background DC noise level
figure(1);
for i = 1:1:Ssize,
plot(energy, s(i,:));
xlabel('Energy [eV]');
ylabel('Intensity [Counts]');
title('Raw spectra without background DC noise level');
hold all;
end;

%--------------------------------------------------------------------------
s1 = s(1,:);

% Fit: 'One Gaussian Fit'.
[xData, yData] = prepareCurveData( energy, s(1,:) );

% Fit: 'One Gaussian Fit'.
ft = fittype( 'gauss1' );
opts = fitoptions( ft );
opts.DiffMinChange = 1e-06;
opts.Display = 'Off';
opts.Lower = [-Inf -Inf 0];
opts.MaxIter = 4000;
opts.StartPoint = [2651 1.98447945023518 0.0136694453034466];
opts.Upper = [Inf Inf Inf];
ex = excludedata( xData, yData, 'box', [1.92 2.01 0 3000] );
opts.Exclude = ex;

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'One Gaussian Fit' );
plot( fitresult, xData, yData, ex );
legend( 'Spectral Data', 'Excluded Data' , 'One Gaussian Fit' );
% Label axes
xlabel( 'Energy [eV]' );
ylabel( 'Intensity [Counts]' );
grid on

MyCoeffs = coeffvalues(fitresult) %PROBLEM!!!!


Picture:
https://dl.dropboxusercontent.com/u/61594870/toolbox%20fit.png


MyCoeffs has values:

1.0e+03 *

2.283629646095954 0.001968030029287 0.000037120057741



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.