現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.
Image Analyst
2013 年 4 月 12 日
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.
Image Analyst
2013 年 4 月 12 日
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 で Creating and Concatenating Matrices についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
