looping through column data to fit curve

조회 수: 3 (최근 30일)
Helga Gomes
Helga Gomes 2015년 10월 9일
편집: Walter Roberson 2015년 10월 9일
I have a spreadsheet 'ANA2009_1ag.xlsx' with column 1= wavelength lambda and col2=specta1, col3=spectra2, col4=spectra3 and so on
I want to fit a curve to each of the spectra using the equation:
ag(lambda) = ag(lambda_o)*exp(-S(lambda-lambda_o)
So I can get the slope amplitude at lambda and slope S.
I have the code and I am able to run it if I have a single text file with 2 columns: column 1= wavelength lambda and col2=specta1 But as i said above I have several spectra so i would like to make a loop so that the routine that i have for fitting the curve (and getting the slope) loops from one column to the next. My code is as follows.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[num,txt,raw]=xlsread('ANA2009_1ag.xlsx');
%Get the size of data to be plotted
[r,c]=size(num);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% takeout range of interest
I=find(num(:,1)<650 & num(:,1)>400);
wl=num(I,1);
a_g=num(I,2);
%setting options for fmisearch
opts = optimset('fminsearch');
opts = optimset(opts,'MaxIter',4000);
opts = optimset(opts,'MaxFunEvals',2000); %// usually 100*number of params
opts = optimset(opts,'TolFun',1e-9);
%opts = optimset('LevenbergMarquardt','on');
%guess for paramters (amplitude at 532 and slope)
x0=[0.1, 0.03];
%minimization routine
x1 = fminsearch(@least_squares,x0,opts,a_g,wl)
%plot data and fit
plot(wl, a_g, '.k', wl, x1(1)*exp(-x1(2)*(wl-412)),'b')
DATA IS FROM 300 NM (LAMBDA) TO 685 NM (lambda) BUT I CHOSE TO FIT THE CURVE FROM 650-400NM
EXAMPLE OF DATA:
Wavelength Spectra-1 Spectra-2
300 1.561434 1.434769
300.5 1.549919 1.42786
301 1.531495 1.414042
301.5 1.506162 1.400224
302 1.483132 1.386406
302.5 1.467011 1.372588
303 1.45089 1.356467
303.5 1.443981 1.342649
304 1.42786 1.333437
304.5 1.414042 1.324225
305 1.407133 1.31271
My expected output is: x1 a vector which contains the amplitude at 412nm and slope S

답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by