I tried to find a value of a parameter of the system namely(a1 and a2) using Least Square with my own models.
My Model is MISO system: % 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)));
My method to calculate the parameter : Xθ = (X'X)^(-1)X'Y
I was tried to fit the REGRESSOR with my own models, but it always showed error, where I made a mistake??
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)
Thanks in advance
Try:
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
Or:
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
tells me that they get same answer.
However, your data must have some problem since the second column of Reg
is all zeros and Par(2)
will become NaN
.