K1 =
MATLAB is deleting my negatives in matrix multiplication and i am confused
4 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Abhishek Kumar Singh
2024-9-6
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 个评论
Walter Roberson
2024-9-6
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
更多回答(1 个)
Walter Roberson
2024-9-6
移动:Walter Roberson
2024-9-6
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!