您的位置:首页 > 编程语言 > MATLAB

matlab---数据拟合

2016-02-22 22:18 459 查看
指数曲线拟合

function [a, resnorm] = fit
t = 0: 60: 1800;
f = [100 96.1111 92.7778 89.4444 86.6667 84.4444 82.2222 80 77.7778 76.1111 ...
74.4444 72.7778 71.1111 69.4444 68.3333 67.2222 66.1111 65 63.8889 ...
62.7778 61.6667 60.5556 59.4444 58.3333 57.2222 56.1111 55 53.8889 ...
52.7778 51.6667 51.1111];
grid;
fun = inline('a(1) * exp(-1 * a(2) * t) + 40', 'a', 't');
a0 = zeros(1, 31);
[a, resnorm] = lsqcurvefit(fun, a0, t, f);
plot(t, f, '*');
hold on;
g = a(1) * exp(-1 * a(2) * t) + 40;
plot(t, g, 'r-');
xlabel('t');
ylabel('f1');
title('f1(t)');
end




function [a, resnorm ] = fitN

x = [11 15 20 21 22 23 25 26 27];
t = [0.053 0.36 2.3 5.1 10.9 16.8 87.5 132.3 188.6];

fun = inline('a(1) * a(2).^x + a(3)', 'a', 'x');
a0 = [1 1 1];
[a, resnorm] = lsqcurvefit(fun, a0, x, t);
plot(x, t, '*');
hold on;
g = a(1) * a(2).^x + a(3);
plot(x, g, 'r-');

xlabel('x');
ylabel('t');
title('时间');

end




多项式拟合

function [a, c ] = fitN
x = [5 11 15 19 20 21 22 23 24 25 26 27];
t = [0.0036 0.053 0.36 1.3 2.3 5.1 10.9 16.8 36.1 87.5 132.3 188.6];
syms c;
for i = 1:18
y2 = polyfit(x, t, i);
Y = polyval(y2, x);    %计算拟合函数在x处的值。
if sum((Y-t).^2)<0.1
c = i;            %c为求得的多项式最高阶数, 误差平方和精度范围为0.1
break;
end
end

plot(x, t, '*');
hold on;
a = polyfit(x, t, c);     %得到系数,按次数从高到低排列
y = polyval(a, x);

%y = poly2sym(a, x);      %在主窗口输入,系数数组转换为多项式
%vpa(y)                   %在主窗口输入,分数转换为小数
%y = 0.000001*x.^11 - 0.0002*x.^10 + 0.02*x.^9 - 1.1*x.^8 + 41.55*x.^7 - 1039.46*x.^6 + 18254.16*x.^5 - 224494.33*x.^4 + 1888238.89*x.^3 - 10294775.069*x.^2 + 32510012.84*x - 44542021.91;  %得到的多项式,从主窗口中粘贴而来

plot(x, y, 'r-');
xlabel('x');
ylabel('t');
title('时间');

end


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: