Converting Anonymous Function to a Symbolic Function
이전 댓글 표시
Hello,
I have an anonymous function that looks like this (v=velocity, t=time):
v = @(t) exp(1).^(sin(t)) - 1;
and I want to turn it into a symbolic function. How can I achieve this?
채택된 답변
추가 답변 (2개)
v = @(t) exp(1).^(sin(t)) - 1;
str2sym(char(v)) %r2017b or later
댓글 수: 1
doesnt work with nested functions ... :(
ie
www = @(t) t+1
yyy = @(t) www(t)+1
madhan ravi
2019년 3월 29일
편집: madhan ravi
2019년 3월 29일
This turns the function handle argument as symbolic variable as well as the function handle into a symbolic function:
V=func2str(v);
z=regexp(V,'[^()]*','match');
syms(regexp(z{2},'\,','split'))
str2sym(regexp(V,'(?<=[\)])\S*','match')) % requires 2017b or later
sym(regexp(V,'(?<=[\)])\S*','match','once')) % prior to 2017b (haven't tested)
댓글 수: 7
Michel Bertrand
2020년 10월 9일
just fine. On Matlab 2019b, MAcOsX Catalina. Thanks
Pho Hale
2021년 4월 23일
This is the only one that works for me on 2021a, macOS
Walter Roberson
2021년 4월 23일
Adam's answer https://www.mathworks.com/matlabcentral/answers/449489-converting-anonymous-function-to-a-symbolic-function#answer_364896 works fine for me on Mac in R2021a -- recognizing that exp(1) is going to be treated numerically and give an approximation, and will not be automatically rewritten in terms of the exp() function.
%at = 1;
%bt = 1;
w = @(k,T) log(1 - exp(-k.^2 + 1/T)).*(at./k + bt*k)
a = @(T) exp(2);
b = @(T) exp(3);
W1 = @(T) integral(@(k) w(k,T), a(T), b(T) )
W = sym(W1)
I am trying to conver W1 into a symbolic function tried str2sym as well. It is not working eitherways
Walter Roberson
2023년 12월 30일
You cannot convert calls to integral() into symbolic calls -- not unless you do text manipulation.
I recommend that you give up on this.
The problem occurs well before the sym call.
%at = 1;
%bt = 1;
w = @(k,T) log(1 - exp(-k.^2 + 1/T)).*(at./k + bt*k)
try % Using try/catch so I can evaluate other code later in this comment
w(1, 2)
catch ME
fprintf("MATLAB threw the following error:\n\n%s\n", ME.message)
end
Neither at nor bt are defined before the anonymous function w is defined, and neither of those identifiers are specified in the list of input arguments. Therefore they are left undefined. When you try to evaluate the anonymous function, MATLAB checks to see if there are functions named at and bt that can be called with 0 input arguments; if there are, it can call them and use their output in the anonymous function.
Since there are no such functions, MATLAB complains that it cannot evaluate the anonymous function.
If all the operations you perform in your anonymous function were supported for symbolic variables, you could just evaluate the anonymous function.
f = @(x) sin(x)+cos(1/x)-exp(x^2);
syms t
ft = f(t) % sin, cos, exp, /, and ^ are all defined for sym objects
But if you know you're going to want to integrate symbolically, don't call the numeric integration function integral and then convert the result to a symbolic expression. Call the symbolic integration function int instead.
카테고리
도움말 센터 및 File Exchange에서 Code Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!