log function returning complex answers where there should be none

11 ビュー (過去 30 日間)
Sandra Levin
Sandra Levin 2020 年 2 月 25 日
回答済み: Priyanshu Mishra 2020 年 2 月 28 日
So I have a 22*9 matrix. with 22 regions and 9 years. I am doing a decomposition analysis where need to calculate different factors contribution to the overalla change in total carbon emissions, between each set of years, for each region.
For some reason however, my code returns complex answers (for both Ln(r,m) and DeltaCO2CI) when there should only be real numbers. Can't figure out what i have done wrong?
here's the problematic part of the code:
DeltaCO2CI=zeros(21,9);
for r=2:22
for m=2:9
Ln(r,m)=(co2r(r,m)-co2r(r,m-1))./(log(co2r(r,m)-co2r(r,m-1))));
DeltaCO2CI(r,m)=sum((Ln(r,m)).*log(CI(r,m)./CI(r,m-1))));
end
end
  2 件のコメント
Adam
Adam 2020 年 2 月 25 日
Have you broken it down on command line to work out which component is yielding the complex number? Put a breakpoint in and put each component onto the command line to pin down which is complex. Just looking at the code I have no idea, especially as I have no idea what is in the variables being referenced.
Stephen23
Stephen23 2020 年 2 月 25 日
"Can't figure out what i have done wrong?"
This operation returns one or more negative values:
co2r(r,m)-co2r(r,m-1)

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

回答 (1 件)

Priyanshu Mishra
Priyanshu Mishra 2020 年 2 月 28 日
Hi Sandra,
The complex values might be coming because value of co2r(r,m)-co2r(r,m-1) is negative. I would suggest you to put a break point and debug why these values are coming negative.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by