How can I interpolate just one point on a given function while saving time?
显示 更早的评论
Hi all,
I have a function
and I have a collection of N couple of points
, namely
and
respecting the function f. I want to evaluate my function in a single point
out of set X, so
. Until now I'm using the Matlab function
= interp1(
,'spline'). But maybe this methods requires a certain computational effort. Since I always evaluate my function on a single point (not a vector of points), is there an alternative function (or method) to interp1 to do this task that allows to save time?
and I have a collection of N couple of points Thank you in advance!
4 个评论
Jan
2021-3-19
Do you really need a spline?
Walter Roberson
2021-3-19
Will you be doing this repeatedly with the same X and Y?
Is your X sorted? Is it monotonic (no duplicates, no reversals) ?
Renato Quartullo
2021-3-19
编辑:Renato Quartullo
2021-3-19
Image Analyst
2021-3-20
Everything takes computation effort. Please state how long it takes, and how fast you need it to be. You can use tic and toc to time it.
采纳的回答
更多回答(1 个)
Walter Roberson
2021-3-19
With the X being sorted and known increments, for any xstar you can directly compute the corresponding X bin that does not exceed xstar as xstar*2+721. Call that idx
jdxstart = min(max(1,idx-2), length(X) - 4);
jdx = jdxstart:jdxstart+4;
pp = spline(X(jdx), y(idx))
ystar = ppval(pp, xstar);
That is, we extract X y "near" xstar and spline only there to reduce the work.
6 个评论
Renato Quartullo
2021-3-20
Jan
2021-3-20
interp1 and spline have a lot of overhead. If you post some code, which shows unequivocally, how you call these functions, it is possible to suggest a lean version, which avoids the overhead. Because your system is tiny, it is e.g. a waste of time to set it up as sparse matrix as in spline().
Renato Quartullo
2021-3-22
Renato Quartullo
2021-3-22
Renato Quartullo
2021-3-22
类别
在 帮助中心 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
