please help with my code

Im trying to use kalman filters
A = [1.1269 -0.4940 0.1129;
1.0000 0 0;
0 1.0000 0];
B = [-0.3832;
0.5919;
0.5191];
C = [1 0 0];
Plant = ss(A,[B B],C,0,[] ,'inputname',{'u' 'w'},'outputname','y');
Q = 1;
R = 1;
[kalmf,L,P,M] = kalman(Plant,Q,R);
M
kalmf = kalmf(1,:);
a = A;
b = [B B 0*B];
c = [C;C];
d = [0 0 0;0 0 1];
P = ss(a,b,c,d,-1,'inputname',{'u' 'w' 'v'},'outputname',{'y' 'yv'});
sys = parallel(P,kalmf,1,1,[],[]);
SimModel = feedback(sys,1,4,2,1); % Close loop around input #4 and output #2
SimModel = SimModel([1 3],[1 2 3]); % Delete yv from I/O list
SimModel.InputName;
SimModel.OutputName;
src = dsp.SignalSource('SamplesPerFrame',5600);
src.Signal = xlsread('D:\ieee14\trainingset.xlsx',1,'A1:AB5601');
u = src.Signal;
src1 = dsp.SignalSource('SamplesPerFrame',5600);
src1.Signal = xlsread('D:\ieee14\faultset.xlsx',1,'A1:AB5601');
w = src1.Signal;
src2 = dsp.SignalSource('SamplesPerFrame',5600);
src2.Signal = xlsread('D:\ieee14\faultset.xlsx',1,'A5601:AB11201');
v = src2.Signal;
%T = horzcat[w,v,u,...];
%D = vertcat(u,v,w);
[out,x] = lsim(SimModel,[],[w,v,u]);
% sine1 = dsp.SineWave('spf',5600);
% sine1.Signal = xlsread('D:\ieee14\faultset.xlsx',1,'A1:N5601'), 10,xlsread('D:\ieee14\faultset.xlsx',1,'O1:AB5601');
% src = dsp.SignalSource(xlsread('D:\ieee14\faultset.xlsx',1,'A5601:AB11201'));
% %src.Signal = xlsread('D:\ieee14\faultset.xlsx',1,'A5601:AB11201');
%
i keep getting this error , how can i remove it?
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
i have attached my input files as pictures as the files are too big to upload ; input image is a dataset with stable and unstable system voltage and phase (vtg is from A:N and phase from O:AB) and the input image is an excel sheet with 5600 fault values and 5601:16801 are the normal stable system values
input2 image is the values to be tested and using kalman to find how close they are to the dataset
Thanks in advance
I also wanted to keep u to be a input like
src = dsp.SignalSource('SamplesPerFrame',1);
src.Signal = xlsread('D:\ieee14\trainingset.xlsx',1,'A1:AB1');
u = src.Signal;
but as it shows this error
Error using DynamicSystem/lsim (line 84)
When simulating the response to a specific input signal, the input data U must be a matrix with as many rows as samples in the time
vector T, and as many columns as input channels.
Error in kalman15thapril (line 36)
[out,x] = lsim(SimModel,[w,v]);

18 Comments

