problem in symbolic matrix multiplication
5 ビュー (過去 30 日間)
古いコメントを表示
Hi, I am performing simple multiplication on two matrices. However getting error while doing so. I am not able to understand what is error and where I am going wrong. Therefore I am not able to correct error. Can someone provide any ideas or insights? here is my code:
clear all;
clc;
syms xlc
%sr=sym([]);
sr=zeros(4,1);
B=[ 0.64*xlc - 1.2, 1.6 - 1.28*xlc, 0.64*xlc - 0.4; 0.64*xlc - 2.8, 4.8 - 1.28*xlc, 0.64*xlc - 2.0; 0.64*xlc - 4.4, 8.0 - 1.28*xlc, 0.64*xlc - 3.6; 0.64*xlc - 6.0, 11.2 - 1.28*xlc, 0.64*xlc - 5.2];
d=1.0e-03 *[0.000000000095833;0.058593750095833; 0.109375000095833; 0.152343750095834; 0.187500000095834; 0.214843750095834; 0.234375000095834; 0.246093750095834; 0.250000000095834];
e_n=[1,2, 3; 3, 4, 5; 5, 6, 7; 7, 8, 9];
for i=1:1:4
for j=1:1:3
I = e_n(i,j);
sr(i)=sr(i)+(B(i,j)*d(j));
end
end
What I am doing: I want to multiply
sr(1)=B(1,1)*d(1)+B(1,2)*d(2)+B(1,3)*d(3)
sr(2)=B(2,1)*d(3)+B(2,2)*d(4)+B(2,3)*d(5)
and so on. But matlab is giving errors. Any help or ideas are welcome. Thanks.
0 件のコメント
採用された回答
Walter Roberson
2013 年 11 月 11 日
You initialize sr as empty, so sr(i) does not exist yet to be indexed by "i" for each different "i" value.
sr = sym(zeros(4,1));
0 件のコメント
その他の回答 (2 件)
Andrei Bobrov
2013 年 11 月 11 日
syms xlc
B=[ 0.64*xlc - 1.2, 1.6 - 1.28*xlc, 0.64*xlc - 0.4; 0.64*xlc - 2.8, 4.8 - 1.28*xlc, 0.64*xlc - 2.0; 0.64*xlc - 4.4, 8.0 - 1.28*xlc, 0.64*xlc - 3.6; 0.64*xlc - 6.0, 11.2 - 1.28*xlc, 0.64*xlc - 5.2];
d=1.0e-03 *[0.000000000095833;0.058593750095833; 0.109375000095833; 0.152343750095834; 0.187500000095834; 0.214843750095834; 0.234375000095834; 0.246093750095834; 0.250000000095834];
ii = bsxfun(@plus,1:size(B,2),2*(0:size(B,1)-1)');
sr = sum(B.*d(ii),2);
Roger Stafford
2013 年 11 月 10 日
It isn't clear whether you are receiving a matlab error message or are getting erroneous results. If it is the latter the line in the inner for-loop should be changed to:
sr(i)=sr(i)+(B(i,j)*d(I));
3 件のコメント
Walter Roberson
2013 年 11 月 11 日
What error message is it that you are getting for the first situation?
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!