matlab中nlinfit函数非线性拟合

matlab中nlinfit函数非线性拟合的使用方法如下:
例1
clear all;
x1=[0.4292 0.4269 0.381 0.4015 0.4117 0.3017]';
x2=[0.00014 0.00059 0.0126 0.0061 0.00425 0.0443]';
x=[x1 x2];
y=[0.517 0.509 0.44 0.466 0.479 0.309]';
f=@(p,x) 2.350176*p(1)*(1-1/p(2))*(1-(1-x(:,1).^(1/p(2))).^p(2)).^2.*(x(:,1).^(-1/p(2))-1).^(-p(2)).*x(:,1).^(-1/p(2)-0.5).*x(:,2);
p0=[8 0.5]';
opt=optimset('TolFun',1e-3,'TolX',1e-3);%
[p R]=nlinfit(x,y,f,p0,opt)
例2
混凝土的抗压强度随养护时间的延长而增加,现将一批混凝土作成12个试块,记录了养护日期x(日)及抗压强度y(kg/cm2)的数据:
养护时间x 2 3 4 5 7 9 12 14 17 21 28 56
抗压强度y 35+r 42+r 47+r 53+r 59+r 65+r 68+r 73+r 76+r 82+r 86+r 99+r
建立非线性回归模型,对得到的模型和系数进行检验。
注明:此题中的+r代表加上一个[-0.5,0.5]之间的随机数
%模型为:y=a+k1*exp(m*x)+k2*exp(-m*x);
clc;clear;
x=[2 3 4 5 7 9 12 14 17 21 28 56];
r=rand(1,12)-0.5;
y1=[35 42 47 53 59 65 68 73 76 82 86 99];
y=y1+r
myfunc=inline('beta(1)+beta(2)*exp(beta(4)*x)+beta(3)*exp(-beta(4)*x)','beta','x');
beta=nlinfit(x,y,myfunc,[0.5 0.5 0.5 0.5]);
a=beta(1),k1=beta(2),k2=beta(3),m=beta(4)
%test the model
xx=min(x):max(x);
yy=a+k1*exp(m*xx)+k2*exp(-m*xx);
plot(x,y,'o',xx,yy,'r')
结果:
a = 87.5244
k1 = 0.0269
k2 = -63.4591
m = 0.1083
You need to define your 'model' as a function (possibly in a seperate m-file). For example
>>beta = nlinfit(X,y,myfun,beta0)
where MYFUN is a MATLAB function such as:
function yhat = myfun(beta, X)
b1 = beta(1);
b2 = beta(2);
yhat = 1 ./ (1 + exp(b1 + b2*X));
MYFUN can also be an inline object:
fun = inline('1 ./ (1 + exp(b(1) + b(2*x))', 'b', 'x')
nlinfit(x, y, fun, b0)
例3
y=(Asin(Bx)+C)*sin(Dx)+E
x=11.96,y=1.7;
x=12.22,y=7.3;
x=27.56,y=1.0;
x=27.82,y=8.0;
x=19.76,y=4.5;
求未知参数A,B,C,D,E
x=[11.96,12.22,19.76,27.56,27.82];
y=[1.7,7.3,4.5,1.0,8.0];
fun=inline('(a(1)*sin(a(2)*x)+a(3)).*sin(a(4)*x)+a(5)','a','x');
a=nlinfit(x,y,fun,[0.05 0.05 0.05 0.05 0.05]);
yy=(a(1)*sin(a(2)*x)+a(3)).*sin(a(4)*x)+a(5);
plot(x,y,'*',x,yy,'r')
非线性优化-matlab函数库-optimset
创建或编辑一个最优化参数选项
句法规则
options = optimset('param1',value1,'param2',value2,...) %设置所有参数及其值,未设置的为默认值options = optimset %全部设置为默认
options = optimset(optimfun) %设置与最优化函数有关的参数为默认
options = optimset(oldopts,'param1',value1,...) %复制一个已存在的选项,修改特定项
options = optimset(oldopts,newopts) %用另一个新选项合并目前选项
因素
ParameterValueDescriptionDisplay
'off' | 'iter' | 'final' | 'notify'
'off' 表示不显示输出; 'iter' 显示每次迭代的结果; 'final' 只显示最终结果; 'notify' 只在函数不收敛的时候显示结果.MaxFunEvals
positive integer函数允许估值的最大值.MaxIter
positive integer迭代次数的最大值.TolFun
positive scalar函数迭代的终止误差.TolX
positive scalar结束迭代的X值.
Tags: 

延伸阅读

最新评论

发表评论