matlab进行多项式拟合

转载文章请注释文章的来源,文章所有权归创作者所有

clear;
filename='D:\常用文件\异养细菌\进度\拟合曲线图\三亚湾1.xls';
sheet=1;
xlRange='B2:T45';
%[num]=xlsread(filename,sheet,xlRange);
[num]= xlsread(filename,sheet,xlRange);
%补充,可以读取成txt,num,raw三种格式文件
[row,col]=size(num);
% dataset=zeros(row,col);
% for i=1:row
%     for j=1:col
%         dataset(i,j)=num(i,j)
%     end
% end
%*****************************************
%重要思想,matlab中可以直接调用矩阵,其他里面通过调用数组来调用矩阵
%****************************************
%选择要读取的列数,并写到data中去
data=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19,20];
%x=zeros(row,col);
% 例如读取第五行chl a数据,可以如下形式
% 写成 x=num(1:row,data(5));5是可以更改的
x1=num(1:row,data(5));
y1=num(1:row,data(19));
%y2=num(1:row,data(20))
x=log10(x1);
y=log10(y1*10^6);
% 理解,ones(length(y),1)代表行数为y,列数为1的,全为1的矩阵,和x矩阵合起来,组成2行*length(y)的矩阵
X=[ones(length(y),1),x];

%11111111111111111111111111
%一元线性取对数拟合
%*****************************
%b是方程的系数矩阵,bint是回归系数的区间,r残差,rint置信区间,stats用于检验回归模型是否正确
 [b,bint,r,rint]=regress(y,X);
 
 %22222222222222222222222222
 %多项式拟合
 %***************************
 T=polyfit(x,y1,3);
 y3=polyval(T,x1);
 subplot(2,1,2),plot(x,y1,'go',x,y1,'b--');
 rm=mean(r);
 subplot(2,1,2),
 plot(x,r,'ro');
 axis([-1.5 1.5 -1 1]);
 hold on;
 %参考线绘制
 refline(0,rm);
 refline(0,0.5);
 refline(0,-0.5);
 title('残差分布图 ');
 xlabel('log10(chl a concentration)');
 ylabel('残差曲线图');
 %rcoplot(r,rint);
% y1=b(1)+b(2)*x;
% plot(x,y,'bx',x,y1,'r');
%%%%%%%%%求解相关系数
% c1=corrcoef(x1,y1);
c=corrcoef(y,x);
% %计算pearson相关性
% c2=corr(x,y,'type','pearson');
% %计算spearman相关性
% c3= corr(x, y, 'type' , 'Spearman');
%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%置信区间绘制************
[p,s] = polyfit(x,y,1);

%standard_1=std(A,0)

subplot(2,1,1),plot(x,y,'ro');
hold on;
[yfit,dy] = polyconf(p,x,s);%'predopt','curve'
%fill主要用于Filled 2-D polygons
%fill([x,fliplr(x)],[yfit-dy,fliplr(yfit+dy)],'r'); %[0.8706 0.9216 0.9804]
line(x,yfit,'color','g')
line(x,yfit-dy,'color','r','linestyle','--')
line(x,yfit+dy,'color','r','linestyle','--')
%text函数主要是用于标注注释的
text(-0.5,7.5,strcat('相关系数r=',num2str(c(1,2))));
nfshcl=strcat('拟合方程式y=',num2str(b(2,1)),'*x+',num2str(b(1,1)));
text(-0.5,5,nfshcl);
title('chl a concentration-bacteria abundance 拟合曲线图 ');
xlabel('log10(chl a concentration)');
ylabel('log10(bacteria abundance)');
axis([-1.5 1.5 4 8]);
% subplot(2,1,1),plot(x,y,'k+',x,y1,'r');

%*******************************
%多项式拟合
%*******************************
% T=polyfit(x,y,1);
% y2=polyval(T,x);
% subplot(2,1,2),plot(x,y,'go',x,y2,'b--');

猜你喜欢

转载自blog.csdn.net/good_learner_1/article/details/88766896