Element multiplication and subtraction

조회 수: 2 (최근 30일)
CD
CD 2019년 3월 26일
댓글: Adam Danz 2019년 3월 26일
I'd like to run the following though I keep getting element multiplication/division errors:
Ron = 0.012;
RL = 0.003;
R = 6.667;
D = 0:0.001:.999;
M = (D/(1-D))*(1/( (2*Ron*D/(R*((1-D)^2)))+1+(RL/R)*(1/((1-D)^2)) ))
plot (D,M);
I've several permutations of placing dots in front of multiplication, division and raise to the power signs though I just cannot seem to get this to work.
How should "M" be written so that Matlab will accept it?
Thank you

채택된 답변

Adam Danz
Adam Danz 2019년 3월 26일
편집: Adam Danz 2019년 3월 26일
This line below avoids error but that doesn't mean it does what it is intended to do.
%(D/(1-D))*(1 /( (2*Ron*D/(R*((1-D) ^2)))+1+(RL/R)*(1 /((1-D) ^2)) )) %old version
M = (D/(1-D))*(1./( (2*Ron*D/(R*((1-D).^2)))+1+(RL/R)*(1./((1-D).^2)) )) %new version
% _ _ _ _ %underline differences
James Tursa's solution is also error-free (copied below) but produces totally different results.
M2 = (D./(1-D)).*(1./( (2.*Ron.*D./(R.*((1-D).^2)))+1+(RL./R).*(1./((1-D).^2)) ))
isequal(M,M2)
%ans =
% logical
% 0
This is why it's critical to understand what the equation is supposed to do. I haven't put much time into making sense of it so maybe James' version is the correct version or maybe neither answers are correct.

추가 답변 (2개)

James Tursa
James Tursa 2019년 3월 26일
편집: James Tursa 2019년 3월 26일
If you are unsure, then just put dots next to every * and / and ^ operation. If a scalar happens to be involved it will still work as expected. E.g.,
M = (D./(1-D)).*(1./( (2.*Ron.*D./(R.*((1-D).^2)))+1+(RL./R).*(1./((1-D).^2)) ))
  댓글 수: 3
James Tursa
James Tursa 2019년 3월 26일
편집: James Tursa 2019년 3월 26일
"... is a bad approach ..."
I will respectfully disagree on this one. Just looking at the variables there was obviously only one vector, namely D. So IMO it was quite likely he was just looking for element-wise operations in calculating M ... he was just unsure where to put the dots. Again, I am talking about this particular post only!
Adam Danz
Adam Danz 2019년 3월 26일
You took the time to realize that and it was a good call. My caution is against the preface "If you are unsure...". I don't think it is a good idea to throw a dot in, test it, and if there's no error, accept it without being certain that it's the correct correction. My answer is a good example of that.

댓글을 달려면 로그인하십시오.


CD
CD 2019년 3월 26일
The second solution (M2) matches my results via excel.
Thank you!
  댓글 수: 1
Adam Danz
Adam Danz 2019년 3월 26일
편집: Adam Danz 2019년 3월 26일
Ok, good! Don't forget to accept his answer. Having a set of expected results helps to decide if you're equation is correct. Otherwise, avoid throwing in dots just to avoid error and assuming the calculation is correct.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by