cftool
在其核心使用fit
。你可以做些什么来进一步探索适合度和残差是否适合你的工作空间。通过曲线拟合工具窗口顶部的“拟合”菜单执行此操作,然后选择“保存到工作区”。使用这个拟合对象(一个曲面的cfit
或一个曲面的sfit
),您可以使用该曲线拟合工具进行相同的分析。
让我说明如何获得拟合,创建残差图和如何计算残差。产生的图像如下所示。在代码中,residuals
变量包含x
和y
中每个属于每个样本对的每个元素的拟合残差。
% Generate data
rng default
x = sort(rand(10, 1));
y = randn(size(x)) - 3*x;
% Fit a line
fitted = fit(x, y, fittype('poly1'));
% Plot fitted line with data
figure
subplot 311
plot(fitted, x, y)
% Plot residuals
subplot 312
plot(fitted, x, y, 'residuals)')
ylabel residuals
% Get residuals
residuals = y - fitted(x);
% Create stem plot of residuals
subplot 313
stem(x, residuals)
legend residuals
xlabel x
ylabel residuals