Nana - which line is generating the error? Please copy and paste the full error message to this question.
Notice that you are using row 5601 in both src1 and src2 . If the intention was that the two should not overlap then src2 would have to start at A5602
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in kalman15thapril (line 37)
[out,x] = lsim(SimModel,[],[w,v,u]);
Please show size(w), size(v), size(u)
Nana Fernandes
Nana Fernandes on 16 Apr 2017
Edited: Nana Fernandes on 16 Apr 2017
u is 6x28 double
w is 5600x28 double
v is 5601x28 double
You assigned a new value to u after the code you posted earlier.
You have different number of rows for each of the items, but you are trying to put them together as columns.
You are passing the [w,v,u] into the time input of lsim, which needs a vector.
You appear to be using discrete lsim . For that, t can be omitted or set to a empty array, but the u input (which you set to empty) would need to be an array with as many columns as there are inputs to the system. But perhaps you did omit t: in that case the [w,v,u] would be appearing in the x0 position, but x0 needs to be a vector.
how can i omit t? and the number of column are the same for all u v and w only the numbers of rows are changing
im sorry but i cant understand where i went wrong
Your SimModel appears to have three inputs and two outputs. The syntax for lsim is
lsim(sys,u,t)
"The input u is an array having as many rows as time samples (length(t)) and as many columns as system inputs. For instance, if sys is a SISO system, then u is a t-by-1 vector. If sys has three inputs, then u is a t-by-3 array. Each row u(i,:) specifies the input value(s) at the time sample t(i). The signal u also appears on the plot."
As you do have three inputs, your u needs to be something-by-3 .
As for t:
"The model sys can be continuous or discrete, SISO or MIMO. In discrete time, u must be sampled at the same rate as the system. In this case, the input t is redundant and can be omitted or set to an empty matrix."
As your model appears to be discrete, you could set t to [].
This would lead to code something like:
U3 = something with three columns derived from w, v and u;
[out,x] = lsim(SimModel, U3, []);
However, I have no idea which of your 28 columns of your three signals should be the inputs. Perhaps you should be using a system with 28 inputs.
Ok thanks
You assigned a new value to u after the code you posted earlier.
You have different number of rows for each of the items, but you are trying to put them together as columns.
You are passing the [w,v,u] into the time input of lsim, which needs a vector.
You appear to be using discrete lsim . For that, t can be omitted or set to a empty array, but the u input (which you set to empty) would need to be an array with as many columns as there are inputs to the system. But perhaps you did omit t: in that case the [w,v,u] would be appearing in the x0 position, but x0 needs to be a vector.
how can i assign x0 ? just assign a vector values? i defined u3 like u said above but i still get dimension mismatch error
A = [1.1269 -0.4940 0.1129;
1.0000 0 0;
0 1.0000 0];
B = [-0.3832;
0.5919;
0.5191];
C = [1 0 0];
Plant = ss(A,[B B],C,0,[] ,'inputname',{'u' 'w'},'outputname','y'); %changed the time here from -1 to []
Q = 1;
R = 1;
[kalmf,L,P,M] = kalman(Plant,Q,R);
M;
kalmf = kalmf(1,:);
a = A;
b = [B B 0*B];
c = [C;C];
d = [0 0 0;0 0 1];
P = ss(a,b,c,d,[],'inputname',{'u' 'w' 'v'},'outputname',{'y' 'yv'}); %changed -1 to [] here
sys = parallel(P,kalmf,1,1,[],[]);
SimModel = feedback(sys,1,4,2,1); % Close loop around input #4 and output #2
SimModel = SimModel([1 3],[1 2 3]); % Delete yv from I/O list
SimModel.InputName;
SimModel.OutputName;
src = dsp.SignalSource('SamplesPerFrame',1);
src.Signal = xlsread('D:\ieee14\trainingset.xlsx',1,'A1:AB1'); % changed this
u = src.Signal;
src1 = dsp.SignalSource('SamplesPerFrame',5600);
src1.Signal = xlsread('D:\ieee14\faultset.xlsx',1,'A1:AB5601');
w = src1.Signal;
src2 = dsp.SignalSource('SamplesPerFrame',5600);
src2.Signal = xlsread('D:\ieee14\faultset.xlsx',1,'A5601:AB11201');
v = src2.Signal;
t = [28:1]'; %changed this
[out,x] = lsim(SimModel,[w,v,u]);
i have made some changes in the code and written where all i have changed so its easier for you to understand
i still get the following error
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in kalman15thapril (line 38)
[out,x] = lsim(SimModel,[w,v,u]);
please help :)
Stephen23
Stephen23 on 17 Apr 2017
Edited: Stephen23 on 17 Apr 2017
Tell us exactly what these commands display:
size(u)
size(v)
size(w)
size(u) ans =
1 28
size(v) ans =
5601 28
size(w)
ans =
5601 28
Stephen23
Stephen23 on 17 Apr 2017
Edited: Stephen23 on 17 Apr 2017
@Nana Fernandes: the variables have different numbers of rows, so that cannot be concatenated horizontally. They do have the same number of columns, so they could be concatenated vertically:
[w;v;u]
I have no idea if that suits your algorithm though: you should check the input requirements for lsim.
thanks a lot solved a major part of my problem
i will give 28 inputs now as my columns are 28 and i will use vertical concatenation
but other than the three original inputs of mine i want to remove the rest
i know i can use the code below but can someone explain to me what and how it is done so i can implement it for 28 inputs
SimModel = SimModel([1 3],[1 2 3]);
for further reference
In your line
P = ss(a,b,c,d,[],'inputname',{'u' 'w' 'v'},'outputname',{'y' 'yv'}); %changed -1 to [] here
your b and d would have to have 28 columns (and you would want to add more items to the inputnames)
ok and to remove those additional inputs from my simmodel ? how can i do that?
I am not sure if I understand your question, but it seems to me you could extract appropriate columns from your input. For example,
U3 = [w(:,4), v(:,4), u(:,21)];
[out,x] = lsim(SimModel, U3, []);
As i am forfully adding 28 inputs so i can get the kalman filter to run , i would like to remove the extra 25 inputs from my input i the code given in mathwork documentation they removed ye input by using the below syntax
SimModel = SimModel([1 3],[1 2 3]); % Delete yv from I/O list
i cant understand what and how is the syntax taking out yv from the i/o list and i need help to understand it
i need the same help for the syntax you provided
U3 = [w(:,4), v(:,4), u(:,21)];
[out,x] = lsim(SimModel, U3, []);
if i can understand the syntax i can use it for my model

Sign in to comment.

Answers (0)

Asked:

on 15 Apr 2017

Edited:

on 18 Apr 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!