Matrix subtraction within Matlab

I am generating a 3X1 matrix and name it 'xT'. Simultaneously I am generating another 3X1 matrix and name it 'x'. When I am computing the difference between these two i.e. Er = xT - x within Matlab, the answer is different than individual subtraction. Therefore when I am subtracting xT(2,1) - x(2,1), I am getting a different answer when Matlab is computing the Er. Please help.

7 Comments

Illustrate.
Is this way, are you trying?
xT=randi(10,3,1) % Random integer 3x1 mat
x=randi(20,3,1) % % Random integer 3x1 another mat
Er=xT-x
Command Window:
xT =
7
8
3
x =
1
3
16
Er =
6
5
-13
The xT is generated as follows:
%Update the true state
xt = phi*xt;
xt11(T) = xt(1,1);
xt21(T) = xt(2,1);
xt31(T) = xt(3,1);
and x is generated by
x = xA*pKAp + xB*pKBp + xC*pKCp;
x11(T) = x(1,1);
x21(T) = x(2,1);
x31(T) = x(3,1);
This cycle runs at every instance T. At the end of every cycle,
I derive the Er as follows:
Er = xt - x;
Er11(T) = Er(1,1);
Er21(T) = Er(2,1);
Er31(T) = Er(3,1);
The problem is when I carry a physical difference for example xt(2,1) - x(2,1), they do not match with Er which is xt-x.
When I run my code, at instance T = 230, the xt is as follows:
xt =
1.0e+04 *
1.0869
-0.0000
-0.0001
and
x =
1.0e+04 *
1.0869
0.0000
-0.0001
If I exract the xt(2,1) from this, I get
xt(2,1)
ans =
-0.3000
When I extract x(2,1), I get
x(2,1)
ans =
0.3174
Therefore a manual subtraction gives:
xt(2,1)-x(2,1)
ans =
-0.6174
But the Matlab calculation of Er, which is xt - x, gives
Er(2,1)
ans =
-0.0674
How is that possible?
James Tursa
James Tursa on 29 Aug 2019
Edited: James Tursa on 29 Aug 2019
It isn't possible, of course. You are either not doing the coding the way you think you are, or you are misinterpreting the displayed results. Instead of "describing" to us what you think you are doing, just copy & paste the actual code and actual output. If you need to attach a mat file for the values, do that as well. You don't provide enough digits in your post for us to guess what is going on.
% Generate the state estimate
x = xA*pKAp + xB*pKBp + xC*pKCp;
x11(T) = x(1,1);
x21(T) = x(2,1);
x31(T) = x(3,1);
% Generate the error
Er = xt - x;
Er11(T) = Er(1,1);
Er21(T) = Er(2,1);
Er31(T) = Er(3,1);
%Update the true state
xt = phi*xt;
xt11(T) = xt(1,1);
xt21(T) = xt(2,1);
xt31(T) = xt(3,1);
This is exactly what I am doing. While the Er(3,1) matches with manual subtraction xt(3,1)-x(3,1), the Er(2,1) doesn't match with xt(2,1)-x(2,1).
Please see the attached graph (file) which shows the difference between the two calculations.
Stephen23
Stephen23 on 29 Aug 2019
Edited: Stephen23 on 29 Aug 2019
@Monish Sengupta : please also upload your data in a .mat file.
Not in a text file, an excel file, or anything else. In a .mat file.
We cannot check your results without your data.
I have no uploaded the .mat file.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 29 Aug 2019
Edited: John D'Errico on 29 Aug 2019
whos
Name Size Bytes Class Attributes
E 2x1 16 double
E11 1x1 8 double
E21 1x1 8 double
Er 3x1 24 double
Er11 1x435 3480 double
Er21 1x435 3480 double
Er31 1x435 3480 double
ErA 3x1 24 double
ErA11 1x434 3472 double
ErA21 1x434 3472 double
ErA31 1x434 3472 double
ErB 3x1 24 double
ErB11 1x434 3472 double
ErB21 1x434 3472 double
ErB31 1x434 3472 double
ErC 3x1 24 double
ErC11 1x434 3472 double
ErC21 1x434 3472 double
ErC31 1x434 3472 double
H 2x3 48 double
I 3x3 72 double
InA 2x1 16 double
InA11 1x434 3472 double
InA21 1x434 3472 double
...
This, by the way, is why you DON'T ever want to create such reams of numbered variables. That is incredibly bad programming style, as it will cuse you to make mistake after mistake, as you type the wrong variable name. LEARN TO USE VECTORS. Better, learn to use arrays.
Regardless, you only think it did something different, probably because of the many different variable names, and possibly because you are using a short display format.
format long g
>> x
x =
10869.0885676116
0.228888860503846
-1.08890062860864
xt
xt =
10869.05
-0.299999999999384
-1.1
xt - x
ans =
-0.0385676116566174
-0.528888860503229
-0.0110993713913601
xt(2) - x(2)
ans =
-0.528888860503229
The results will be identical. The only problem is that sometimes when you display a vector as a result, it might get displayed so it looks differently, especially if you are only showing 4 digits.
A frequent and similar mistake made by new users at MATLAB, is they will display the numbers using format short, as:
format short
>> x
x =
1.0e+04 *
1.0869
0.0000
-0.0001
>> xt
xt =
1.0e+04 *
1.0869
-0.0000
-0.0001
and then either miss the 1.0e+4 at the top, or they will think those numbers are exactly what they see, then do a subtraction by hand.
Sorry, but these are NOT MATLAB mistakes. It is a user error, with 100% probability. I won't even call it probable user error, as that would imply some vague chance that it was not a user error. As you develop better skills at MATLAb, you will learn to make better use of your variables, but you will also learn to go more slowly, to check your work more carefully.

1 Comment

Thank you for your helpful comments. I have managed to find the problem.

Sign in to comment.

Asked:

on 29 Aug 2019

Commented:

on 29 Aug 2019

Community Treasure Hunt

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

Start Hunting!