# Matlab Showing Imaginary Numbers as Real

70 views (last 30 days)

Show older comments

I'm running a code to solve a thermomechanics problem and the code that solves an equation sometimes output an imaginary and real value. I set up an if statement with the isreal command to weed out the imaginary answers and set a new value to the real part of the solution, and it worked on an example I knew the values for, however it is not working for a solution that the vpa for has an imaginary part. Any thoughts?

syms B

U = 3;

N = 4;

g = 10

e = [0 1 2 3];

S =solve(U==N*sum(e.*exp(-B.*e))./sum(exp(-B.*e)),B)

if isreal(S(1)) == 1

beta = S(1)

elseif isreal(S(2)) == 1

beta = S(2)

else fprintf("no real beta value")

end

When tested for S(1) specifically I get the following, which highlights my confusion.

>> isreal(S)

ans =

logical

1

>> vpa(S)

ans =

0.2123079473420932527729328757573 - 2.2897391314147019270086132884395i

##### 0 Comments

### Accepted Answer

dpb
on 9 Oct 2024 at 17:10

Edited: dpb
on 9 Oct 2024 at 21:03

syms B

U = 3;

N = 4;

g = 10;

e = [0 1 2 3];

B=double(solve(U==N*sum(e.*exp(-B.*e))./sum(exp(-B.*e)),B,Real=true))

Read the documentation/examples for solve, for <Name-Value Arguments>, 'Real' is the first one listed and is by default 'False'

##### 2 Comments

dpb
on 9 Oct 2024 at 18:25

### More Answers (2)

dpb
on 9 Oct 2024 at 14:53

syms B

U = 3;

N = 4;

g = 10

e = [0 1 2 3];

S =solve(U==N*sum(e.*exp(-B.*e))./sum(exp(-B.*e)),B)

whos S

S

isnumeric(S)

The problem is that isreal is not defined to handle a symbolic variable as its input -- an enhancement would be it should warn or the list of classes it responds to to always return false expanded to cover the symbolic object. Whoever designed/wrote it didn't have symbolic variables in mind it appears.

You don't want to test the variable S anyway, you want to test the numeric result.

##### 8 Comments

dpb
on 10 Oct 2024 at 16:08

I had thought the description in the doc for which

"which ___-all displays the paths to all items on the MATLAB path with the requested name, as well as any files in special folders that have been implicitly added to the path. Such items include methods of instantiated classes."

would have covered the case when the Symbolic TB was available, but obviously it didn't. I don't have it so my only experience is this specific thread...

Walter Roberson
on 9 Oct 2024 at 22:08

Edited: Walter Roberson
on 9 Oct 2024 at 22:10

syms B

U = 3;

N = 4;

g = 10

e = [0 1 2 3];

S =solve(U==N*sum(e.*exp(-B.*e))./sum(exp(-B.*e)), B, 'MaxDegree', 3)

if imag(S(1)) == 0

beta = S(1);

elseif imag(S(2)) == 0

beta = S(2);

elseif imag(S(3)) == 0

beta = S(3);

else

fprintf("no real beta value")

end

disp(char(beta))

##### 5 Comments

dpb
on 10 Oct 2024 at 17:32

Edited: dpb
on 10 Oct 2024 at 18:25

But since @Luke was looking for only real solutions, why do that instead of using the Real option is what I didn't follow the purpose of???

As this is my first and only foray into the Symbolic TB I am pretty much klewless as to using it...

### See Also

### Categories

Find more on

**Symbolic Variables, Expressions, Functions, and Preferences**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!