# log function returning complex answers where there should be none

7 views (last 30 days)
Sandra Levin on 25 Feb 2020
Answered: Priyanshu Mishra on 28 Feb 2020
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 CommentsShowHide 1 older comment
Stephen23 on 25 Feb 2020
"Can't figure out what i have done wrong?"
This operation returns one or more negative values:
co2r(r,m)-co2r(r,m-1)

Priyanshu Mishra on 28 Feb 2020
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.