Writing linear interpolation code

1 view (last 30 days)
Shaun King
Shaun King on 12 May 2020
Edited: Shaun King on 12 May 2020
I need to write a matlab function lin_interpol(x_pt,y_pt) which, given vectors of data points x pt, y pt, returns the linear interpolating function f. I need to use find(x > x pt) to find the locations of the entries in x pt which are > x. I also need to compare the output with that of the matlab function interp1(x_pt,y_pt,xx), which evaluates the linear interpolant through x_pt, y_pt at the points in xx.

Answers (1)

Image Analyst
Image Analyst on 12 May 2020
Edited: Image Analyst on 12 May 2020
You can't have spaces in variable names, or two input arguments to a function without a comma between them, so lin_interpol(x pt,y pt) really needs to be lin_interpol(xpt, ypt) or lin_interpol(x, ypt, y, ypt) depending on if you're passing in 2 or 4 arguments.
And when you say interp1(x pt,y pt,xx) is that 3 arguments, or 5? If you're going to have a variable number of arguments (which complicates things) you're going to have to do
function interp1(varargin)
and then look at nargin and extract out the correct things depending on how many arguments nargin tells you are there.
Is pt or xpt a scalar? Or another vector? If it's another vector, then I presume you have an x vector to compare xpt against? If xpt is a scalar, then you still need an x vector that you can plot, unless you're going to assume some range and create one.
See my attached polyfit demo.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!