Filter löschen
Filter löschen

Which one is the correct one to calculate

1 Ansicht (letzte 30 Tage)
Yamina chbak
Yamina chbak am 1 Jul. 2021
Kommentiert: Yamina chbak am 2 Jul. 2021
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 Kommentar
Jan
Jan am 1 Jul. 2021
Bearbeitet: Jan am 1 Jul. 2021
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: ^ ^ ^

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 1 Jul. 2021
Bearbeitet: Jan am 1 Jul. 2021
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

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by