how to pass inline function in a M-file function (user defined)

3 ビュー (過去 30 日間)
saloni singhal
saloni singhal 2012 年 3 月 26 日
function out = convert(f(t),t,th)
if(th==1)
out=f(1);
return
else
out=f(th).*convert(f(t),t,th-1);
return
end
end
"in the command window"
t=[1:1:10];
f=inilne('sin(t)','t');
out=convert('f(t)','t',4);

回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 3 月 26 日
function out = convert(f,t,th)
if th == 1
out = f(1);
return
else
out = f(th) .* convert(f, t, th-1);
return
end
end
In the command window,
t = [1:1:10];
f = inline('sin(t)', 't');
out = convert(f, t, 4);
Note: your convert() function never uses the value of t, so you might as well not pass it around.
I don't know why you want to be taking sin(1) ?
I am wondering if in convert(), instead of out=f(1) you want out=f(t) ?

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by