from catboost import CatBoostClassifier import pandas as pd # 导入pandas工具库 import matplotlib.pyplot as plt pd.set_option('display.max_columns', 1000) pd.set_option('display.max_rows', 1000) pd.set_option('display.max_colwidth', 1000) import numpy as np import matplotlib.pyplot as plt """ Q 系统噪声 R 测量噪声 X(k|k-1) 上一次状态预测结果 X(k-1|k-1) 上一时刻的最优预测值 P(k|k-1) X(k|k-1)对应的convariance协方差 P(k-1|k-1) X(k-1|k-1) 对应的convariance协方差 """ x_last = 0 p_last = 0 Q = 0.01 # 系统噪声 R = 0.5 # 测量噪声 def kalman(z_measure, x_last=0, p_last=0, Q=0.018, R=0.0542): x_mid = x_last p_mid = p_last + Q kg = p_mid / (p_mid + R) x_now = x_mid + kg * (z_measure - x_mid) p_now = (1 - kg) * p_mid p_last = p_now x_last = x_now return x_now, p_last, x_last path = 'C:/Users/calm夜/Desktop/作业1.xlsx' data_B = pd.read_excel(path, header=None) x = list(data_B.iloc[::, 0]) y=[] for i in range(len(x)): pred, p_last, x_last = kalman(x[i], x_last, p_last, Q, R) y.append(pred) ax1 = plt.subplot(1,2,1) ax2 = plt.subplot(1,2,2) plt.sca(ax1) plt.plot(x, color="g") # 测量值 plt.sca(ax2) plt.plot(y, color="r") # 预测值 plt.show() 把python代码转成matlab代码
时间: 2024-04-10 17:33:33 浏览: 98
Sure! Here's the equivalent MATLAB code for your Python code:
```matlab
Q = 0.01; % 系统噪声
R = 0.5; % 测量噪声
x_last = 0;
p_last = 0;
path = 'C:/Users/calm夜/Desktop/作业1.xlsx';
data_B = xlsread(path);
x = data_B(:, 1);
y = zeros(size(x));
for i = 1:length(x)
x_mid = x_last;
p_mid = p_last + Q;
kg = p_mid / (p_mid + R);
x_now = x_mid + kg * (x(i) - x_mid);
p_now = (1 - kg) * p_mid;
p_last = p_now;
x_last = x_now;
y(i) = x_now;
end
ax1 = subplot(1,2,1);
plot(x, 'g'); % 测量值
ax2 = subplot(1,2,2);
plot(y, 'r'); % 预测值
```
Please note that in MATLAB, you will need to use the `xlsread` function to read data from an Excel file. Also, the `subplot` function is used to create subplots, and the color should be specified as a string ('g' for green and 'r' for red).
阅读全文