一个x对应两组y的数据在matlab中拟合怎样将曲线显示到一个图形中
c=[0.53101 0.52179 0.50123 0.47652 0.45231 0.42542 0.40997 0.38728 0.37084 0.35515 0.33933 0.32311 0.31735 0.30787 0.3017 0.2972 0.28969 0.28652 0.28727 0.28915 0.29334 0.29975 0.30972 0.32113 0.33944 0.35925 0.37153 0.39057 0.45678 0.48005 0.47624 0.50166];
d=[0.51879 0.50849 0.48348 0.46559 0.4435 0.4135 0.40336 0.382 0.36632 0.35128 0.33528 0.32028 0.3155 0.30666 0.30121 0.2964 0.29102 0.28939 0.28983 0.2934 0.30028 0.30755 0.31798 0.32893 0.34736 0.36269 0.37796 0.3948 0.4401 0.46196 0.48057 0.50569];
t是自变量,c和d分别是两次测量的数据,要两次数据都和t拟合,而且要两个拟合曲线显示在一起,以形成对比,求程序,或者指导,有追加 展开
(1)通过对原始数据绘图可看出t-c、t-d都基本符合二次曲线,所以就用二次多项式来拟合所给数据;
(2)代码如下,为了看效果,分别画在两幅图中:
t=[79.55 89.55 99.55 109.55 119.55 129.55 139.55 149.55 159.55 169.55 179.55 189.55 199.55 209.55 219.55 229.55 239.55 249.55 259.55 269.55 279.55 289.55 299.55 309.55 319.55 329.55 339.55 349.55 359.55 369.55 379.55 389.55];
c=[0.53101 0.52179 0.50123 0.47652 0.45231 0.42542 0.40997 0.38728 0.37084 0.35515 0.33933 0.32311 0.31735 0.30787 0.3017 0.2972 0.28969 0.28652 0.28727 0.28915 0.29334 0.29975 0.30972 0.32113 0.33944 0.35925 0.37153 0.39057 0.45678 0.48005 0.47624 0.50166];
d=[0.51879 0.50849 0.48348 0.46559 0.4435 0.4135 0.40336 0.382 0.36632 0.35128 0.33528 0.32028 0.3155 0.30666 0.30121 0.2964 0.29102 0.28939 0.28983 0.2934 0.30028 0.30755 0.31798 0.32893 0.34736 0.36269 0.37796 0.3948 0.4401 0.46196 0.48057 0.50569];
p1 = polyfit(t,c,2); %用来拟合t-c的二次多项式,多项式的系数按降幂排列在数组p1中
p2 = polyfit(t,d,2); %用来拟合t-d的二次多项式,多项式的系数按降幂排列在数组p2中
fc = polyval(p1,t); %计算拟合值
fd = polyval(p2,t); %计算拟合值
%plot(t,c,t,fc,t,d,t,fd,'linewidth',2)
subplot(1,2,1)
plot(t,c,t,fc,'linewidth',2)
legend('t-c','t-fc')
subplot(1,2,2)
plot(t,d,t,fd,'linewidth',2)
legend('t-d','t-fd')
图如下:
c=[0.53101 0.52179 0.50123 0.47652 0.45231 0.42542 0.40997 0.38728 0.37084 0.35515 0.33933 0.32311 0.31735 0.30787 0.3017 0.2972 0.28969 0.28652 0.28727 0.28915 0.29334 0.29975 0.30972 0.32113 0.33944 0.35925 0.37153 0.39057 0.45678 0.48005 0.47624 0.50166];
d=[0.51879 0.50849 0.48348 0.46559 0.4435 0.4135 0.40336 0.382 0.36632 0.35128 0.33528 0.32028 0.3155 0.30666 0.30121 0.2964 0.29102 0.28939 0.28983 0.2934 0.30028 0.30755 0.31798 0.32893 0.34736 0.36269 0.37796 0.3948 0.4401 0.46196 0.48057 0.50569];
p1=polyfit(t,c,2);
p2=polyfit(t,d,2);
plot(t,c,'r',t,polyval(p1,t),'r',t,d,'b',t,polyval(p2,t),'b','linewidth',2)
红色为一组,蓝色为一组
祝你学习愉快!