function [fitresult, gof] = createFit(x1, y)
%CREATEFIT(X1,Y)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : x1
% Y Output: y
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% 另请参阅 FIT, CFIT, SFIT.
% 由 MATLAB 于 23-Mar-2022 22:29:24 自动生成
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( x1, y );
% Set up fittype and options.
ft = fittype( 'poly5' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Normalize = 'off';
opts.Robust = 'Bisquare';
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. x1', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x1', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
grid on
希望能给予解答
你好,首先拟合
[fitresult, gof] = createFit(x1, y)
然后预测
比方说
x2 = [1,2,3,4]; % 已知x2,预测y2
y2 = fitresult(x2); % 这样就OK拉
有帮助望采纳哟,谢谢啦