Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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 ?

Risposta accettata

Star Strider
Star Strider il 25 Ago 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.

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by