显示 更早的评论
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.
采纳的回答
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.
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,
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.
@ 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.
@ 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 个)
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)
