clc,clear
data = xlsread('问题2数据表.xlsx');
%%
% 花菜类
y1 = data(:, 1);
x1 = data(:, 2);
%%
% 花叶类
y2 = data(:, 3);
x2 = data(:, 4);
%%
% 辣椒类
y3 = data(:, 5);
x3 = data(:, 6);
%%
% 茄类
y4 = data(:, 7);
x4 = data(:, 8);
%%
% 食用菌
y5 = data(:, 9);
x5 = data(:, 10);
%%
% 水生根茎类
y6 = data(:, 11);
x6 = data(:, 12);
%% Fit: '花菜类销量与平均定价关系函数'.
function [fitresult, gof] = createFit(x, y)
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( 'poly2' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Robust = 'LAR';
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', '花菜类销量与平均定价关系函数' );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. x', '花菜类销量与平均定价关系函数', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
grid on
%% Fit: '花叶类销量与平均定价关系函数'.
function [fitresult, gof] = createFit(x2, y2)
[xData, yData] = prepareCurveData( x2, y2 );
% Set up fittype and options.
ft = fittype( 'poly2' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Robust = 'LAR';
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', '花叶类销量与平均定价关系函数' );
h = plot( fitresult, xData, yData );
legend( h, 'y2 vs. x2', '花叶类销量与平均定价关系函数', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x2', 'Interpreter', 'none' );
ylabel( 'y2', 'Interpreter', 'none' );
grid on
%% Fit: '辣椒类销量与平均定价关系函数'.
function [fitresult, gof] = createFit(x3, y3)
[xData, yData] = prepareCurveData( x3, y3 );
% Set up fittype and options.
ft = fittype( 'poly2' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Robust = 'LAR';
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', '辣椒类销量与平均定价关系函数' );
h = plot( fitresult, xData, yData );
legend( h, 'y3 vs. x3', '辣椒类销量与平均定价关系函数', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x3', 'Interpreter', 'none' );
ylabel( 'y3', 'Interpreter', 'none' );
grid on
%% Fit: '茄类销量与平均定价关系函数'.
function [fitresult, gof] = createFit(x4, y4)
[xData, yData] = prepareCurveData( x4, y4 );
% Set up fittype and options.
ft = fittype( 'poly2' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Robust = 'LAR';
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', '茄类销量与平均定价关系函数' );
h = plot( fitresult, xData, yData );
legend( h, 'y4 vs. x4', '茄类销量与平均定价关系函数', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x4', 'Interpreter', 'none' );
ylabel( 'y4', 'Interpreter', 'none' );
grid on
%% Fit: '食用菌销量与平均定价关系函数'.
function [fitresult, gof] = createFit(x5, y5)
[xData, yData] = prepareCurveData( x5, y5 );
% Set up fittype and options.
ft = fittype( 'poly2' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Robust = 'LAR';
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', '食用菌销量与平均定价关系函数' );
h = plot( fitresult, xData, yData );
legend( h, 'y5 vs. x5', '食用菌销量与平均定价关系函数', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x5', 'Interpreter', 'none' );
ylabel( 'y5', 'Interpreter', 'none' );
grid on
%% Fit: '水生根茎类销量与平均定价关系函数'.
function [fitresult, gof] = createFit(x6, y6)
[xData, yData] = prepareCurveData( x6, y6 );
% Set up fittype and options.
ft = fittype( 'poly2' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Robust = 'LAR';
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', '水生根茎类销量与平均定价关系函数' );
h = plot( fitresult, xData, yData );
legend( h, 'y6 vs. x6', '水生根茎类销量与平均定价关系函数', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x6', 'Interpreter', 'none' );
ylabel( 'y6', 'Interpreter', 'none' );
grid on