problem in symbolic matrix multiplication

조회 수: 4 (최근 30일)
Andrew
Andrew 2013년 11월 10일
댓글: Andrew 2013년 11월 11일
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