How can I interpolate just one point on a given function while saving time?

48 ビュー (過去 30 日間)
Renato Quartullo
Renato Quartullo 2021 年 3 月 19 日
コメント済み: Michal 2023 年 8 月 25 日
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?
Thank you in advance!
  4 件のコメント
Renato Quartullo
Renato Quartullo 2021 年 3 月 19 日
編集済み: Renato Quartullo 2021 年 3 月 19 日
@Jan The spline method could be an accurate one, any other suggestion? (I tried with a linear interpolation, but it is not sufficient)
@Walter Roberson the vector X represent angles in degree, X = -360:0.5:359.5.
Image Analyst
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.

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

採用された回答

Jan
Jan 2021 年 3 月 22 日
3 times faster without overhead, if you fokus on the neighborhood:
X = -360:0.5:359.5;
Y = rand(size(X));
xi = 7.59;
tic
for k = 1:1e4
yi1 = interp1(X, Y, xi,'spline');
end
toc
tic
index = find(xi >= X, 1, 'last');
XX = X(index-8:index+8);
for k = 1:1e4
F = griddedInterpolant(XX, Y(index-8:index+8), 'spline');
yi2 = F(xi);
end
toc
yi1 - yi2 % Of course a difference: RAND are very noisy input data
% Elapsed time is 1.554598 seconds.
% Elapsed time is 0.513496 seconds.
  1 件のコメント
Michal
Michal 2023 年 8 月 25 日
You should check out conditions: 1 <= index-8 & index+8 <= length(X)

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

その他の回答 (1 件)

Walter Roberson
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 件のコメント
Jan
Jan 2021 年 3 月 22 日
編集済み: Jan 2021 年 3 月 22 日
[Moved to an answer]
Renato Quartullo
Renato Quartullo 2021 年 3 月 22 日
It's great!! Thank you so much Yan, very very great!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by