Problem with "Conversion to logical from sym is not possible."

조회 수: 12 (최근 30일)
Chris Poffel
Chris Poffel 2017년 1월 4일
편집: Walter Roberson 2017년 1월 9일
Hello, i am using a really simple example for this problem, which I just can't get to work. I tried most of the examples which where suggested in similiar topics. I defined this function:
function [FDInt] = mg(x)
if x<0.8686
FDInt = 1/(0.27+exp(-x));
else
FDInt = (4/(3*sqrt(pi)))*(x^2 + pi^2/6)^(3/4);
end
end
and I call it here:
syms EF
solve(mg(EF)==10^20, EF)
I get this error message: "Error in mg (line 3) if x<0.8686 " I tried using vpa(x) or double(x) without working. I think the solution should be really easy - however, I just can't get it to work. If someone could give me a hint, that would be nice :-)

채택된 답변

Star Strider
Star Strider 2017년 1월 4일
There seems to be only one value that meets that criterion. Just use an anonymous function and fzero:
mg = @(x) (x<0.8686).*(1./(0.27+exp(-x))) + (x>=0.8686).*((4./(3*sqrt(pi)))*(x.^2 + pi^2/6).^(3/4));
EF = fzero(@(x) mg(x)-1E+20, 1)
EF =
26.0470e+012

추가 답변 (2개)

Walter Roberson
Walter Roberson 2017년 1월 4일
You are passing a symbolic variable into a function that tests the value with < in the context of an if statement. That is not permitted, because if requires a definite decision and you cannot make a definite decision about whether a symbolic variable has a particular relationship or not.
You can use piecewise() if you have a new enough MATLAB, or you can code with the form that Star Strider shows (but that form can fail if the unselected expression turns out to be infinite)
  댓글 수: 2
Chris Poffel
Chris Poffel 2017년 1월 5일
In Versiion 2015b, I can't find this function. Was it implemented in a 2016-version?
Chris Poffel
Chris Poffel 2017년 1월 5일
편집: Walter Roberson 2017년 1월 9일
damn. Time to upgrade then. As my argument will be a vector, this would probably the best choice. Thanks

댓글을 달려면 로그인하십시오.


Karan Gill
Karan Gill 2017년 1월 9일
Since you MATLAB isn't new enough to have piecewise, you can use heaviside as a substitute for piecewise.
FDInt = 1/(0.27+exp(-x))*heaviside(x-0.8686) + (4/(3*sqrt(pi)))*(x^2 + pi^2/6)^(3/4)*heaviside(0.8686-x)
But yes, using piecewise is simplest if you can upgrade :)
Karan (Symbolic doc)

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by