How to get the string form of an anonymous function?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Richard
el 16 de Feb. de 2021
Comentada: Richard
el 16 de Feb. de 2021
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.
Respuesta aceptada
Fangjun Jiang
el 16 de Feb. de 2021
Editada: Fangjun Jiang
el 16 de Feb. de 2021
f = @(x) exp(-0.5*x);
>> func2str(f)
ans =
'@(x)exp(-0.5*x)'
Or, provide your string, use str2func()
Más respuestas (0)
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!