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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get 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