使用 Matlab 进行最小二乘参数估计
Least Square Parameter Estimation with Matlab
我试图通过我自己的模型使用最小二乘法找到系统参数的值,即(a1 和 a2)。
我的模型是 MISO 系统:% tz(k) = a1*(((1/2*t)*(to*to(k-1)))- (1/2*t)*(tz*tz(k-1))) + a2*(((1/2*t)*(tn*tn(k-1)))- (1/2*t)*(tx*tx(k-1)));
我计算参数的方法:Xθ = (X'X)^(-1)X'Y
我试过用我自己的模型拟合REGRESSOR,但总是报错,是哪里搞错了??
close all;
clear all;
clc;
%% =====================
% MISO system using Least Square
% Model of the system, described by diference equation
% tz(k) = a1*(((1/2*t)*(to*to(k-1)))- (1/2*t)*(tz*tz(k-1))) + a2*(((1/2*t)*(tn*tn(k-1)))- (1/2*t)*(tx*tx(k-1)));
% Sample Data input and output of the system,
t = 15 ; %sampling time 15 minutes
to=[23; 23; 23; 23; 23; 23; 23;]; %input 1
tz=[26; 27; 27; 27; 26; 27; 26]; %output
tn=[26; 27; 27; 27; 26; 27; 26]; %input 2
tx=[26; 27; 27; 27; 26; 27; 26]; %input 3
% Problem: Find the value of a1 and a2 with Least Square Algorithm.
%% =====================
% Least Square
% model: Y=Reg*Par;
% Reg: regresor, Par: parameter.
% Arranging elemen Y, Reg, and Par:
% Y=[y(2); ...; y(N);
% Reg=[-y(1) u(1); ....; -y(N-1) u(N-1)];
% Reg=[-y(1) u(1); ....; -y(N-1) u(N-1)];
% Par=[a1;b1];
% Find value of Parameter:
% Par=(Reg.'*Reg)\Reg.'*Y;
%% =====================
% Program MATLAB
N=length(tz); % Count Total element vektor tz.
% Register elemen Y
Y=tz(2:end,1);
% Register elemen Regressor
Reg=[((1/2*t)*to*(to(1:N-1,1)))-((1/2*t)*tz*(tz(1:N-1,1))) ((1/2*t)*tn*(tx(1:N-1,1)))-((1/2*t)*tn*(tx(1:N-1,1)))];
% Least Square for getting the value of a1 and b4:
Par=(Reg.'*Reg)\Reg.'*Y;
disp('Value of a1 and a2 :');
a1=Par(1)
a2=Par(2)
提前致谢
尝试:
Reg=zeros(6,2);
for ii=2:length(to)
Reg(ii-1,:) = [(((1/2*t)*(to(ii)*to(ii-1)))- (1/2*t)*(tz(ii)*tz(ii-1))),...
(((1/2*t)*(tn(ii)*tn(ii-1)))- (1/2*t)*(tx(ii)*tx(ii-1)))];
end
或:
Reg1 = [(((1/2*t)*(to(1:end-1).*to(2:end)))- (1/2*t)*(tz(1:end-1).*tz(2:end))),...
(((1/2*t)*(tn(1:end-1).*tn(2:end)))- (1/2*t)*(tx(1:end-1).*tx(2:end)))];
isequal
告诉我他们得到了相同的答案。
但是,您的数据一定有问题,因为 Reg
的第二列全为零,Par(2)
将变为 NaN
。
我试图通过我自己的模型使用最小二乘法找到系统参数的值,即(a1 和 a2)。
我的模型是 MISO 系统:% tz(k) = a1*(((1/2*t)*(to*to(k-1)))- (1/2*t)*(tz*tz(k-1))) + a2*(((1/2*t)*(tn*tn(k-1)))- (1/2*t)*(tx*tx(k-1)));
我计算参数的方法:Xθ = (X'X)^(-1)X'Y
我试过用我自己的模型拟合REGRESSOR,但总是报错,是哪里搞错了??
close all;
clear all;
clc;
%% =====================
% MISO system using Least Square
% Model of the system, described by diference equation
% tz(k) = a1*(((1/2*t)*(to*to(k-1)))- (1/2*t)*(tz*tz(k-1))) + a2*(((1/2*t)*(tn*tn(k-1)))- (1/2*t)*(tx*tx(k-1)));
% Sample Data input and output of the system,
t = 15 ; %sampling time 15 minutes
to=[23; 23; 23; 23; 23; 23; 23;]; %input 1
tz=[26; 27; 27; 27; 26; 27; 26]; %output
tn=[26; 27; 27; 27; 26; 27; 26]; %input 2
tx=[26; 27; 27; 27; 26; 27; 26]; %input 3
% Problem: Find the value of a1 and a2 with Least Square Algorithm.
%% =====================
% Least Square
% model: Y=Reg*Par;
% Reg: regresor, Par: parameter.
% Arranging elemen Y, Reg, and Par:
% Y=[y(2); ...; y(N);
% Reg=[-y(1) u(1); ....; -y(N-1) u(N-1)];
% Reg=[-y(1) u(1); ....; -y(N-1) u(N-1)];
% Par=[a1;b1];
% Find value of Parameter:
% Par=(Reg.'*Reg)\Reg.'*Y;
%% =====================
% Program MATLAB
N=length(tz); % Count Total element vektor tz.
% Register elemen Y
Y=tz(2:end,1);
% Register elemen Regressor
Reg=[((1/2*t)*to*(to(1:N-1,1)))-((1/2*t)*tz*(tz(1:N-1,1))) ((1/2*t)*tn*(tx(1:N-1,1)))-((1/2*t)*tn*(tx(1:N-1,1)))];
% Least Square for getting the value of a1 and b4:
Par=(Reg.'*Reg)\Reg.'*Y;
disp('Value of a1 and a2 :');
a1=Par(1)
a2=Par(2)
提前致谢
尝试:
Reg=zeros(6,2);
for ii=2:length(to)
Reg(ii-1,:) = [(((1/2*t)*(to(ii)*to(ii-1)))- (1/2*t)*(tz(ii)*tz(ii-1))),...
(((1/2*t)*(tn(ii)*tn(ii-1)))- (1/2*t)*(tx(ii)*tx(ii-1)))];
end
或:
Reg1 = [(((1/2*t)*(to(1:end-1).*to(2:end)))- (1/2*t)*(tz(1:end-1).*tz(2:end))),...
(((1/2*t)*(tn(1:end-1).*tn(2:end)))- (1/2*t)*(tx(1:end-1).*tx(2:end)))];
isequal
告诉我他们得到了相同的答案。
但是,您的数据一定有问题,因为 Reg
的第二列全为零,Par(2)
将变为 NaN
。