P_num = (det[:, -1] == 0).sum()
时间: 2024-06-03 12:12:37 浏览: 116
这行代码是在 Python 中对一个二维数组 det 进行操作,其中 det[:, -1] 表示选取 det 数组中所有行的最后一列组成的一维数组,然后 == 0 表示将这个一维数组中所有等于 0 的元素标记为 True,不等于 0 的元素标记为 False。最后,sum() 函数将这个一维布尔数组中所有 True 的元素加起来,得到的结果就是这个数组中等于 0 的元素个数。这个个数被赋值给变量 P_num。
相关问题
N = 3; % 矩阵维度 num_trials = 10000; % 随机矩阵数量 dets = zeros(num_trials, 1); % 存储所有随机矩阵的行列式值 for i = 1:num_trials % 生成 1 和 -1 的随机矩阵 A = 2*randi([0, 1], N, N) - 1; % 计算行列式 dets(i) = det(A); end % 统计不同的行列式值 unique_dets = unique(dets); num_unique_dets = numel(unique_dets); disp(['共有 ', num2str(num_unique_dets), ' 种不同的行列式值']);如何解决行列式负值丢失的问题
在给定的代码中,行列式的值是通过 MATLAB 自带的 `det` 函数来计算的。然而,该函数对于大多数随机生成的矩阵可能会返回一个近似为零的值,而不是精确的值。这可能会导致在存储所有随机矩阵的行列式值时,一些负值可能被错误地视为零值而丢失。
为了解决这个问题,可以使用一个名为 `det_exact` 的自定义行列式计算函数来替代 MATLAB 自带的 `det` 函数。该函数可以通过计算矩阵的 LU 分解来精确计算行列式的值,从而避免了近似值的问题。以下是一个示例 `det_exact` 函数的代码:
```matlab
function d = det_exact(A)
% 精确计算 N 阶方阵 A 的行列式值
N = size(A, 1);
[L,U,P] = lu(A);
d = det(L)*det(U)*prod(diag(P)) * (-1)^(sum(diag(U)<0));
end
```
使用该函数来计算随机矩阵的行列式值,可以避免负值丢失的问题。以下是修改后的代码:
```matlab
N = 3; % 矩阵维度
num_trials = 10000; % 随机矩阵数量
dets = zeros(num_trials, 1); % 存储所有随机矩阵的行列式值
for i = 1:num_trials % 生成 1 和 -1 的随机矩阵
A = 2*randi([0, 1], N, N) - 1;
dets(i) = det_exact(A); % 使用 det_exact 函数计算行列式
end
% 统计不同的行列式值
unique_dets = unique(dets);
num_unique_dets = numel(unique_dets);
disp(['共有 ', num2str(num_unique_dets), ' 种不同的行列式值']);
```
上述代码将使用 `det_exact` 函数来计算随机矩阵的行列式值,这样就可以避免由于近似值而丢失负值的问题。
%继电式自整定调节器 clear; clc; %% 初值 Ts=0.001; L=300; yp=0; d=1; %% 传递函数离散化 Gs=tf(1,conv(conv([10,1],[5,1]),[2,1])); dsys =c2d(Gs,Ts,'tustin '); [num,den]=tfdata(dsys,'v'); len=length(den); %% 等幅振荡 for t=1:len-1 y(t)=0; u(t)=0; e(t)=yp-y(t); time(t)=t*Ts; end for t=len:L/Ts if e(t-1)>0 u(t)=d; else u(t)=-d; end y(t)=-den(2)*y(t-1)-den(3)*y(t-2)-den(4)*y(t-3)+num(1)*u(t)+num(2)*u(t-1)+num(3)*u(t-2)+num(4)*u(t-3); e(t)=yp-y(t); time(t)=t*Ts; end figure(1) plot(time,y,'DisplayName','y'); xlabel('时间t/s'); ylabel('输出值'); title('继电器控制下被控对象输出值'); %% 周期计算 i=1; for t=2:L/Ts if y(t)>y(t-1) t1(i)=t; i=i+1; end end i=1; for t=2:length(t1) if (t1(t)-t1(t-1))>1 t2(i)=t1(t); i=i+1; end end sum=0; for t=ceil((1/2)*length(t2))+1:length(t2) sum=sum+(t2(t)-t2(t-1)); end %% PID整定参数 Ku=4*d/(pi*max(y)); Tu=Ts*sum/(length(t2)-ceil((1/2)*length(t2))); %P控制 %Kc=0.5*Ku;Ti=0;Td=0; %Kp=Kc; Ki=0; Kd=0; %PI控制 %Kc=0.4*Ku;Ti=0.8*Tu;Td=0; %PID控制 Kc=0.6*Ku; Ti=0.5*Tu; Td=0.12*Tu; Kp=Kc; Ki=Kp*Ts/Ti; Kd=Kp*Td/Ts; %% PID控制 for t=1:len y(t)=0; u(t)=0; e(t)=yp-y(t); time(t)=t*Ts; end yp=1; for t=len:L/Ts det_u=Kp*(e(t-1)-e(t-2))+Ki*e(t-1)+Kd*(e(t-1)-2*e(t-2)+e(t-3)); u(t)=u(t-1)+det_u; y(t)=(1/den(1))*(-den(2)*y(t-1)-den(3)*y(t-2)-den(4)*y(t-3)+num(1)*u(t)+num(2)*u(t-1)+num(3)*u(t-2)+num(4)*u(t-3)); e(t)=yp-y(t); time(t)=t*Ts; end figure(2) plot(time,y,'DisplayName','y'); xlabel('时间t/s'); ylabel('输出值'); title('P控制下被控对象输出值');转成python语言
以下是将MATLAB代码转换为Python代码的结果:
```python
import numpy as np
from scipy.signal import convolve, cont2discrete
import matplotlib.pyplot as plt
# 初值
Ts = 0.001
L = 300
yp = 0
d = 1
# 传递函数离散化
Gs = np.poly1d([1], r=False) / np.poly1d([10, 1], r=False) / np.poly1d([5, 1], r=False) / np.poly1d([2, 1], r=False)
dsys = cont2discrete((Gs.num, Gs.den), Ts, method='tustin')
num, den = dsys.num[0], dsys.den[0]
len_den = len(den)
# 等幅振荡
y = np.zeros(L)
u = np.zeros(L)
e = np.zeros(L)
time = np.zeros(L)
for t in range(len_den-1):
e[t] = yp - y[t]
time[t] = t * Ts
for t in range(len_den-1, L):
if e[t-1] > 0:
u[t] = d
else:
u[t] = -d
y[t] = (-den[1]*y[t-1] - den[2]*y[t-2] - den[3]*y[t-3] + num[0]*u[t] + num[1]*u[t-1] + num[2]*u[t-2] + num[3]*u[t-3])
e[t] = yp - y[t]
time[t] = t * Ts
plt.figure(1)
plt.plot(time, y, label='y')
plt.xlabel('时间t/s')
plt.ylabel('输出值')
plt.title('继电器控制下被控对象输出值')
# 周期计算
t1 = []
for t in range(1, L):
if y[t] > y[t-1]:
t1.append(t)
t2 = []
for t in range(1, len(t1)):
if t1[t] - t1[t-1] > 1:
t2.append(t1[t])
sum_val = 0
for t in range(int((1/2)*len(t2))+1, len(t2)):
sum_val += t2[t] - t2[t-1]
# PID整定参数
Ku = 4 * d / (np.pi * np.max(y))
Tu = Ts * sum_val / (len(t2) - int((1/2)*len(t2)))
Kc = 0.6 * Ku
Ti = 0.5 * Tu
Td = 0.12 * Tu
Kp = Kc
Ki = Kp * Ts / Ti
Kd = Kp * Td / Ts
# PID控制
y = np.zeros(L)
u = np.zeros(L)
e = np.zeros(L)
time = np.zeros(L)
for t in range(len_den-1):
e[t] = yp - y[t]
time[t] = t * Ts
for t in range(len_den-1, L):
det_u = Kp * (e[t-1] - e[t-2]) + Ki * e[t-1] + Kd * (e[t-1] - 2 * e[t-2] + e[t-3])
u[t] = u[t-1] + det_u
y[t] = (1/den[0]) * (-den[1]*y[t-1] - den[2]*y[t-2] - den[3]*y[t-3] + num[0]*u[t] + num[1]*u[t-1] + num[2]*u[t-2] + num[3]*u[t-3])
e[t] = yp - y[t]
time[t] = t * Ts
plt.figure(2)
plt.plot(time, y, label='y')
plt.xlabel('时间t/s')
plt.ylabel('输出值')
plt.title('P控制下被控对象输出值')
plt.show()
```
需要注意的是,Python中的绘图需要使用`matplotlib`库。
阅读全文
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)