why would a complex number appear when taking the natural log of real elements in a vector column?

I'm running a monte carlo simulation of an rbc model and as is normal it generates vector columns (each with 10,500 elements) containing the values of the modelled variables.
However, when taking the natural log of the elements in one of the vectors, there is one element that outputs as a complex number - even though its input is a real number. This messes up the calculation of subsequent moments.
When I take the original vector and calculate the nl of the array in another package there isn't any strange outputs, so I suspect it may be something to do with Matlab itself.
Has anyone come across a similar problem?
Thanks, Stefan

3 Comments

It would be a good idea to check and post the value of the concerned element. This would reveal the cause very fast.
Even i am facing the same problem, As @Sean de Wolski answered, the numbers here are non-negative but still the log value is having zero imag value, when I run it individually or smaller set, I don't see this issue. What is the reason for this ?
@Sasi: please UPLOAD some sample data in a mat file by clicking the paperclip button. Then it will be much much much easier to check what is happening (exactly as Jan wrote ten years ago).
We cannot run code in screenshots, nor can we use any data that you "show" in screenshots. Screenshots are not a data exchange file format. If you want help please show code as text and upload data in a MAT file.

Sign in to comment.

 Accepted Answer

log(-2)
The natural log of anything negative will create a complex number even if it is real.

More Answers (0)

Categories

Asked:

on 18 Jul 2012

Edited:

on 30 Nov 2023

Community Treasure Hunt

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

Start Hunting!