What is the best method to generate function handle from double array?

I have experimental data in array but I need pass it to a function(integral) as a function handle. May you advice some solution? For example I try this method but it's not very clear
arr = [1 2 2];
f = @(t)(t - floor(t)) .* arr(ceil(t)) - (t - ceil(t)) .* arr(floor(t));
t = integral(f, 1, 3);

 採用された回答

John D'Errico
John D'Errico 2018 年 5 月 14 日
編集済み: John D'Errico 2018 年 5 月 14 日

0 投票

Integral is not designed to integrate a list of data points such as you have. Worse, all you are doing is creating a piecewise linear function!
What tool can integrate a piecewise linear approximation, and a tool that is trivial to use?
arr = [1 2 2];
trapz(1:3,arr)
ans =
3.5
Use of an adaptive numerical integration to integrate a list of data points is silly. Sorry, but it is.

3 件のコメント

Evgen Servetnik
Evgen Servetnik 2018 年 5 月 15 日
And one more question what you may advice for function ode45?
John D'Errico
John D'Errico 2018 年 5 月 15 日
Are you asking how to use a function handle in a tool like ODE45? Or, for that matter, how you COULD have used it in integral?
You pass in the function handle to the tool. For some reason, you tried to pass in the number i into integral. You created the function handle f. Then why would you pass in the number i into integral?
The same applies to ODE45. You create a function handle that takes the correct arguments. Then you pass it into ODE45, with the arguments to ODE45 created as is appropriate for that code. There are multiple examples provided in the docs to ODE45 that show how to use it.
doc ode45
Evgen Servetnik
Evgen Servetnik 2018 年 5 月 15 日
no, I ask how to generate function handle from array

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Numerical Integration and Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by