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
0 个评论
采纳的回答
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))
Read the documentation/examples for solve, for <Name-Value Arguments>, 'Real' is the first one listed and is by default 'False'
2 个评论
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
2024-10-9
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 个评论
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
2024-10-9
编辑:Walter Roberson
2024-10-9
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 个评论
另请参阅
类别
在 Help Center 和 File 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!