Converting Anonymous Function to a Symbolic Function

26 ビュー (過去 30 日間)
Keenan Descartin
Keenan Descartin 2019 年 3 月 11 日
コメント済み: Steven Lord 2023 年 12 月 30 日
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?

採用された回答

Adam Danz
Adam Danz 2019 年 3 月 11 日
編集済み: Adam Danz 2019 年 3 月 28 日
Is this what you're looking for?
v = @(t) exp(1).^(sin(t)) - 1;
sym(v)
%ans =
%(3060513257434037/1125899906842624)^sin(t) - 1
% where 3060513257434037/1125899906842624 is an approximation of exp(1)
  3 件のコメント
Josué Jiménez
Josué Jiménez 2020 年 11 月 13 日
I love you
Walter Roberson
Walter Roberson 2020 年 11 月 13 日
Or equivalently,
sym t
v(t)
Caution: not everything can be converted these ways, and not everything will convert the way you expect.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2021 年 4 月 23 日
v = @(t) exp(1).^(sin(t)) - 1;
str2sym(char(v)) %r2017b or later
ans = 
  1 件のコメント
Ryan
Ryan 2021 年 10 月 19 日
編集済み: Ryan 2021 年 10 月 19 日
doesnt work with nested functions ... :(
ie
www = @(t) t+1
yyy = @(t) www(t)+1

サインインしてコメントする。


madhan ravi
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 件のコメント
Walter Roberson
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.
Steven Lord
Steven Lord 2023 年 12 月 30 日
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)
w = function_handle with value:
@(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
MATLAB threw the following error: Unrecognized function or variable 'at'.
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
ft = 
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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by