Which one is the correct one to calculate

1 次查看(过去 30 天)
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 中查找有关 Logical 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by