Matlab/Simulink仿真,构网型储能系统dq阻抗扫频,考虑了所有控制回路
Matlab/Simulink仿真构网型储能系统dq阻抗扫频考虑了所有控制回路代码加仿真都有一键运行出图这是波特图Bode Plot通常用于验证设计的控制器如PID、超前滞后校正是否符合预期的频率响应特性。上图幅频特性横轴是频率Hz纵轴是增益dB。下图相频特性横轴是频率Hz纵轴是相位度。蓝色实线你设计的数学模型传递函数的理论响应。红色空心圆圈实际系统的测量数据或辨识数据。MATLAB 代码模板%% 初始化环境clc; clear; close all;% 定义频率范围 (与你图片一致: 10^1 到 10^4 Hz)f logspace(1, 4, 100); % 100个点w 2 * pi * f; % 转换为 rad/s%% 模拟数据生成 (请替换为你自己的系统) % 这里我构造了4个不同的传递函数以此模仿你图片中的四种不同曲线形态% — Figure 1 模拟 (类似低通滤波或积分环节) —sys1 tf(1000, [1 10]); % 一个简单的一阶系统% — Figure 2 模拟 (类似带陷波或复杂极点) —% 模仿中间凹陷的形状num2 [1 2 100];den2 [1 5 105 200];sys2 tf(num2, den2);% — Figure 3 模拟 (类似PID或超前滞后) —% 模仿相位先降后升sys3 pid(1, 10, 0.1);% — Figure 4 模拟 (高频共振) —% 模仿高频处的尖峰sys4 tf(100, [1 2 1000]);%% 绘图代码 % — 绘制 Figure 1 —figure(‘Name’, ‘Figure 1’);% 获取模型响应数据[mag1, phase1] bode(sys1, w);mag1 squeeze(mag1); phase1 squeeze(phase1);% 绘制理论曲线 (蓝色实线)subplot(2,1,1);semilogx(f, 20*log10(mag1), ‘b-’, ‘LineWidth’, 1.5); grid on;ylabel(‘幅值/dB’); title(‘Figure 1: 幅频特性’);subplot(2,1,2);semilogx(f, phase1, ‘b-’, ‘LineWidth’, 1.5); grid on;ylabel(‘相位/^{circ}’); xlabel(‘f/Hz’); title(‘Figure 1: 相频特性’);% 模拟一些“实测数据” (红色圆圈) - 这里加了一点噪声来模拟真实情况noise_mag 20*log10(mag1) randn(size(mag1));noise_phase phase1 randn(size(phase1)) * 5;hold on;subplot(2,1,1); semilogx(f, noise_mag, ‘ro’); hold on; grid on;subplot(2,1,2); semilogx(f, noise_phase, ‘ro’); hold on; grid on;% — 绘制 Figure 2 —figure(‘Name’, ‘Figure 2’);[mag2, phase2] bode(sys2, w);mag2 squeeze(mag2); phase2 squeeze(phase2);subplot(2,1,1);semilogx(f, 20*log10(mag2), ‘b-’, ‘LineWidth’, 1.5); grid on;ylabel(‘幅值/dB’); title(‘Figure 2: 幅频特性’);subplot(2,1,2);semilogx(f, phase2, ‘b-’, ‘LineWidth’, 1.5); grid on;ylabel(‘相位/^{circ}’); xlabel(‘f/Hz’); title(‘Figure 2: 相频特性’);% 模拟实测点subplot(2,1,1); semilogx(f, 20*log10(mag2)randn(size(mag2)), ‘ro’); hold on; grid on;subplot(2,1,2); semilogx(f, phase2randn(size(phase2))*5, ‘ro’); hold on; grid on;% — 绘制 Figure 3 —figure(‘Name’, ‘Figure 3’);[mag3, phase3] bode(sys3, w);mag3 squeeze(mag3); phase3 squeeze(phase3);subplot(2,1,1);semilogx(f, 20*log10(mag3), ‘b-’, ‘LineWidth’, 1.5); grid on;ylabel(‘幅值/dB’); title(‘Figure 3: 幅频特性’);subplot(2,1,2);semilogx(f, phase3, ‘b-’, ‘LineWidth’, 1.5); grid on;ylabel(‘相位/^{circ}’); xlabel(‘f/Hz’); title(‘Figure 3: 相频特性’);% 模拟实测点subplot(2,1,1); semilogx(f, 20*log10(mag3)randn(size(mag3)), ‘ro’); hold on; grid on;subplot(2,1,2); semilogx(f, phase3randn(size(phase3))*5, ‘ro’); hold on; grid on;% — 绘制 Figure 4 —figure(‘Name’, ‘Figure 4’);[mag4, phase4] bode(sys4, w);mag4 squeeze(mag4); phase4 squeeze(phase4);subplot(2,1,1);semilogx(f, 20*log10(mag4), ‘b-’, ‘LineWidth’, 1.5); grid on;ylabel(‘幅值/dB’); title(‘Figure 4: 幅频特性’);subplot(2,1,2);semilogx(f, phase4, ‘b-’, ‘LineWidth’, 1.5); grid on;ylabel(‘相位/^{circ}’); xlabel(‘f/Hz’); title(‘Figure 4: 相频特性’);% 模拟实测点subplot(2,1,1); semilogx(f, 20*log10(mag4)randn(size(mag4)), ‘ro’); hold on; grid on;subplot(2,1,2); semilogx(f, phase4randn(size(phase4))*5, ‘ro’); hold on; grid on;代码关键点解析tf(num, den):这是建立传递函数模型的核心。你需要根据你的实际控制器公式填写分子num和分母den的系数。bode(sys, w):计算系统在指定频率 w 下的幅值和相位。注意 MATLAB 内部计算通常使用 rad/s但绘图时我们将其转换为 Hz 以匹配你的图片。semilogx:这是绘制波特图的关键函数它将 X 轴频率设置为对数坐标Y 轴保持线性完全符合控制理论的标准。20*log10(mag):将幅值转换为分贝dB。红色圆圈 (‘ro’):在你的图片中红色圆圈代表实验测得的数据点。在实际工程中这些数据通常来自频率响应分析仪FRA或者Simulink的System Identification Toolbox。核心控制算法代码这是图中“电压电流双环控制”和“PQ计算”等子系统内部的逻辑通常使用 MATLAB Function 或 C 语言实现。Simulink 建模脚本一段 MATLAB 脚本用于自动搭建图中主电路的拓扑结构。核心控制算法代码这部分代码对应图中中间那个大的控制子系统。它接收电压电流反馈输出调制波信号。电压电流双环控制器这是图中的核心控制逻辑外环控制直流母线电压内环控制并网电流。function [Id_ref, Iq_ref] fcn(Vdc, Vdc_ref, Id, Iq, Theta)% 双环控制核心逻辑% 输入: Vdc(直流母线电压), Vdc_ref(电压参考), Id/Iq(反馈电流), Theta(锁相环相位)% 输出: Id_ref, Iq_ref (电流内环参考实际工程中此处通常输出调制波这里简化为PI输出)%% 1. 参数定义Kp_v 0.5; % 电压环比例系数Ki_v 0.01; % 电压环积分系数Kp_i 0.1; % 电流环比例系数Ki_i 10.0; % 电流环积分系数%% 2. 外环电压控制 (生成电流参考)% 计算电压误差err_v Vdc_ref - Vdc;% 简单PI控制逻辑 (实际应用中需使用Discrete PI模块或带抗饱和的积分器)persistent Int_v;if isempty(Int_v)Int_v 0;endInt_v Int_v err_v * Ki_v * 0.0001; % 0.0001为采样时间TsId_ref_unlimited Kp_v * err_v Int_v;% 限幅 (防止电流过大)if Id_ref_unlimited 100Id_ref 100;elseif Id_ref_unlimited dq)% 使用Simulink内置的abc_to_dq变换或手动实现矩阵乘法% 这里假设已有Vd, Vq, Id, Iq% 2. 功率公式% P 1.5 * (VId VqIq)% Q 1.5 * (VId - VdIq)% 在Simulink中通常直接使用 “Instantaneous Power (3ph, 2-wire)” 模块% 或者使用上述公式在 MATLAB Function 中计算Simulink 主电路搭建脚本这段脚本会自动创建一个包含直流源、三相逆变器、LCL滤波器和电网的模型对应图中的主电路部分。function build_power_circuit% 创建模型modelName ‘GridInverter_MainCircuit’;new_system(modelName);open_system(modelName);%% 1. 添加电源模块 (DC Source) add_block(powerlib/Power Sources/DC Voltage Source, [modelName /DC Source]); set_param([modelName /DC Source], Amplitude, 800); % 800V直流 %% 2. 添加三相逆变器 (Universal Bridge) add_block(powerlib/Power Electronics/Universal Bridge, [modelName /Inverter]); set_param([modelName /Inverter], BridgeType, 3-leg Voltage Source Converter); set_param([modelName /Inverter], SnubberResistance, inf); % 去除缓冲电路 %% 3. 添加LCL滤波器 % 逆变器侧电感 L1 add_block(powerlib/Elements/Series RLC Branch, [modelName /L1]); set_param([modelName /L1], BranchType, L, Resistance, 0.01, Inductance, 2e-3); % 滤波电容 Cf add_block(powerlib/Elements/Series RLC Branch, [modelName /Cf]); set_param([modelName /Cf], BranchType, C, Capacitance, 10e-6); % 电网侧电感 L2 add_block(powerlib/Elements/Series RLC Branch, [modelName /L2]); set_param([modelName /L2], BranchType, L, Resistance, 0.01, Inductance, 0.5e-3); %% 4. 添加电网 (Three-Phase Source) add_block(powerlib/Power Sources/Three-Phase Source, [modelName /Grid]); set_param([modelName /Grid], Phase_to_phase_rms_voltage, 380); set_param([modelName /Grid], Frequency, 50); %% 5. 添加测量模块 add_block(powerlib/Measurements/Current Measurement, [modelName /Iabc_Meas]); add_block(powerlib/Measurements/Voltage Measurement, [modelName /Vabc_Meas]); %% 6. 连接模块 (简化连接逻辑) % 注意实际连接需要使用 add_line 函数坐标需精确调整 % 此处仅示意连接顺序 % DC Source - Inverter DC terminals % Inverter AC terminals - L1 - Cf - L2 - Grid % 自动排列布局 Simulink.BlockDiagram.arrangeSystem(modelName);end