将以下代码转化为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!!!")
时间: 2023-10-09 11:15:01 浏览: 169
以下是将给定代码转化为 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` 函数。
阅读全文