怎么把两个线拟合到一个图里,然后显示y轴标题为“电阻值”,x轴标题为“个数”
x=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
y1=[2.23,2.53,4.52,3.01,2.75,3.86,2.25,2.39,0.19,2.62,-4.68,-4.76,-10.75,-7.84,-5.25,-9.96];
y2=[2.37,2.53,3.91,3.01,2.75,3.86,2.25,2.39,0.19,2.62,1.07,-4.76,-10.75,-7.84,-5.25,-9.96];
figure(1);
%plot(x,y1,'r',x,y2,'b);
plot(x,y,'r');
title('线性拟合')
hold on;
[a,b]=Linear_fitting(x,y);
t=1:1:16;
z=a*t+b;
plot(t,z);
function [a,b]=Linear_fitting(x,y)
n=size(x,2);
sum_xy=sum(x.*y);
sum_x=sum(x);
sum_y=sum(y);
sum_xx=sum(x.*x);
a=(n*sum_xy-sum_x*sum_y)/(n*sum_xx-sum_x^2);
b=(sum_xy-sum_xx*sum_y/sum_x)/(sum_x-n*sum_xx/sum_x);
end
|