K1 =
MATLAB is deleting my negatives in matrix multiplication and i am confused
4 views (last 30 days)
Show older comments
Suhaani
on 6 Sep 2024
Commented: Walter Roberson
on 6 Sep 2024
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
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
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]
K1 = L1.' * k1 * L1
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
More Answers (1)
Walter Roberson
on 6 Sep 2024
Moved: Walter Roberson
on 6 Sep 2024
That code does not generally remove negatives.
k1 = randn(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]
K1 = L1.' * k1 * L1
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 = L1.' * k1 * L1
0 Comments
See Also
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!