有数据如下,对其进行曲线拟合:
x=[0;0.4;1.2;2;2.8;3.6;4.4;5.2;6;7.2;8;9.2;10.4;11.6;12.4;13.6;14.4;15];
y=[1;0.85;0.29;-0.27;-0.53;-0.4;-0.12;0.17;0.28;0.15;-0.03;-0.15;-0.071;0.059;0.08;0.032;-0.015;-0.02];
实验结果:
使用二次多项式进行曲线拟合
close all;
clc;
clear;
x=[0;0.4;1.2;2;2.8;3.6;4.4;5.2;6;7.2;8;9.2;10.4;11.6;12.4;13.6;14.4;15];
y=[1;0.85;0.29;-0.27;-0.53;-0.4;-0.12;0.17;0.28;0.15;-0.03;-0.15;-0.071;0.059;0.08;0.032;-0.015;-0.02];
plot(x,y,'r*')
p=polyfit(x,y,2)
x2 = 1:.1:18; %需要拟合的横轴范围
y2 = polyval(p,x2); %获取多项式拟合的纵轴数值
plot(x,y,'r*',x2,y2,'k-')
实验结果:
使用三次多项式进行曲线拟合
close all;
clc;
clear;
x=[0;0.4;1.2;2;2.8;3.6;4.4;5.2;6;7.2;8;9.2;10.4;11.6;12.4;13.6;14.4;15];
y=[1;0.85;0.29;-0.27;-0.53;-0.4;-0.12;0.17;0.28;0.15;-0.03;-0.15;-0.071;0.059;0.08;0.032;-0.015;-0.02];
plot(x,y,'r*')
p=polyfit(x,y,3)
x2 = 1:.1:18; %需要拟合的横轴范围
y2 = polyval(p,x2); %获取多项式拟合的纵轴数值
plot(x,y,'r*',x2,y2,'k-')
实验结果:

使用自定义公式::y= acos(kt)exp(wt);
clear ;
close all;
clc;
syms t;
x=[0;0.4;1.2;2;2.8;3.6;4.4;5.2;6;7.2;8;9.2;10.4;11.6;12.4;13.6;14.4;15];
y=[1;0.85;0.29;-0.27;-0.53;-0.4;-0.12;0.17;0.28;0.15;-0.03;-0.15;-0.071;0.059;0.08;0.032;-0.015;-0.02];
f=fittype('a*cos(k*t)*exp(w*t)','independent','t','coefficients',{'a','k','w'});
cfun=fit(x,y,f);
x0=0:0.2:18; %需要拟合的横轴范围
y0=cfun(x0); %用自定义公司拟合的纵轴
plot(x,y,'r*',x0,y0,'b-');
title('自定义公式进行曲线拟合');
实验结果:

cfun参数:

Coefficients (with 95% confidence bounds):
系数参数具有95%置信限:
很明显,自定义曲线拟合的方式较多项式的方法更合优良一些。
++++++++++++++++++++++++++++END+++++++++++++++++++++++++
参考
[1] https://www.bilibili.com/read/cv9604660/