problem in symbolic matrix multiplication

4 次查看(过去 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.

采纳的回答

Walter Roberson
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));

更多回答(2 个)

Andrei Bobrov
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);
  1 个评论
Andrew
Andrew 2013-11-11
Thank you very much for your help. However to be honest, I did not understand your code. I am a beginner of matlab and your code, at first glance, seems complicated. This may be the way that professionals or experts use, however I am not one among them. But, I am trying to find out what you have done. Again, thanks!

请先登录,再进行评论。


Roger Stafford
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 个评论
Andrew
Andrew 2013-11-11
Making sr variable symbolic or double does not seem to make difference. Any ideas or suggestions?
Walter Roberson
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!

Translated by