I am having trouble constructing a 'for loop'. Can you suggest me on how to modify it?

1 回表示 (過去 30 日間)
ABHISHEK GHOSH
ABHISHEK GHOSH 2024 年 8 月 29 日
編集済み: Voss 2024 年 8 月 29 日
I have written the Matlab code based on the results I obtained. However, the code is not producing the expected result, and I am having trouble pinpointing my mistake. Can you please help me identify where I went wrong and assist me in correcting it?
I want to do a summation.
I211_sum=0;
I22_sum =0;
% Loop for I211
for k= 1: L
for i = 1:k
for m = 1: i-1
I211_sum = I211_sum ...
+ ( nchoosek(L,k) * ( (-1)^k ) * (exp(-H1*k) ) ...
* (exp(B)) * (P^-k) * nchoosek(k,i) * ((B/P)^i)...
* ( (exp(-D) * ( (factorial(m-1)/factorial(i-1)) * ((-A)^(i-1-m)) * ((E- (B/P))^(-m)) ) )...
+ ( ((-A)^(i-1)) * (1/factorial(i-1)) * ((exp(-(B*A)/ P))) * (expint(A * (E- (B/P)))) ) ) ) ;
end
end
end
I211(r) = I211_sum;
% Loop for I22
for k= 1: L
I22_sum = I22_sum + ( nchoosek(L,k) * ((-1)^k) * (P1^k) * (1-( exp(-F) )) * ((exp(-H*k))/(G^k)) ) ;
end
I22(r) = I22_sum;
  1 件のコメント
Voss
Voss 2024 年 8 月 29 日
Tip: You can use the nchoosek function to avoid having to write three separate factorial function calls.
Example:
L = 5;
k = 3;
factorial(L) / (factorial(k)*factorial(L-k) )
ans = 10
nchoosek(L,k)
ans = 10

サインインしてコメントする。

採用された回答

Voss
Voss 2024 年 8 月 29 日
編集済み: Voss 2024 年 8 月 29 日
One thing I notice is that
(-A)^i-1
should be
(-A)^(i-1)
EDIT: that's now corrected in the question.
  3 件のコメント
Voss
Voss 2024 年 8 月 29 日
編集済み: Voss 2024 年 8 月 29 日
And the code appears to have an extra *A here
* (exp(B)) * A * (P^-k)
% ^^^
that doesn't seem to be in the image.
EDIT: that's now corrected in the question.
ABHISHEK GHOSH
ABHISHEK GHOSH 2024 年 8 月 29 日
I follow your instructions and do modification accordingly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by