Clear Filters
Clear Filters

Matlab Showing Imaginary Numbers as Real

70 views (last 30 days)
Luke
Luke on 9 Oct 2024 at 14:40
Edited: dpb on 10 Oct 2024 at 18:25
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

Accepted Answer

dpb
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))
B = 0.6740
Read the documentation/examples for solve, for <Name-Value Arguments>, 'Real' is the first one listed and is by default 'False'
  2 Comments
Luke
Luke on 9 Oct 2024 at 17:15
You, sir/ma'am, are amazing. Thank you!
dpb
dpb on 9 Oct 2024 at 18:25
Well, not so much me on this one...I needed a break from the panic I'm working on elsewhere so thought I might as well take a look into the TB documenation...I figured there had to be some way, I just didn't know it having never had the TB to use/learn...

Sign in to comment.

More Answers (2)

dpb
dpb on 9 Oct 2024 at 14:53
syms B
U = 3;
N = 4;
g = 10
g = 10
e = [0 1 2 3];
S =solve(U==N*sum(e.*exp(-B.*e))./sum(exp(-B.*e)),B)
whos S
Name Size Bytes Class Attributes S 1x1 8 sym
S
isnumeric(S)
ans = logical
0
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
Paul
Paul on 10 Oct 2024 at 15:21
The sym method is called.
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);
which isreal(S)
/MATLAB/toolbox/symbolic/symbolic/@sym/sym.m % sym method
I checked which and I guess the sym method doesn't show up on the -all list because it's buried in a file that's not named isreal.m?
dpb
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...

Sign in to comment.


Walter Roberson
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
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))
log((2^(1/3)*(581 - 9*3867^(1/2))^(1/3))/9 + (2^(1/3)*(9*3867^(1/2) + 581)^(1/3))/9 + 1/9)
  5 Comments
dpb
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...
Walter Roberson
Walter Roberson on 10 Oct 2024 at 18:25
It is just a different approach.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!