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);