本實(shí)例展示了如何使用面向?qū)ο蟮姆椒▽?duì)數(shù)據(jù)進(jìn)行非線性擬合关翎;涉及到的主要知識(shí)點(diǎn)為:
- 利用nlinfit進(jìn)行非線性擬合扛门;
- 利用Dependent關(guān)鍵詞實(shí)現(xiàn)屬性的實(shí)時(shí)更新;
- 使用get和set實(shí)現(xiàn)對(duì)屬性的賦值(其實(shí)不需要纵寝,僅僅是為了示范其寫法)论寨;
- 使用了局部函數(shù);
具體的代碼如下:
classdef Rate_cal<handle
properties
x;
y;
end
properties(Dependent)
Fit;
Rate;
Hfigure;
end
methods
function obj = Rate_cal(x,y)
obj.x=x;
obj.y=y;
end
function set.x(obj,val)
obj.x=val;
end
function set.y(obj,val)
obj.y=val;
end
function Fit=get.Fit(obj)
[Fit,~]=Rate_cal3(obj.x,obj.y);
end
function Rate=get.Rate(obj)
[~,Rate]=Rate_cal3(obj.x,obj.y);
end
function Hfigure=get.Hfigure(obj)
Hfigure= plot(obj.x,obj.y);
end
end
end
function [f,f3]=Rate_cal3(x,y)
% 本函數(shù)用于計(jì)算物質(zhì)生成/消耗速率
%
% f為物質(zhì)檢測(cè)數(shù)據(jù)的擬合爽茴;f3為物質(zhì)消耗速率方程
%
% see also:nlinfit,matlabFunction
f=@(b,x)(b(2) + (b(1)-b(2))./(1 + exp((x-b(3))./b(4))));
beta0=[63;7;14;3];
beta = nlinfit(x,y,f,beta0);
syms t;
f=f(beta,t);
f2=diff(f,t,1);
f3=matlabFunction(f2);
f=matlabFunction(f);
end