convert cell to an integrable function

3 ビュー (過去 30 日間)
Mohammadfarid ghasemi
Mohammadfarid ghasemi 2022 年 5 月 27 日
回答済み: Joel Van Sickel 2022 年 6 月 10 日
Dear all,
I want to enter several functions as function handles as input to a function and integrate them according the the prescribed variable in handle, a schematic representation of my request is as follow:
function y=ftest(func)
for i=1:size(func,2)
f=func;
ff=f{i};
y(i)=integral(@(var) ff(var),0,1);
end
end
Any help would be greatly appreciated :)
  2 件のコメント
Jan
Jan 2022 年 5 月 27 日
Yes, this looks good. I'd omit the indirection over f and use func directly. And if func contains a cell of function handles already, there is no need to convert them to a function handle:
function y=ftest(func)
y = zeros(1, size(func, 2)); % Pre-allocate
for i = 1:size(func,2)
ff = func{i};
y(i) = integral(ff, 0, 1); % Or directly: integral(func{i}, 0, 1);
end
end
Do you have problems with your code? What exactly do you use as input? Do you get an error message? If so, which one?
Mohammadfarid ghasemi
Mohammadfarid ghasemi 2022 年 5 月 28 日
It worked, thank you so much:)

サインインしてコメントする。

回答 (1 件)

Joel Van Sickel
Joel Van Sickel 2022 年 6 月 10 日
This question was answered by Jan in the comments. To clean up the answers section, I am posting it down here as well:
Yes, this looks good. I'd omit the indirection over f and use func directly. And if func contains a cell of function handles already, there is no need to convert them to a function handle:
ThemeCopy
function y=ftest(func)
y = zeros(1, size(func, 2)); % Pre-allocate
for i = 1:size(func,2)
ff = func{i};
y(i) = integral(ff, 0, 1); % Or directly: integral(func{i}, 0, 1);
end
end
Do you have problems with your code? What exactly do you use as input? Do you get an error message? If so, which one?

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by