problem in symbolic matrix multiplication

4 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Nov 2013
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));

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 11 Nov 2013
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 Comment
Andrew
Andrew on 11 Nov 2013
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!

Sign in to comment.


Roger Stafford
Roger Stafford on 10 Nov 2013
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 Comments
Andrew
Andrew on 11 Nov 2013
Making sr variable symbolic or double does not seem to make difference. Any ideas or suggestions?
Walter Roberson
Walter Roberson on 11 Nov 2013
What error message is it that you are getting for the first situation?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!