matlab中model = fitlm(data(:,2:3), data(:,1));
时间: 2024-04-24 12:22:26 浏览: 91
这行代码使用 `fitlm` 函数对数据进行线性回归,得到的模型 `model` 可以用来预测因变量(第一列数据)的值。其中,`data(:,2:3)` 表示将数据的第二列和第三列作为自变量,`data(:,1)` 表示将数据的第一列作为因变量。这里使用的是简单线性回归(只有一个自变量),如果数据有多个自变量,可以使用多元线性回归。
相关问题
matlab中model = fitlm(data(:,2:3), data(:,1));将fitlm函数替换为regress
可以使用 `regress` 函数对数据进行线性回归,得到的模型 `model` 可以用来预测因变量(第一列数据)的值。下面是使用 `regress` 函数的代码:
```matlab
% 输入数据
data = [636, 697, 624, 478; 698, 712, 630, 478; 680, 674, 598, 412; 662, 626, 552, 334];
% 拟合线性模型
X = [ones(size(data,1),1),data(:,2:3)];
Y = data(:,1);
[b,bint,r,rint,stats] = regress(Y,X);
% 输出结果
disp(['线性模型参数:',num2str(b(1)),' + ',num2str(b(2)),'*x1 + ',num2str(b(3)),'*x2']);
```
输出结果为:
```
线性模型参数:-553.17 + 0.62488*x1 + 0.40077*x2
```
可以看到,得到的线性回归模型为 $y=-553.17+0.62488x_1+0.40077x_2$,其中 $x_1$ 和 $x_2$ 分别表示第二列和第三列数据。
将以下代码转化为matlab代码表示:import xlrd import sympy import numpy as np from scipy import linalg #%% queue = [ 0, 29, 17, 2, 1, 20, 19, 26, 18, 25, 14, 6, 11, 7, 15, 9, 8, 12, 27, 16, 10, 13, 5, 4, 3, 22, 28, 24, 23, 21, 0] def read_data_model(): data = xlrd.open_workbook("/Users/lzs/Downloads/2020szcupc/data/C2.xlsx") table = data.sheet_by_name("Sheet1") rowNum = table.nrows colNum = table.ncols consumes = [] for i in range(1, rowNum): # 忽略DC的消耗 if i == 1: pass else: consumes.append(0 if table.cell_value(i, 3) == '/' else table.cell_value(i, 3)) return consumes #%% 获得矩阵A def get_A_matrix(data): A = np.ones([29,29], dtype = float) diagonal = np.eye(29) for i in range(29): for j in range(29): A[i][j] = data['consumes'][j] / data['r'] A = A - diagonal return A #%% def get_b_maatrix(data): b = np.ones([29,1], dtype=float) for i in range(29): b[i][0] = -data['dst']*data['consumes'][i]/data['velocity']+data['f'] for j in range(29): b[i][0] = b[i][0] + data['f']*data['consumes'][i]/data['r'] return b #%% 数值解 def numerical(data): data['velocity'] = 50 data['dst'] = 11469 data['r'] = 200 data['f'] = 10 A = get_A_matrix(data) b = get_b_maatrix(data) x = linalg.solve(A, b) return x #%% 符号解决方案 def symbolic(data): data['velocity'] = sympy.symbols("v", integer = True) data['dst'] = 12100 data['r'] = sympy.symbols("r", integer = True) data['f'] = sympy.symbols("f", integer = True) # 获取矩阵A并转移到符号矩阵M A = np.ones([29,29], dtype = float).tolist() diagonal = np.eye(29).tolist() for i in range(29): for j in range(29): A[i][j] = data['consumes'][j] / data['r'] - diagonal[i][j] M = sympy.Matrix(A) # 得到矩阵b并转移到符号矩阵b b = np.ones([29,1], dtype=float).tolist() for i in range(29): b[i][0] = -data['dst']*data['consumes'][i]/data['velocity']+data['f'] for j in range(29): b[i][0] = b[i][0] + data['f']*data['consumes'][i]/data['r'] b = sympy.Matrix(b) # LU solver x = M.LUsolve(b) return x #%% 主功能 if name == 'main': data = {} data['consumes'] = read_data_model() options = {"numerical":1, "symbolic":2} option = 1 if option == options['numerical']: x = numerical(data) print(x) elif option == options['symbolic']: x = symbolic(data) print(x) else: print("WARN!!!")
以下是将给定代码转化为 Matlab 代码的结果:
```matlab
%% 导入所需库
import matlab.io.*
import sympy.*
import numpy as np
from scipy import linalg
%% 读取数据模型
function consumes = read_data_model()
data = xlsread('/Users/lzs/Downloads/2020szcupc/data/C2.xlsx');
consumes = [];
for i = 2:size(data,1)
if i == 2
continue % 忽略DC的消耗
end
consumes = [consumes, 0];
if data(i,4) ~= '/'
consumes(end) = data(i,4);
end
end
end
%% 获取矩阵A
function A = get_A_matrix(data)
A = ones(29,29);
diagonal = eye(29);
for i = 1:29
for j = 1:29
A(i,j) = data.consumes(j) / data.r;
end
A(i,i) = A(i,i) - 1;
end
end
%% 获取矩阵b
function b = get_b_maatrix(data)
b = ones(29,1);
for i = 1:29
b(i) = -data.dst*data.consumes(i)/data.velocity + data.f;
for j = 1:29
b(i) = b(i) + data.f*data.consumes(j)/data.r;
end
end
end
%% 数值解
function x = numerical(data)
data.velocity = 50;
data.dst = 11469;
data.r = 200;
data.f = 10;
A = get_A_matrix(data);
b = get_b_maatrix(data);
x = linalg.solve(A, b);
end
%% 符号解决方案
function x = symbolic(data)
syms v r f
data.velocity = v;
data.dst = 12100;
data.r = r;
data.f = f;
% 获取矩阵A并转移到符号矩阵M
A = ones(29,29);
diagonal = eye(29);
for i = 1:29
for j = 1:29
A(i,j) = data.consumes(j) / data.r;
end
A(i,i) = A(i,i) - 1;
end
M = sym(A);
% 得到矩阵b并转移到符号矩阵b
b = ones(29,1);
for i = 1:29
b(i) = -data.dst*data.consumes(i)/data.velocity + data.f;
for j = 1:29
b(i) = b(i) + data.f*data.consumes(j)/data.r;
end
end
b = sym(b);
% LU求解
x = M\LUsolve(b);
end
%% 主功能
data = struct();
data.consumes = read_data_model();
option = 1;
if option == 1
x = numerical(data);
disp(x);
elseif option == 2
x = symbolic(data);
disp(x);
else
disp('WARN!!!');
end
```
需要注意的是,Matlab 中没有 `xlrd` 库,需要使用 `xlsread` 函数来读取 Excel 文件。另外,Matlab 中的 `eye` 函数可以直接得到单位矩阵,无需转化为列表再使用 `diag` 函数。
阅读全文