将函数定义为x,y数据的插值 [英] Define function as interpolation of x,y data

查看:133
本文介绍了将函数定义为x,y数据的插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  0 0 
1 1
2 4
3 9
4 16
5 25

现在我想定义一个函数f(x),其中x是第一列,f(x)是第二列,然后可以打印此函数的值,如下所示:

  f(2)

其中应该给我4分。



我如何做到这一点?

解决方案

假设你想要一些数值的返回值作为参考,你可以使用线性插值:

 函数y = linearLut(x)
xl = [0 1 2 3 4 5];
yl = [0 1 4 9 16 25];
y = interp1(xl,yl,x);
end

该函数的更通用版本可能是:

 函数y = linearLut(xl,yl,x)
y = interp1(xl,yl,x);
end

然后您可以使用匿名函数创建特定实例:

  f = @(x)(linearLut([0 1 2 3 4],[0 1 4 9 16],x)); 
f(4);


I have 2 columns of x y data in data.txt like this:

0  0
1  1
2  4
3  9
4  16
5  25

Now I want to define a function f(x) where x is the first column and f(x) is the second column, and then be able to print values of this function like so:

f(2)

Which should give me 4.

How do I achieve this?

解决方案

Assuming that you want some return value for numbers between the ones you have as reference, you can use linear interpolation:

    function y= linearLut(x)
         xl = [0 1 2 3 4 5];
         yl = [0 1 4 9 16 25];
         y = interp1(xl,yl,x);
    end

A more generic version of the function might be:

    function y= linearLut(xl,yl,x)
         y = interp1(xl,yl,x);
    end

And then you can create specific instances by using anonymous functions:

    f = @(x)(linearLut([0 1 2 3 4],[0 1 4 9 16],x));
    f(4);

这篇关于将函数定义为x,y数据的插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