循环遍历列数据以适应matlab中的曲线

时间:2015-10-08 08:50:39

标签: matlab loops multiple-columns curve-fitting

我有一个电子表格ANA2009_1ag.xlsx,第1列=波长λ,col2 = specta1,col3 = spectra2,col4 = spectra3等

我想使用以下公式为每个光谱拟合曲线:

ag(lambda) = ag(lambda_o)*exp(-S(lambda-lambda_o)

所以我可以得到斜率S

我有代码,如果我有一个包含2列的文本文件,我可以运行它: 第1列=波长λ和col2 = specta1 但正如我上面所说,我有几个光谱,所以我想做一个循环,以便我得到的程序拟合曲线,并获得从一列到下一列的斜率循环。作为matlab循环的新手是我最大的问题。 我的代码如下。

%//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[num,txt,raw]=xlsread('ANA2009_1ag.xlsx');

%// Get the size of data to be plotted 
[r,c]=size(num);

%//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %// using txt vector to make appropriate legend that matches the data plot
   Sepctra=cellstr(txt); %//C = cellstr converts array to a cell array.
   %//FIND OUT HOW YOU CAN SAY TO END OF COLUMN 
for i=2:7
   Sepctra(i)=txt(i);
end   

%//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%//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')

数据从300海里(LAMBDA)到685海里(λ)但我选择从650-400NM拟合曲线

Wavelength  AN10070A-4m AN10066A-10m 
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

我的预期输出是: x1是412nm处的振幅和斜率S的矢量 我想发布我得到的曲线拟合的数字,但我不知道该怎么做。

0 个答案:

没有答案