The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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 ]

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


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
%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

%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
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;

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!!!!


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]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.