Anybody know a work-around for plotting multiple-input anonomous functions with fplot ?
4 views (last 30 days)
Show older comments
Ronny Landsverk
on 25 Aug 2016
Commented: Star Strider
on 25 Aug 2016
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 ?
0 Comments
Accepted Answer
Star Strider
on 25 Aug 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 Comments
More Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!