Edd*_*ddy 5 matlab function spatial-interpolation
我在 data.txt 中有 2 列 xy 数据,如下所示:
0 0
1 1
2 4
3 9
4 16
5 25
Run Code Online (Sandbox Code Playgroud)
现在我想定义一个函数 f(x),其中 x 是第一列,f(x) 是第二列,然后能够打印该函数的值,如下所示:
f(2)
Run Code Online (Sandbox Code Playgroud)
这应该给我 4。
我该如何实现这一目标?
假设您想要作为参考的数字之间的一些返回值,您可以使用线性插值:
function y= linearLut(x)
xl = [0 1 2 3 4 5];
yl = [0 1 4 9 16 25];
y = interp1(xl,yl,x);
end
Run Code Online (Sandbox Code Playgroud)
该函数的更通用版本可能是:
function y= linearLut(xl,yl,x)
y = interp1(xl,yl,x);
end
Run Code Online (Sandbox Code Playgroud)
然后您可以使用匿名函数创建特定实例:
f = @(x)(linearLut([0 1 2 3 4],[0 1 4 9 16],x));
f(4);
Run Code Online (Sandbox Code Playgroud)