How to get the string form of an anonymous function?

조회 수: 3 (최근 30일)
Richard
Richard 2021년 2월 16일
댓글: Richard 2021년 2월 16일
I was thinking about creating a function, which automatically creates plot, data, etc. This function is supposed to be an aid for solving differential equations with numerical methods.
The function is:
function [x,yf,yg]=graphing(f,g,interval)
x = linspace(interval(1),interval(end),80);
yf = f(x);
yg = g(x);
plot(x,yf,'g',x,yg,'b');
tf = 'f(x)=';
tg = 'g(x)=';
legend(tf,tg);
end
And the command could be:
interval = [0 4];
f = @(x) exp(-0.5*x);
g =@(x) sin(2*x);
[x,yf,yg] = graphing(f,g,interval)
There is a way to get the string form of the functional notation for the anonymous function, so that it is possible having in legend the exact function form? It could help me for orientation, even if it is not necessary. Just as a matter of interest.
Thank you in advance.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2021년 2월 16일
편집: Fangjun Jiang 2021년 2월 16일
f = @(x) exp(-0.5*x);
>> func2str(f)
ans =
'@(x)exp(-0.5*x)'
Or, provide your string, use str2func()

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by