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

조회 수: 4 (최근 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.
  댓글 수: 2
Ronny Landsverk
Ronny Landsverk 2016년 8월 25일
편집: Ronny Landsverk 2016년 8월 25일
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 2016년 8월 25일
My pleasure.
The char approach is interesting. I’ll keep that in mind.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by