matlab 参数识别,[转载]自编最小二乘法的Matlab参数辨识程序(含实例)
function [sysd,sys,err] = ID(Y,U,Ts)
%
%基于遞推最小二乘法的參數辨識程序
%僅針對二階系統:)
%出處:http://blog.sina.com.cn/xianfa110
%=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=
%Inputs:
%=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=
%Y = nX1 vector of your model output
%U = nX1 vector of your model input
%Ts = sample time
%
%=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=
%Outputs:
%=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=
%sysd = discrete-time transfer function identified
%sys = continuous-time transfer function identified
%err = error
%
if nargin<3 || nargin>3
error('Must be three inputs!');
end
if length(Y)~=length(U)
error('length of inputs must be equal.');
end
n=length(U);
Y=reshape(Y,n,1);
U=reshape(U,n,1);
theta=[0.1;0.1;0.1;0.1;0.1];
P=2^25*eye(5);
R0=1;
for m=3:n
X=[Y(m-1) Y(m-2) U(m) U(m-1) U(m-2)]';
alfa=1/(R0+X'*P*X);
L=alfa*P*X;
theta(:,m-1)=theta(:,m-2)+L*(Y(m)-X'*theta(:,m-2));
P=P/R0-alfa*P*X*X'*P;
err=Y(m)-X'*theta(:,m-2);
if abs(err)<=1e-10
break;
end
end
m=length(theta(1,:));
result=[-theta(1:2,m);theta(3:5,m)];
t=1:m;
figure;
plot(t,theta(1,:),t,theta(2,:),t,theta(3,:),t,theta(4,:),t,theta(5,:));
legend('th1','th2','th3','th4','th5');
num=[result(3),result(4),result(5)];
den=[1,result(1),result(2)];
sysd=tf(num,den,Ts);
[n,d]=d2cm(num,den,Ts,'tustin');
sys=tf(n,d);
%%====================================================
exaple:
對于以下模型:
運行之后,數據通過Scope傳遞到工作空間,方法參見Simulink利用Scope輸出及繪制仿真波形技巧。
輸入以下代碼:
Y=data(:,3);U=data(:,2);
[sysd,sys,e]=ID(Y,U,0.001);
得到結果如下:
Transfer function:
-4.314e-009 z^2 + 8.784e-005 z + 4.362e-005
-------------------------------------------
z^2 - 1.975
z + 0.9753
Sampling time: 0.001
Transfer function:
-1.12e-005 s^2 - 0.04417 s + 133.1
----------------------------------
s^2 + 25.02 s - 0.008906
e =
-6.8120e-011
相關文章:
總結
以上是生活随笔為你收集整理的matlab 参数识别,[转载]自编最小二乘法的Matlab参数辨识程序(含实例)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java jmx 监控_只用五分钟为系统
- 下一篇: 剩余 大小 查看内存_JVM的内存分配策