MATLAB Answers

How to extend smoothingspline predictions

10 views (last 30 days)
I need to extend my best fit curve so that I can get a current prediction when y position = 70 mm, is there a possible way to do this?
clear all
load TOD_regular_lda.mat
x = 1;
for i = 1:1:5
current(x) = mean(lda(x).um);
z(x) = lda(x).z;
C = current;
x = x + 1;
C = C(:);
Z = z(:);
% a = fit(C,Z,'poly2');
b = fit(C,Z,'smoothingspline');
axis([-0.05 0 -400 100 ])
hold on
% plot(a,'r--')
xlabel('Current (m/s)')
ylabel('y position (mm)')


Sign in to comment.

Accepted Answer

Anish Mitra
Anish Mitra on 22 Feb 2016
I cannot comment on the quality of the extension (extrapolation) of the fit, since it depends on the data. However, the 'feval' method can be used with the 'cfit' object that is returned by the 'fit' function.
Following is a sample code to extend the fitted values, and compare them to the original synthetic data.
% Code to test extrapolation for different fit types
% (smoothingspline and quadratic polynomial curve)
clear; close all; clc;
% Generate data for the entire range (to compare the extrapolated result
% later on
x_all = (0:0.01:5)';
y_all = 0.2*x_all.^3 - x_all.^2;
% Obtain fitting data
x_data = x_all(x_all>1 & x_all<4);
y_data = y_all(x_all>1 & x_all<4);
% Fit a smoothing spline
fit_ss = fit(x_data,y_data,'smoothingspline');
% Fit a quadratic polynomial curve
fit_poly = fit(x_data,y_data,'poly2');
% Evaluate the fitted function for the original range using the feval
% function
y_ss = feval(fit_ss,x_all);
y_poly = feval(fit_poly,x_all);
% Plot all data
figure; hold on;
plot(x_all, y_all, 'b');
plot(x_data, y_data, 'g.');
plot(x_all, y_ss,'r');
plot(x_all, y_poly, 'k');
Note that 'SmoothingSpline' is generally not recommended if the objective is to then extrapolate the fit to outside the initial data limits. The following example contains more details on Smoothing Splines.

More Answers (0)