Which one is correct ? Using (' ') or @(var)
古いコメントを表示
I found that using (' ') can result in the same value on feval(), or other functions like fplot(), instead of using @(x). What is the difference? Also I read documentation and inline(expr) works too, even though it's rather not recommended. For example :
feval('sin',[pi/2,pi])
%or
fplot('2*x^3+2.*cos(2*x).*sin(2*x)+3.*tan(2*x)',[-5 5], '--r')
%or example for @()
feval(@(x) sin(x),[pi/2,pi])
fplot(@(x) 2*x^3+2.*cos(2*x).*sin(2*x)+3.*tan(2*x),[-5 5], '--r')
2 件のコメント
Image Analyst
2021 年 11 月 8 日
編集済み: Image Analyst
2021 年 11 月 8 日
I don't understand the question. Where are you using either '' or @????
Exactly what is not recommended? Reading the documentation . . . or some particular function like feval() or fplot()?
Aji Bowo
2021 年 11 月 8 日
採用された回答
その他の回答 (1 件)
Some older functions allow you to specify the name of a function to call. But if you're writing code to use in a recent release of MATLAB, prefer using an anonymous function or a regular function handle.
Don't use inline objects either. Those are very old and have some idiosyncracies that function handles or anonymous functions don't. For instance, composing anonymous functions is easy. Composing inline objects is most certainly not.
f = @(x) 2*x;
g = @(x) x.^2;
h = @(x) f(g(x)); % 2*x.^2
h(5) % 2*5.^2 = 50
f2 = inline('2.*x', 'x');
g2 = inline('x.^2', 'x');
h2 = inline('f(g(x))', 'f', 'g', 'x')
h2(f2, g2, 5) % Yes, you have to pass f2 and g2 into h2
カテゴリ
ヘルプ センター および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!