이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
I need to find the sum of few terms in binomial expansion...more precisely i need to find the sum of this expression:
∑ (nCr) * p^r * q^(n - r)
and limits for summation are from r = 2 to 15. and n=15
any help would be most welcome.
채택된 답변
Ahmed A. Selman
2013년 4월 11일
편집: Ahmed A. Selman
2013년 4월 11일
The general steps to find such a summation are:
- Start a loop over r,
- Calculate each term as a function of (r),
- In the loop, add the terms one by one to a unique matrix,
- After the loop is finished, sum over the added terms.
The code should be something as :
% ∑ (nCr) * p^r * q^(n - r)
clc; clear all
p =...;
q =...;
n = 15;
i = 0;
for r = 2:15
i = i+1;
nCr = ...;% calculate the coefficients here
Terms(i) = nCr * p.^r .* q.^(n-r);
end
SumOfTerms = sum(Terms)
The output is in (SumOfTerms), which should be a single value.
I assumed that (nCr) is not a constant, as I expect, it must be a function of (n and r). If it was a constant of (n and r), then define it outside the loop.
I also didn't understand the meaning of ( *| ) at the beginning and end of the formula in your question. If they mean some operation s ( complex conjugate or absolute) then add any of the commands at the very end of the code:
finalSum = conj(SumOfTerms);% for complex conjugate value.
or
finalSum = real(SumOfTerms);% for real value.
and in this case the output will be in (finalSum).
If you tried it and it didn't work, please let me know in which line it made an error, and the error message.
댓글 수: 11
Thank you very much for your ans. Mr.Selman,but there is still a problem.
*| is nothing in code (just typo)...please ignore that.
and i think nCr is calculated in matlab using nchoosek(n,k) command and when i used it in place of nCr (inside the for loop)in the code u written as nchoosek(15,r) matlab is giving Error as:
Error: The expression to the left of the equals sign is not a valid target for an assignment.
Any idea?
I only added the command for nCr and it worked fine as:
...
p=...;% works with p=0, 1, 2, -1
q=...;% works with q=0, 1, 3, 0
...
nCr = nchoosek(15,r);
...
As a matter of fact it is also expandable to a loop over n and r, keeping in mind that there is a restriction of (n-r) >= 0. So, let's try this:
clc
clear
p=1;
q=3;
i=0;
nTerms=zeros(15,15);
rTerms=zeros(1,15);
for r = 2:15
i = i+1;
for n=r:15;
nCr = nchoosek(n,r);
nTerms(i,n) = nCr * p.^r .* q.^(n-r);
end
rTerms(r)=sum(nTerms(:,n));
end
SumOfAllTerms = sum(rTerms);% Sum with condition (n-r)>=0
n15_Sum=sum(nTerms(:,15));% Only at r=2:15 and n=15
also works, error-free.
a
2013년 4월 12일
thanks Mr. Selman for your time..It may be lame thing to ask but please tell me that this is a script.right?
and when i have copy and paste the code in my command window and pressed Enter, the Command Window is blank with no response and Yes there is no error.
Though I am sure it might have worked on your side but on mine side the Command Window is blank.
regards,
a
2013년 4월 12일
The command window is not showing anything after pressing the Enter Key.
please help.
regards.
Paste it into a new editor window and run it from there, not the command line. The command line is mostly for running single statements, not huge groups of statements.
a
2013년 4월 12일
@ Image Analyst : I did the same...but my Command Window is blank.
Look in the variable editor, or use fprintf(), or take the semicolon off the end of the lines.
@ Image Analyst : Thank You Sir.
I am new to Matlab, so i did not inspect the Workspace.My answer is listed there in Workspace.I was only bothering the behavior of Command Window.
Thanks.
regards.
a
2013년 4월 12일
@ Ahmed A Selman:
thank You Mr. Ahmed A Selman for helping me out.
Regards,
@ Ahmed A. Selman
Sir, the code you provided works well...Can you please explain me how this loop is executing?
추가 답변 (1개)
Roger Stafford
2013년 4월 11일
With that range of r I would think it would be more efficient to compute
(q+p)^n-q^n-n*q^(n-1)*p
카테고리
도움말 센터 및 File Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
