matlab nlinfit函数的应用例子

混凝土的抗压强度随养护时间的延长而增加,现将一批混凝土作成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)

 

你可能感兴趣的:(matlab,function,plot,fun,object)