U=[1.45,1.85,2.02,2.2,2.67,2.98]
V=[200,400,500,600,800,1000]
H=[25.6,43.5,51.4,60.2,79.6,95.2]
[AX,H1,H2]=plotyy(U,V,U,H,'plot')
set(H1,'Marker','o','line','none','color','k')
set(H2,'Marker','*','line','none','color','r')
legend('容积','实际高度')
xlabel('电压值/V')
yl1=get(AX(1),'Ylabel')
set(yl1,'string','容积/L')
yl2=get(AX(2),'Ylabel')
set(yl2,'string','实际高度/cm')
p1=polyfit(U,V,1)
p2=polyfit(U,H,1)
VV=p1(1)*U+p1(2)
HH=p2(1)*U+p2(2)
hold on
[AX,HH1,HH2]=plotyy(U,VV,U,HH)
set(HH1,'color','k')
set(HH2,'color','r')
clc
disp(['V=',num2str(p1(1)),'*U',num2str(p1(2))])
disp(['H=',num2str(p2(1)),'*U',num2str(p2(2))])
运行结果
V=513.0775*U-542.8718
H=45.1123*U-39.7716
线性拟合如下:
------------------------------------------------------------------
x=[0 20 40 60 100 130 160 190]
y=[18 17.586 17.136 16.704 15.84 15.129 14.544 13.896]
a=polyfit(x,y,1)
xi=0:0.001:200
yi=polyval(a,xi)
plot(x,y,'go','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',6)
xlabel('深度/m','fontsize',16)
ylabel('温度/℃','fontsize',16)
axis([0 200 12 20])
hold on
plot(xi,yi,'linewidth',2,'markersize',16)
legend('原始数据点','拟合曲线')
sprintf('直线方程:Y=%0.5gxX+%0.5g',a(1),a(2))
-----------------------------------------------------------------
指数拟合如下:
-----------------------------------------------------------------
先定义函数:
function Y=nhfun(b,x)
Y=b(1)*exp(b(2)*x)+b(3)
在运行程序:
x=[0 20 40 60 100 130 160 190]
y=[18 17.586 17.136 16.704 15.84 15.129 14.544 13.896]
x0=[0,1,0] %设置初始值
b=lsqcurvefit(@nhfun,x0,x,y)
Xi=0:0.001:200
Yi=b(1)*exp(b(3)*Xi)+b(2)
plot(x,y,'cp','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',6)
hold on
plot(Xi,Yi,'linewidth',2,'markersize',16)
legend('原始数据点','拟合曲线')
sprintf('曲线方程:Y=%0.5g+e^(%0.5gxX)+(%0.5g)',b(1),b(2),b(3))
--------------------------------------------------------------
!!!曲线方程程序运行时会报错,是因为你的X数据中有一个为0,这个不影响结果的
图每次只能发一张,所以我发到你邮箱哈
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)