微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Matlab至python嵌套循环的转换

如何解决Matlab至python嵌套循环的转换

我正在将matlab模型转换为python,目的是在python中获得与matlab中相同的图。但是,我陷入了循环转换。

这是可复制的matlab代码

%model data

Dt = 1;
T_oi=12;
K = [1,1,1.50000000000000,0.500000000000000,0.500000000000000];
for i=1:432
    k11(i)= 0.5;
    tao_o(i)= 210;
    R_tap(i) = 10.7491679273828;
    x(i)= 0.8;
    dT_or(i)= 52;
    T_a(i) = 20;
end

dt = (30:30:432*30);
T_o=[];

%model

for step=1:432

    if step==1
        for i=1:dt(step)  
            DT_o(i) = Dt/k11(step)/tao_o(step)*((((1+R_tap(step)*K(step)^2)/(1+R_tap(step)))^x(step))*dT_or(step)-(T_oi-T_a(step)));
            T_o(i) = T_oi + DT_o(i);
        end
    else
        for i=1:(dt(step)-dt(step-1))
            DT_o(i+dt(step-1)) = Dt/k11(step)/tao_o(step)*((((1+R_tap(step)*K(step)^2)/(1+R_tap(step)))^x(step))*dT_or(step)-(T_o(i+dt(step-1)-1)-T_a(step)));
            T_o(i+dt(step-1)) = T_o(i+dt(step-1)-1) + DT_o(i+dt(step-1));
        end
    end
end

figure(1) 
plot(T_o);
grid on

以下是我在python中可重现的尝试。.我知道python中的索引从0开始,但是无论我在范围内尝试什么,我总是在DT_o [i上得到'IndexError:列表分配索引超出范围' ]计算。

import numpy as np
from matplotlib import pyplot as plt

# model data,feel free to ignore
Dt = 1
T_oi = 12
K = [1,0.500000000000000]
k11=[0.5]*432
tao_o=[210]*432
R_tap=[10.7491679273828]*432
x=[0.8]*432
dT_or= [52]*432
T_a=[20]*432


T_o = []
DT_o = []

# model,here comes the issue
dt = np.arange(30,431 * 30,30).reshape(-1)

for step in np.arange(431).reshape(-1):
    if step == 1:
        for i in np.arange(1,dt[step].reshape(-1)):
            DT_o[i] = Dt / k11[step] / tao_o[step] * ((((1 + R_tap[step] * K[step] ** 2) / (1 + R_tap[step])) ** x[step]) * dT_or[step] - (T_oi - T_a[step])) #IndexError here
            T_o[i] = T_oi + DT_o[i]   

    else:
        for i in np.arange(1,(dt[step] - dt[step - 1])).reshape(-1):
            DT_o[i + dt[step - 1]] = Dt / k11[step] / tao_o[step] * ((((1 + R_tap[step] * K[step] ** 2) / (1 + R_tap[step])) ** x[step]) * dT_or[step] - (T_o[i + dt[step - 1] - 1] - T_a[step]))
            T_o[i + dt[step - 1]] = T_o[i + dt[step - 1] - 1] + DT_o[i + dt[step - 1]]

fig_1 = plt.figure()
plt.plot(T_o)
plt.grid()
plt.show()

我最接近获得相同图的方法是将表达式分解成列表推导式,然后扩展要绘制的变量的范围(使用另一个列表推导式),但是表达式似乎不受外部回路,并且没有提供相同的Y轴。以下是我的可重复列表理解尝试:

import numpy as np
from matplotlib import pyplot as plt

# model data    
Dt = 1
T_oi = 12
K = [1,0.500000000000000]

k11 = []
tao_o = []
R_tap = []
x = []
dT_or = []
T_a = []

k11=[0.5]*432
tao_o=[210]*432
R_tap=[10.7491679273828]*432
x=[0.8]*432
dT_or= [52]*432
T_a=[20]*432

# dt = np.arange(30,30).reshape(-1)

# model
# for step in np.arange(431).reshape(-1):
#     if step == 1:
#         for i in np.arange(1,dt[step].reshape(-1)):
#             DT_o[i] = Dt / k11[step] / tao_o[step] * ((((1 + R_tap[step] * K[step] ** 2) / (1 + R_tap[step])) ** x[step]) * dT_or[step] - (T_oi - T_a[step]))
#             T_o[i] = T_oi + DT_o[i]
#
#     else:
#         for i in np.arange(1,(dt[step] - dt[step - 1])).reshape(-1):
#             DT_o[i + dt[step - 1]] = Dt / k11[step] / tao_o[step] * ((((1 + R_tap[step] * K[step] ** 2) / (1 + R_tap[step])) ** x[step]) * dT_or[step] - (T_o[i + dt[step - 1] - 1] - T_a[step]))
#             T_o[i + dt[step - 1]] = T_o[i + dt[step - 1] - 1] + DT_o[i + dt[step - 1]]


K1 = [x ** 2 for x in K]
R_tap1 = [x * y for x,y in zip(R_tap,K1)]
R_tap2 = [1 + x for x in R_tap1]
R_tap3 = [1 + x for x in R_tap]
R_tap_div = [x / y for x,y in zip(R_tap2,R_tap3)]
R_tap_sq = [y ** x for y in R_tap_div]
T_a1 = [T_oi - x for x in T_a]
R_tap_sq_or = [x * y for x,y in zip(R_tap_sq,dT_or)]
R_tap_tot = [x - y for x,y in zip(R_tap_sq_or,T_a1)]
K11_Dt = [Dt / x for x in np.array(k11)]
K11_Dt_tao = [x / np.array(tao_o) for x in K11_Dt]

DT_o = [x * y for x,y in zip(K11_Dt_tao,R_tap_tot)]
T_o = [T_oi + np.array(x) for x in DT_o]


T_o = [el for el in T_o for _ in range(30)]

fig_1 = plt.figure()
plt.plot(T_o)
plt.grid()
plt.show()

是否有一种方法可以优化列表理解或重新格式化循环以获取相同的图?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。