Which one is the correct one to calculate

1 回表示 (過去 30 日間)
Yamina chbak
Yamina chbak 2021 年 7 月 1 日
コメント済み: Yamina chbak 2021 年 7 月 2 日
I tried both of those instruction to calculate as :
where and with are vectors ( zeros(15,1))
Both leads to different results, i don't know which one is the correct one , Can you hepl me ? .
R=0;
for j=1:L
s(j)=(1-q(j))/(1+q(j));
A(j)=4*sin(pi*alpha)*lamnda(j)/(pi*(1+q(j))^2);
R = R+A(j)*exp(-s(j)*dt);
end
% or
R=sum((4*sin(pi*alpha).*lambda/(pi.*(1+q)^2))*exp(-dt*(1-q)./(1+q)));
  1 件のコメント
Jan
Jan 2021 年 7 月 1 日
編集済み: Jan 2021 年 7 月 1 日
If you do not provide L, q, alpha, lamnda and dt, we cannot check your code.
Is "lamnda" a typo? In the 2nd formula it is "lambda".
"Both leads to different results" ist strange, because the 2nd fails with an error. Do you mean:
R=sum((4*sin(pi*alpha).*lambda ./ (pi.*(1+q).^2)).*exp(-dt*(1-q)./(1+q)));
% elementwise: ^ ^ ^

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

採用された回答

Jan
Jan 2021 年 7 月 1 日
編集済み: Jan 2021 年 7 月 1 日
R = 0;
L = 30; % Some educated guesses
q = rand(1, L);
lambda = rand(1, L);
alpha = rand;
dt = rand;
for j = 1:L
s(j) = (1 - q(j)) / (1 + q(j));
A(j) = 4 * sin(pi * alpha) * lambda(j) / (pi * (1 + q(j))^2);
R = R + A(j) * exp(-s(j) * dt);
end
R
R = 2.6454
% R2 = sum((4*sin(pi*alpha) .* lambda / ...
% (pi.*(1+q)^2)) * exp(-dt*(1-q) ./ (1+q)))
% Failing: Several operations must be made elementwise:
R2 = sum((4 * sin(pi * alpha) .* lambda ./ ...
(pi* (1 + q) .^ 2)) .* exp(-dt * (1 - q) ./ (1 + q)))
R2 = 2.6454
  1 件のコメント
Yamina chbak
Yamina chbak 2021 年 7 月 2 日
Thinks you Jan, it's correct

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by