Matlab Showing Imaginary Numbers as Real

3 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2024-10-9
编辑:dpb 2024-10-9
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 个评论
Luke
Luke 2024-10-9
You, sir/ma'am, are amazing. Thank you!
dpb
dpb 2024-10-9
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...

请先登录,再进行评论。

更多回答(2 个)

dpb
dpb 2024-10-9
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 个评论
Paul
Paul 2024-10-10
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 2024-10-10
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
Walter Roberson 2024-10-9
编辑:Walter Roberson 2024-10-9
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 个评论
dpb
dpb 2024-10-10
编辑:dpb 2024-10-10
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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Preferences 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by