I do not know why am I getting imaginary numbers.
2 views (last 30 days)
Show older comments
%
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
0 Comments
Accepted Answer
James Tursa
on 16 Nov 2020
Edited: James Tursa
on 16 Nov 2020
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
James Tursa
on 16 Nov 2020
Edited: James Tursa
on 16 Nov 2020
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));
More Answers (0)
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!