第十四课matlab郭彦甫 曲线回归分析与内插答案
Exercise1 :given the table below:
- find the β0 and β1 of the regression line
- plot the figure

clear; close all;
Temper = [20 30 40 50 60];
TC_Output = [0.025 0.035 0.050 0.060 0.080];
fit = polyfit(Temper, TC_Output,1);
xfit = [Temper(1):0.1:Temper(end)]; yfit = fit(1)*xfit + fit(2);
plot(Temper, TC_Output ,'bo',xfit,yfit,'r-'); set(gca,'fontsize',14);
xlabel('Temperature(℃)'); ylabel('TC Output(mV)');
title('Calibration of TC')
grid on ;

Exercise 2: find the 4th,5th and 6th-order polynomial;
- 找到如下x,y的4阶,5阶和6阶多项式的最佳系数解。
- QUESTION:is it better to use higher order polynomials?
x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
clear ;close all;
x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
for i = 4:6;
subplot(1,3,i-3);p = polyfit(x,y,i);
xfit = x(1):0.1:x(end); yfit = polyval(p, xfit);
plot(x, y ,'ro',xfit,yfit,'b-'); set(gca,'fontsize',14);
ylim([-17,11]),legend('Data points','Fitted curve','Location','southeast')
end

Exercise3 :fit the data using linear lines and cubic splines;
- 对下列数据进行线性拟合和三次样条拟合
x = [0 0.25 0.75 1.25 1.5 1.75 1.85 2 2.125 2.25];
y = [1.2 1.18 1.1 1 0.92 0.8 0.7 0.55 0.35 0];
clear ; close all;
x = [0 0.25 0.75 1.25 1.5 1.75 1.85 2 2.125 2.25];
y = [1.2 1.18 1.1 1 0.92 0.8 0.7 0.55 0.35 0];
plot(x,y,'ro','MarkerFaceColor','r');
xlim([0,2.5]);ylim([0,1.4]);box on;
set(gca,'Fontsize',14);
y =interp1(x,y,x);
hold on;plot(x,y,'-b','linewidth',2);
hold off;
y =spline(x,y,x);
hold on;plot(x,y,'-.g','linewidth',2);
hold off;
h = legend('original','linear','Spline');
set(h,'fontname','times new roman');

以上为matlab2021a版可操作,其他版本可以试试。如有不理解,请留言在评论区。