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!