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);

 采纳的回答

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 个评论

And one more question what you may advice for function ode45?
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
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