Effacer les filtres
Effacer les filtres

Anybody know a work-around for plotting multiple-input anonomous functions with fplot ?

2 vues (au cours des 30 derniers jours)
Hi. Sometimes I like to have anonymous functions that can take multiple inputs but not necessarily be multivariable. For a trivial example, if I wanted to show how a decaying sinusoid changes with amplitude and frequency - I could do
syms x
myFun = @(A,w,x) A*cos(w*x)*exp(-w*x);
figure(1); ezplot(myFun(1,100,x),[-1,1]); axis tight
My function is still 1D, even though I have multiple inputs. This kind of works using EZPLOT, but sometimes the curve has regions that are blank ! It seems that FPLOT handles these issues better. Using FPLOT:
myFun2 = @(x) 1*cos(100*x)*exp(-100*x);
figure(2); fplot(myFun2,[-1,1]); axis tight
FPLOT gives good result, but I cannot use the multiple-input function.
I guess that the best way to solve this issue would be to use a MATLAB function that takes the anonymous function as an argument and returns a string that can be used as the input-function for FPLOT. Any ideas ?

Réponse acceptée

Star Strider
Star Strider le 25 Août 2016
You have to create a separate function handle with ‘myFun2’ calling ‘myFun’. Then fplot is happy.
This runs without error:
syms x
myFun = @(A,w,x) A.*cos(w.*x).*exp(-w.*x);
figure(1); ezplot(myFun(1,100,x),[-1,1]); axis tight
myFun2 = @(x) myFun(1,100,x);
figure(2); fplot(myFun2,[-1,1]); axis tight
Also, it’s always a good idea to vectorise your functions using element-wise operators. See the documentation on Array vs. Matrix Operations for details.
  2 commentaires
Ronny Landsverk
Ronny Landsverk le 25 Août 2016
Modifié(e) : Ronny Landsverk le 25 Août 2016
Thanks for super-fast response. I also figured out that you can do
fplot(char(myFun(1,100,x)),[-1,1])
Star Strider
Star Strider le 25 Août 2016
My pleasure.
The char approach is interesting. I’ll keep that in mind.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by