Speed up systems with interpolant
조회 수: 2 (최근 30일)
이전 댓글 표시
Dear all,
I have to solve a system with interpolants and I have to find the fastest way because it requires quite a lot of CPU time. I noticed that using a 2D interpolant if faster than 1D and this sounds very strange. Here an example of the code:
x1=[0:9];
y1=[0 1 2 4 6 9 13 20 40 100];
tic
for k = 5:1:15
f = @(x)(interp1(x1,y1,x));
%mat = scatteredInterpolant([x1 0 9]',[1 1 1 1 1 1 1 1 1 1 2 2]',[y1 0 100]');
%f = @(x) mat(x,1);
F1 = @(z1,z2,z3) f(z1)+f(z2)+k*(f(z3)*f(z3))/(f(z3)+f(z3))-15;
F2 = @(z2,z3) f(z2)+k*(f(z3)*f(z3))/(f(z3)+f(z3))-10;
F3 = @(z2,z3) f(z2)-k*(f(z3)*f(z3))/(f(z3)+f(z3))+9;
fun = @(z) [F1(z(1),z(2),z(3)), F2(z(2),z(3)), F3(z(2),z(3))];
[z] = fsolve(fun, [5 5 5]);
end
toc
If now I commentate line
f = @(x)(interp1(x1,y1,x));
and uncomment the lines
mat = scatteredInterpolant([x1 0 9]',[1 1 1 1 1 1 1 1 1 1 2 2]',[y1 0 100]');
f = @(x) mat(x,1);
the system solves almost twice faster, even if the solution is the same. Even if it is faster I would not like to use this trick, because I don't see any sense to transform a 1D interpolation to a 2D just to speed up the solution.
Do you have an idea why?
Thanks and regards.
댓글 수: 0
답변 (1개)
Yair Altman
2015년 10월 28일
scatteredInterpolant is a built-in function so we do not know how it is actually implemented internally, but it stands to reason that an internal compiled C function would run faster than an interpreted m-function (interp1.m). Also, it is quite possible that scatteredInterpolant is optimized internally for the 1D case, so mat(x,1) might well be using an optimized code branch.
If you wish to improve interpolation speed, consider using interp1q. Unlike the generic interp1 function, interp1q avoids any checks of its inputs, which are expected to be a monotonically increasing column vector (x), a column vector or matrix with length(x) rows (Y), and a column vector (xi).
An even faster implementation of the core logic was implemented by Bruno Luong using MEX, which he called nakeinterp1. Jan Simon has also implemented a MEX variant, called ScaleTime, which is many times faster than interp1 or interp1q.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!