How to extend smoothingspline predictions
2 views (last 30 days)
Show older comments
Roederer Lyne
on 18 Feb 2016
Commented: Roederer Lyne
on 26 Feb 2016
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?
clc
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;
end
C = C(:);
Z = z(:);
% a = fit(C,Z,'poly2');
b = fit(C,Z,'smoothingspline');
plot(C,Z)
axis([-0.05 0 -400 100 ])
hold on
% plot(a,'r--')
plot(b,'g--')
xlabel('Current (m/s)')
ylabel('y position (mm)')
0 Comments
Accepted Answer
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)
See Also
Categories
Find more on Working with Signals 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!