How to get the string form of an anonymous function?

3 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Fangjun Jiang
Fangjun Jiang il 16 Feb 2021
Modificato: Fangjun Jiang il 16 Feb 2021
f = @(x) exp(-0.5*x);
>> func2str(f)
ans =
'@(x)exp(-0.5*x)'
Or, provide your string, use str2func()

Più risposte (0)

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by