求matlab高手帮忙编写一个函数拟合程序。估计可以得到一个幂函数或者指数函数。
x=[200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000]
y=[0.1 0.25 0.49 0.65 0.7 0.91 1.15 1.26 1.37 1.46 1.52 1.60 1.65 1.67 1.68 1.68 1.69 1.69 1.71]
最后请给出程序代码,要输入函数式、参数、并绘制图形。
十分感谢!!! 展开
【代码】
x=[200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000];
y=[0.1 0.25 0.49 0.65 0.7 0.91 1.15 1.26 1.37 1.46 1.52 1.60 1.65 1.67 1.68 1.68 1.69 1.69 1.71];
cftool(x,y);
【拟合方式一:指数拟合】
General model Power2:
f(x) = a*x^b+c
Coefficients (with 95% confidence bounds):
a = -44.95 (-570, 480.1)
b = -0.02049 (-0.297, 0.2561)
c = 40.3 (-490, 570.6)
Goodness of fit:
SSE: 0.1527
R-square: 0.9708
Adjusted R-square: 0.9672
RMSE: 0.0977
【拟合方式二:最高三次多项式】
Linear model Poly3:
f(x) = p1*x^3 + p2*x^2 + p3*x + p4
Coefficients (with 95% confidence bounds):
p1 = -1.208e-011 (-1.778e-010, 1.536e-010)
p2 = -6.613e-007 (-1.214e-006, -1.088e-007)
p3 = 0.002397 (0.001855, 0.00294)
p4 = -0.388 (-0.5376, -0.2384)
Goodness of fit:
SSE: 0.02784
R-square: 0.9947
Adjusted R-square: 0.9936
RMSE: 0.04308
【拟合方式三:最高四次多项式】
Linear model Poly4:
f(x) = p1*x^4 + p2*x^3 + p3*x^2 + p4*x + p5
Coefficients (with 95% confidence bounds):
p1 = 4.099e-013 (1.256e-013, 6.942e-013)
p2 = -1.815e-009 (-3.073e-009, -5.575e-010)
p3 = 2.001e-006 (1.018e-007, 3.9e-006)
p4 = 0.0009045 (-0.0002188, 0.002028)
p5 = -0.1391 (-0.3494, 0.07117)
Goodness of fit:
SSE: 0.01654
R-square: 0.9968
Adjusted R-square: 0.9959
RMSE: 0.03437
【小结】
用y=c1*x^2/(x^2+c2^2)拟合
clear all
clc
x=[200 300 400 500 600 700 800 900 ...
1000 1100 1200 1300 1400 1500 ...
1600 1700 1800 1900 2000];
y=[0.1 0.25 0.49 0.65 0.7 0.91 1.15 ...
1.26 1.37 1.46 1.52 1.60 1.65 ...
1.67 1.68 1.68 1.69 1.69 1.71];
F=@(c)sum((y-c(1)*x.^2./(x.^2+c(2).^2)).^2);
[coef,fval]=fminsearch(F,[0.1,0.1]);
c1=coef(1)
c2=coef(2)
x1=0:1:2200;
y1=coef(1)*x1.^2./(x1.^2+coef(2).^2);
plot(x,y,'ok','Markersize',8)
hold on
plot(x1,y1,'-k')
xlabel('x')
ylabel('y')