MATLAB is deleting my negatives in matrix multiplication and i am confused

3 views (last 30 days)
Matlab is deleting my negatives and I am confused
I ran the same script with smaller matricies last week and it worked fine
[k1] = truss_stiffness(E,A, coords1);
L1 = [01 01 0 0 0 0 0 0;
0 0 01 01 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0]
K1 = L1.' * k1 *L1;
where k1 is a matrix with negatives and K1 does not

Accepted Answer

Abhishek Kumar Singh
Abhishek Kumar Singh on 6 Sep 2024
The matrix K1 should theoretically preserve any negative values present in the mathematical result of the multiplication. Without access to the actual matrix k1 or the function used to calculate it, pinpointing the exact issue is challenging. However, you can take the following steps to identify the source of the problem:
  • Check if L1 is inadvertently causing the negative values to be canceled out during the multiplication. This can happen if the structure of L1 leads to such a transformation.
  • If the matrices are very large or contain very small values, numerical precision issues might lead to unexpected results. Try the calculation after converting all variables to high precision using vpa function. Refer to the document to know how to do this: https://www.mathworks.com/help/symbolic/vpa.html?s_tid=doc_ta#buytdfn
  • Print out the matrices before and after multiplication to verify where the negatives might be getting lost:
disp('k1:');
disp(k1);
disp('L1:');
disp(L1);
disp('L1 Transpose * k1:');
disp(L1.' * k1);
disp('K1:');
disp(K1);
  • Analyze L1 to ensure it is not designed in a way that inherently cancels out negative values. Use MATLAB's debugging tools to step through the multiplication process and observe intermediate results.
Hope you identify the root cause of the issue.
  1 Comment
Walter Roberson
Walter Roberson on 6 Sep 2024
syms k1 [4, 4]
L1 = [01 01 0 0 0 0 0 0;
0 0 01 01 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0]
L1 = 4x8
1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
K1 = L1.' * k1 * L1
K1 = 
You can see that the effect is to duplicate the upper left entries, and set the rest to zero. There is no possibility of cancelation.
However, there is the possibility of nan output if any of the inputs are infinite
k1 = zeros(4,4); k1(end,end) = inf;
K1 = L1.' * k1 * L1
K1 = 8x8
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 6 Sep 2024
Moved: Walter Roberson on 6 Sep 2024
That code does not generally remove negatives.
k1 = randn(4, 4)
k1 = 4x4
0.6137 -0.0901 -0.6262 -1.4654 2.1271 1.0403 0.7863 -0.3323 -1.2374 1.0913 -1.7723 -1.1454 0.4974 -0.3905 0.3630 1.3331
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
L1 = [01 01 0 0 0 0 0 0;
0 0 01 01 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0]
L1 = 4x8
1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
K1 = L1.' * k1 * L1
K1 = 8x8
0.6137 0.6137 -0.0901 -0.0901 0 0 0 0 0.6137 0.6137 -0.0901 -0.0901 0 0 0 0 2.1271 2.1271 1.0403 1.0403 0 0 0 0 2.1271 2.1271 1.0403 1.0403 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
However, that code duplicates the upper left corner of the matrix and sets the rest to zero. If the negatives do not happen to be in the upper left corner, then they are going to be deleted.
k1 = - magic(4)
k1 = 4x4
-16 -2 -3 -13 -5 -11 -10 -8 -9 -7 -6 -12 -4 -14 -15 -1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
K1 = L1.' * k1 * L1
K1 = 8x8
-16 -16 -2 -2 0 0 0 0 -16 -16 -2 -2 0 0 0 0 -5 -5 -11 -11 0 0 0 0 -5 -5 -11 -11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!