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

2 次查看(过去 30 天)
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 ?

采纳的回答

Star Strider
Star Strider 2016-8-25
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by