problem in symbolic matrix multiplication

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.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Nov. 2013

1 Stimme

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

Weitere Antworten (2)

Andrei Bobrov
Andrei Bobrov am 11 Nov. 2013

1 Stimme

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 Kommentar

Andrew
Andrew am 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!

Melden Sie sich an, um zu kommentieren.

Roger Stafford
Roger Stafford am 10 Nov. 2013

0 Stimmen

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 Kommentare

Andrew
Andrew am 10 Nov. 2013
Bearbeitet: Andrew am 10 Nov. 2013
Thanks Roger Stafford for your comment. Even if I change inner loop line to
sr(i)=sr(i)+(B(i,j)*d(I));
there is error. When I just put these lines
sr(i)+(B(i,j)*d(I))
for i=I=1, matlab gives proper result. But as soon as I do
sr(1) = sr(i)+(B(i,j)*d(I))
for i=I=1, matlab gives error.
However, I tried to solve this problem with another way,
clear all;
clc;
syms xlc
sr=sym([]); %MODIFICATION
%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(I));
end
end
I made sr a symbolic array. And there is another error "Index exceeds matrix dimensions." Can someone comment on this? Thanks.
Andrew
Andrew am 11 Nov. 2013
Making sr variable symbolic or double does not seem to make difference. Any ideas or suggestions?
Walter Roberson
Walter Roberson am 11 Nov. 2013
What error message is it that you are getting for the first situation?

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 10 Nov. 2013

Kommentiert:

am 11 Nov. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by