How do you approximate a function for interpolated points? I use the natural cubic spline to interpolate points as follows for n = 500 points:

t=[0; 3; 6; 9]
z=[0; 6; 6; 0]

zn = natcubicspline(t,z,ti)
yn = line(ti,zn)

Is there any way approximate a function for these n-many points (for large n)? Or are there ways to treat interpolated points as if they are a function, i.e. find the gradient of the zn vector? Because zn is a vector of constants, this wouldn't necessarily be helpful.

Update: In particular, my data appears to form a 2nd degree polynomial, so I went ahead and used the following Matlab function to fit my data:

p = polyfit(transpose(ti),zn,2)

Which yields coefficient estimates for a 2nd degree polynomial. It does fit the data, but with high error values, and I have to multiply this coefficient vector by a vector [1 z z^2] to get the correct polynomial. Is there any way to streamline this?


1 Answer 1


I think you want to use polyfit on the original data, not the already interpolated data.

p = polyfit(t,z,2);

Now p is your coefficients of the quadratic which interpolates z at t. You can evaluate the polynomial at your points with

zi = polyval(p,ti);

Since it's a polynomial, you can differentiate it (you could automate this and make it cleaner)

p_prime = [2*p(1), p(2)];

and evaluate this;

zi_prime = polyval(p_prime, ti);

For anything more complicated you could look at interp1 and griddedInterpolant. (griddedInterpolant returns a function).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.