Saving an interpolated function to an m-file

조회 수: 7 (최근 30일)
Julius de Hond
Julius de Hond 2019년 10월 19일
댓글: Ethan Duckworth 2021년 7월 12일
I have some complicated function that I have interpolated using interp1() so that evaluating it is much faster. I would like the result of this interpolation to be available systemwide by saving it as a function in an .m-file. An example:
xd = -1:0.01:1;
yd = xd.^2;
f = @(x) interp(xd, yd, x);
Is there a way to save the result f to f.m such that I can evaluate f(x) anywhere?

채택된 답변

Sai Bhargav Avula
Sai Bhargav Avula 2019년 10월 22일
Hi,
To my understanding, you want to save the custom interpolation as function to be available for further evaluation.
For that you can write the code as a MATLAB function.
Here is a sample version for the example code you provided
function res = f(x)
xd = -1:0.01:1;
yd = xd.^2;
res = interp(xd, yd, x);
end
Hope this helps.
  댓글 수: 2
Julius de Hond
Julius de Hond 2019년 10월 24일
This is what I ended up doing, although it is not very elegant. I was hoping to find some workaround to save the interpolated function (f, in my original question) in some way that I could access it later, since I can delete my original data (xd and yd) from the workspace, and still have it working.
This still provided a significant speedup, though, so I'm happy with the result.
Ethan Duckworth
Ethan Duckworth 2021년 7월 12일
I'm not positive, but I doubt you can save anything (memory, speed, etc.) by deleting the data and somehow keeping the function. The interp1 function by default uses linear interpolation, so suppose you had a million data points, and formed the interpolating function. The function itself would have something equivalent in size to the million data points built into the definition/formulas for the function! Of course, you can put the data inside the function file, and not keep it in the workspace.
On the other hand, you might be able to get a more compact/efficient regression function and throw the data away.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by