📄 f30.m
字号:
function f30 %曲线拟合 函数发现
%往往给出一些数据,然后通过曲线拟合,然后预测未知的数据
close all
time=[0,620,2266,3482];
temp=[145,130,103,90];
temp=temp-68;
subplot(2,1,1);
plot(time ,temp,time ,temp,'o'),xlabel('Time (s)'),ylabel('Relative Temperature (F)');
subplot(2,1,2);
semilogy(time ,temp,time ,temp,'o'),xlabel('Time (s)'),ylabel('Relative Temperature (F)');
%------------------------------------------------------------
%对其进行拟合
p=polyfit(time ,log10(temp),1); %指数函数T=68+b*10^(mt)
m=p(1);
b=10^p(2);
t=[0:10:4000];
Te=68+b*10.^(m*t);
figure(2)
semilogy(t ,Te-68,time ,temp,'o'),xlabel('Time (s)'),ylabel('Relative Temperature (F)'); %预测模型的图
%-----------------------------------------------
%help polyfit polyval
%p=polyfit(x,y,n) 用一个n阶多项式拟合由矢量x y所描述的数据。p为多项式系数 polyval可获得可预测的误差估计
figure(3)
x=[1:9];
y=[5,6,10,20,28,33,34,36,42];
xp=[1:0.01:9];
for k=1:4
coeff=polyfit(x,y,k);
yp(k,:)=polyval(coeff,xp);
j(k)=sum((polyval(coeff,x)-y).^2);%累计误差
subplot(2,2,k)
plot(xp,yp(k,:),x,y,'o'),axis([0 10 0 50]);
end
disp(j)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -