# Determining function for smoothing spline

30 views (last 30 days)
Gavin Seddon on 27 Mar 2017
Commented: Image Analyst on 4 Apr 2022
Hello, my data when plotted resembles a plot of a sine function and consequently a polynomial curve will not converge. Therefore I am using a smoothing spline. Is it possible to determine the cubic function associated with this?
GS.
Stephen23 on 27 Mar 2017
The function spline returns a structure which encodes the spline.

Image Analyst on 27 Mar 2017
The equations of the cubics between each pair of knots is given in the coefs field. See how I took the demo script from the help and printed them out:
x = -4:4;
y = [0 .15 1.12 2.36 2.36 1.46 .49 .06 0];
cs = spline(x,[0 y 0]);
xx = linspace(-4,4,101);
plot(x,y,'o',xx,ppval(cs,xx),'-');
% Now print out the coefficients
coefficients = cs.coefs
fprintf('The equation for the different segments are:\n');
for k = 1 : size(coefficients, 1)
fprintf('y = %7.4f * x^3 + %7.4f * x^2 + %7.4f * x + %7.4f\n', coefficients(k,:));
end
In the command window, you will see:
coefficients =
0.20344440353461 -0.0534444035346097 0 0
-0.0903332106038293 0.55688880706922 0.50344440353461 0.15
-0.392111561119293 0.285889175257731 1.34622238586156 1.12
0.148779455081001 -0.890445508100147 0.741666053019146 2.36
0.136993740795287 -0.444107142857143 -0.592886597938144 2.36
0.13324558173785 -0.0331259204712812 -1.07011966126657 1.46
-0.0599760677466861 0.366610824742268 -0.736634756995581 0.49
-0.0633413107511046 0.186682621502209 -0.183341310751105 0.06
The equation for the different segments are:
y = 0.2034 * x^3 + -0.0534 * x^2 + 0.0000 * x + 0.0000
y = -0.0903 * x^3 + 0.5569 * x^2 + 0.5034 * x + 0.1500
y = -0.3921 * x^3 + 0.2859 * x^2 + 1.3462 * x + 1.1200
y = 0.1488 * x^3 + -0.8904 * x^2 + 0.7417 * x + 2.3600
y = 0.1370 * x^3 + -0.4441 * x^2 + -0.5929 * x + 2.3600
y = 0.1332 * x^3 + -0.0331 * x^2 + -1.0701 * x + 1.4600
y = -0.0600 * x^3 + 0.3666 * x^2 + -0.7366 * x + 0.4900
y = -0.0633 * x^3 + 0.1867 * x^2 + -0.1833 * x + 0.0600
Image Analyst on 4 Apr 2022
@Nilanshu Mahant not sure what you mean. The cubic spline determines a cubic polynomial for each segment between two points. For example in my answer above I gave all the equations between all the data points. There is not really any need to create dozens or hundreds of equations. Just use the spline function. By its very nature there is no one, overall cubic function for all of your dozens or hundreds of points. You get an equation between each pair of points. If you need some overall analytical formula instead of doing it numerically with spline, then I suggest you use the theoretical formula for your physical process and then use fitnlm() to get the parameters for your formula. If you need more help, start your own question and attach your data and code to read it in and plot it, and what your desired output would be.