Writing linear interpolation code

1 次查看(过去 30 天)
Shaun King
Shaun King 2020-5-12
编辑: Shaun King 2020-5-12
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.

回答(1 个)

Image Analyst
Image Analyst 2020-5-12
编辑:Image Analyst 2020-5-12
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.

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by