I do not know why am I getting imaginary numbers.

%
clc
close all
clear all
format short
%This is for calculating the entropy with different x and T
kb=1.38064852e23 %Jk^-1 %Boltzmann Constant
x=0:0.01:0.5;
for i=1:length(x)
Sm(i)=-kb*((i*log(i)) + ((1-i)*log(1-i)));
end
MixEn=Sm'; %transpose to fit in a nice table)
T=table(MixEn)
%
My code gives a complex number and I do not know why. I belive it is supposed to be only a real number. I have calulated it using a calculator and it is a real number but with Matlab if gives a complex number.
Workspace:
kb =
1.3806e+23
T =
51×1 table
MixEn
_______________________
NaN+0i
-1.914e+23-4.3374e+23i
-2.6364e+23-8.6749e+23i
-3.1055e+23-1.3012e+24i
-3.4544e+23-1.735e+24i
-3.7324e+23-2.1687e+24i
-3.9636e+23-2.6025e+24i
-4.1615e+23-3.0362e+24i
-4.3345e+23-3.4699e+24i
-4.4883e+23-3.9037e+24i
-4.6265e+23-4.3374e+24i
-4.7522e+23-4.7712e+24i
-4.8674e+23-5.2049e+24i
-4.9737e+23-5.6387e+24i
-5.0724e+23-6.0724e+24i
-5.1645e+23-6.5062e+24i
-5.2509e+23-6.9399e+24i
-5.3322e+23-7.3736e+24i
-5.4089e+23-7.8074e+24i
-5.4816e+23-8.2411e+24i
-5.5507e+23-8.6749e+24i
-5.6164e+23-9.1086e+24i
-5.6792e+23-9.5424e+24i
-5.7393e+23-9.9761e+24i
-5.7968e+23-1.041e+25i
-5.852e+23-1.0844e+25i
-5.9052e+23-1.1277e+25i
-5.9563e+23-1.1711e+25i
-6.0056e+23-1.2145e+25i
-6.0532e+23-1.2579e+25i
-6.0993e+23-1.3012e+25i
-6.1438e+23-1.3446e+25i
-6.187e+23-1.388e+25i
-6.2288e+23-1.4314e+25i
-6.2694e+23-1.4747e+25i
-6.3089e+23-1.5181e+25i
-6.3472e+23-1.5615e+25i
-6.3845e+23-1.6049e+25i
-6.4209e+23-1.6482e+25i
-6.4563e+23-1.6916e+25i
-6.4908e+23-1.735e+25i
-6.5245e+23-1.7783e+25i
-6.5574e+23-1.8217e+25i
-6.5895e+23-1.8651e+25i
-6.6209e+23-1.9085e+25i
-6.6515e+23-1.9518e+25i
-6.6816e+23-1.9952e+25i
-6.7109e+23-2.0386e+25i
-6.7397e+23-2.082e+25i
-6.7679e+23-2.1253e+25i
-6.7955e+23-2.1687e+25i

 Accepted Answer

Look at the following for the values of i you are using:
log(1-i)
E.g.,
>> log(-1)
ans =
0.0000 + 3.1416i

7 Comments

Thank you. But my 'i' values range from 0 to 0.5 as detected from 'x'. If my 'i' is wrong or I am not using in the right way, I do not how to fix that. I need the for loop to run Sm for 0:0.01:0.5. That is what it's doing right?
No, your i values range from 1 to length(x)
Maybe if you post the equation you are trying to code we can help you with the correct syntax. Maybe you mean x(i) and 1-x(i) instead of the i and 1-i you are currently using.
Sm(x) = −kb[xlnx + (1 − x)ln(1 − x)].
where kb is Boltzman constant and x can range from 0-1.
That is the equation.
I tried using x(i) and got real numbers, but I also tried abs(log(x)) and also go real numbers. Both real numbers were different for a particular x. So I do not know which method is correct.
Thank you.
Well, since the equation calls for x, I think you need to be using x in your code:
Sm(i)=-kb*((x(i)*log(x(i))) + ((1-x(i))*log(1-x(i))));
That being said, realize that x(1) is 0, so log(x(1)) is going to generate a -inf value. And when you multiply that by 0 you will get a NaN for the Sm(1) spot.
Okay, thank you for your help.
Note that if that is the only thing going on in your loop, you don't need a loop for this. Just use element-wise operators where needed. E.g.,
Sm = -kb*(x.*log(x) + (1-x).*log(1-x));

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 16 Nov 2020

Edited:

on 16 Nov 2020

Community Treasure Hunt

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

Start Hunting!