buck-boost电路MATLAB仿真

该脚本构建了MATLAB R2021b中的反激式Buck-Boost变换器开环模型,输入电压24V,开关频率100kHz,占空比0.4,理论输出电压为-16V。通过仿真与验证,结果显示输出电压平均值接近理想值,效率达70%以上,电感电流连续,输出纹波小于5%,验证通过。

Matlab 复制代码
function build_buck_boost_model()
% Build an open-loop inverting Buck-Boost model for MATLAB R2021b.
% Run this script once from the folder containing both M-files.

model = 'buck_boost_r2021b';
modelFile = [model '.slx'];

if bdIsLoaded(model)
    close_system(model, 0);
end

new_system(model);

% Electrical parameters are stored in the model workspace so the SLX file
% remains self-contained when moved to another computer.
mw = get_param(model, 'ModelWorkspace');
assignin(mw, 'Vin', 24);
assignin(mw, 'L', 100e-6);
assignin(mw, 'L_dcr', 10e-3);
assignin(mw, 'C_out', 470e-6);
assignin(mw, 'R_load', 20);
assignin(mw, 'f_sw', 100e3);
assignin(mw, 'D', 0.4);
assignin(mw, 'R_src', 10e-3);
assignin(mw, 'R_on', 10e-3);
assignin(mw, 'V_f', 0.8);

% Convert the duty-cycle variable into a pulse width for the gate source.
add_block('spsDCVoltageSourceLib/DC Voltage Source', ...
    [model '/Vin'], 'Position', [60 240 110 300]);
set_param([model '/Vin'], 'Amplitude', 'Vin', ...
    'Measurements', 'None');

% A small source resistance avoids an ideal voltage source being connected
% directly in parallel with an input capacitor.
add_block('spsSeriesRLCBranchLib/Series RLC Branch', ...
    [model '/R_src'], 'Position', [125 265 175 335]);
set_param([model '/R_src'], 'BranchType', 'R', ...
    'Resistance', 'R_src', 'Measurements', 'None');

add_block('spsIGBTLib/IGBT', [model '/Q1'], ...
    'Position', [230 225 285 315]);
set_param([model '/Q1'], 'Ron', 'R_on', 'Lon', '0', ...
    'Vf', 'V_f', 'Tf', '1e-7', 'Tt', '2e-7', ...
    'IC', '0', 'Measurements', 'off');

add_block('spsCurrentMeasurementLib/Current Measurement', ...
    [model '/I_L'], 'Position', [350 250 390 290]);
set_param([model '/I_L'], 'OutputType', 'Complex', ...
    'PhasorSimulation', 'off', 'PSBequivalent', '0');

add_block('spsSeriesRLCBranchLib/Series RLC Branch', ...
    [model '/L1'], 'Position', [465 235 515 305]);
set_param([model '/L1'], 'BranchType', 'RL', ...
    'Resistance', 'L_dcr', 'Inductance', 'L', ...
    'SetiL0', 'off', 'InitialCurrent', '0', ...
    'Measurements', 'None');

add_block('spsDiodeLib/Diode', [model '/D1'], ...
    'Position', [350 380 420 430]);
set_param([model '/D1'], 'Ron', 'R_on', 'Lon', '0', ...
    'Vf', 'V_f', 'IC', '0', 'UseSnubber', 'off', ...
    'Rs', 'inf', 'Cs', 'inf', 'Measurements', 'off');

add_block('spsSeriesRLCBranchLib/Series RLC Branch', ...
    [model '/C_out'], 'Position', [585 355 635 465]);
set_param([model '/C_out'], 'BranchType', 'C', ...
    'Capacitance', 'C_out', 'Setx0', 'on', ...
    'InitialVoltage', '-16', 'Measurements', 'None');

add_block('spsSeriesRLCBranchLib/Series RLC Branch', ...
    [model '/R_load'], 'Position', [720 355 770 465]);
set_param([model '/R_load'], 'BranchType', 'R', ...
    'Resistance', 'R_load', 'Measurements', 'None');

add_block('spsVoltageMeasurementLib/Voltage Measurement', ...
    [model '/V_out'], 'Position', [630 245 670 285]);
set_param([model '/V_out'], 'OutputType', 'Magnitude', ...
    'PhasorSimulation', 'off', 'PSBequivalent', '0');

add_block('spsGroundLib/Ground', [model '/GND'], ...
    'Position', [475 520 505 550]);
add_block('spsGroundLib/Ground', [model '/GND_OUT'], ...
    'Position', [760 520 790 550]);

add_block('simulink/Sources/Constant', [model '/Duty'], ...
    'Position', [35 355 105 385]);
set_param([model '/Duty'], 'Value', 'D');

add_block('spsPWMGeneratorDCDCLib/PWM Generator (DC-DC)', ...
    [model '/PWM'], 'Position', [35 410 135 480]);
set_param([model '/PWM'], 'Fsw', 'f_sw', 'Ts', '0');

add_block('spspowerguiLib/powergui', [model '/powergui'], ...
    'Position', [60 90 160 150]);
set_param([model '/powergui'], 'SimulationMode', 'Continuous', ...
    'DisplayStyle', '1');

add_block('simulink/Signal Routing/Mux', [model '/Mux'], ...
    'Position', [735 215 745 255]);
set_param([model '/Mux'], 'Inputs', '3');

add_block('simulink/Sinks/Scope', [model '/Vout and IL'], ...
    'Position', [810 220 850 260]);
set_param([model '/Vout and IL'], 'NumInputPorts', '1', ...
    'OpenAtSimulationStart', 'off');

add_block('built-in/Outport', [model '/y'], ...
    'Position', [850 225 880 245]);

% Power circuit: Vin+ -> Rsrc -> Q1 -> current sensor -> L -> ground.
connectPhysical(model, 'Vin', 'RConn', 1, 'R_src', 'LConn', 1);
connectPhysical(model, 'R_src', 'RConn', 1, 'Q1', 'LConn', 1);
connectPhysical(model, 'Q1', 'RConn', 1, 'I_L', 'LConn', 1);
connectPhysical(model, 'I_L', 'RConn', 1, 'L1', 'LConn', 1);
connectPhysical(model, 'L1', 'RConn', 1, 'GND', 'LConn', 1);

% The source return and the inductor return share the ground reference.
connectPhysical(model, 'Vin', 'LConn', 1, 'GND', 'LConn', 1);

% Switch node to diode cathode; diode anode goes to the negative output rail.
connectPhysical(model, 'Q1', 'RConn', 1, 'D1', 'RConn', 1);
connectPhysical(model, 'D1', 'LConn', 1, 'C_out', 'LConn', 1);
connectPhysical(model, 'C_out', 'LConn', 1, 'R_load', 'LConn', 1);
connectPhysical(model, 'C_out', 'LConn', 1, 'V_out', 'LConn', 1);

connectPhysical(model, 'C_out', 'RConn', 1, 'GND_OUT', 'LConn', 1);
connectPhysical(model, 'R_load', 'RConn', 1, 'GND_OUT', 'LConn', 1);
connectPhysical(model, 'V_out', 'LConn', 2, 'GND_OUT', 'LConn', 1);

% Gate drive and signal monitoring.
connectSignal(model, 'Duty', 1, 'PWM', 1);
connectSignal(model, 'PWM', 1, 'Q1', 1);
connectSignal(model, 'I_L', 1, 'Mux', 1);
connectSignal(model, 'V_out', 1, 'Mux', 2);
connectSignal(model, 'PWM', 1, 'Mux', 3);
connectSignal(model, 'Mux', 1, 'Vout and IL', 1);
connectSignal(model, 'Mux', 1, 'y', 1);

set_param(model, 'Description', sprintf([ ...
    'Inverting Buck-Boost converter, MATLAB R2021b\n' ...
    'Vin = 24 V, Rsrc = 10 mOhm, fsw = 100 kHz, D = 0.4, L = 100 uH, ' ...
    'C = 470 uF, R = 20 ohm\n' ...
    'Expected ideal output: Vout = -Vin*D/(1-D) = -16 V\n' ...
    'Mux inputs: inductor current, output voltage, and gate command.']));

set_param(model, ...
    'StopTime', '0.02', ...
    'Solver', 'ode23tb', ...
    'MaxStep', '1e-6', ...
    'RelTol', '1e-4', ...
    'AbsTol', '1e-6', ...
    'SaveTime', 'on', ...
    'SaveOutput', 'on', ...
    'OutputSaveName', 'yout', ...
    'ReturnWorkspaceOutputs', 'on');

save_system(model, modelFile);
print(['-s' model], '-dpng', '-r160', 'buck_boost_model.png');
fprintf('Created %s\n', fullfile(pwd, modelFile));
end

function connectPhysical(model, srcBlock, srcKind, srcPort, ...
    dstBlock, dstKind, dstPort)
src = getPortHandle(model, srcBlock, srcKind, srcPort);
dst = getPortHandle(model, dstBlock, dstKind, dstPort);
add_line(model, src, dst, 'autorouting', 'on');
end

function connectSignal(model, srcBlock, srcPort, dstBlock, dstPort)
src = getPortHandle(model, srcBlock, 'Outport', srcPort);
dst = getPortHandle(model, dstBlock, 'Inport', dstPort);
add_line(model, src, dst, 'autorouting', 'on');
end

function handle = getPortHandle(model, block, kind, index)
ports = get_param([model '/' block], 'PortHandles');
values = ports.(kind);
if isempty(values) || index > numel(values)
    error('Port %s(%d) is unavailable on %s/%s.', ...
        kind, index, model, block);
end
handle = values(index);
end
Matlab 复制代码
function results = validate_buck_boost_model()
% Validate the Buck-Boost simulation using the logged output waveforms.
%
% The model Mux contains:
%   1. Inductor current
%   2. Output voltage
%   3. Gate command
%
% Run from the folder containing buck_boost_r2021b.slx.

model = 'buck_boost_r2021b';
if ~bdIsLoaded(model)
    load_system([model '.slx']);
end

mw = get_param(model, 'ModelWorkspace');
Vin = mw.getVariable('Vin');
D = mw.getVariable('D');
fsw = mw.getVariable('f_sw');
Rload = mw.getVariable('R_load');

simOut = sim(model, 'StopTime', '0.02', ...
    'ReturnWorkspaceOutputs', 'on');

logged = simOut.get('yout').get(1).Values;
t = logged.Time;
data = logged.Data;
if size(data, 2) < 3
    error('Expected Mux channels: inductor current, Vout, and gate.');
end

inductorCurrent = data(:, 1);
outputVoltage = data(:, 2);
gate = data(:, 3);

% Use the last 5 ms of the 20 ms run as the steady-state window.
windowStart = max(t(1), t(end) - 5e-3);
windowEnd = t(end);
windowMask = t >= windowStart & t <= windowEnd;
tw = t(windowMask);
vw = outputVoltage(windowMask);
iw = inductorCurrent(windowMask);

avgVout = trapzMean(tw, vw);
avgIL = trapzMean(tw, iw);
minIL = min(iw);
maxIL = max(iw);
voutPp = max(vw) - min(vw);
measuredDuty = zohAverage(t, gate, windowStart, windowEnd);

idealVout = -Vin * D / (1 - D);
voltageError = abs(avgVout - idealVout);
voltageErrorPercent = 100 * voltageError / abs(idealVout);

inputPower = Vin * zohAverage(t, inductorCurrent .* (gate > 0.5), ...
    windowStart, windowEnd);
outputPower = trapzMean(tw, vw.^2) / Rload;
efficiency = outputPower / inputPower;

checks = struct();
checks.correctPolarity = avgVout < 0;
checks.dutyWithinTolerance = abs(measuredDuty - D) <= 0.02;
checks.voltageNearIdeal = voltageErrorPercent <= 20;
checks.lowOutputRipple = voutPp / abs(avgVout) <= 0.05;
checks.nonnegativeCurrent = minIL >= -0.05;
checks.validEfficiency = efficiency > 0.70 && efficiency <= 1.05;
checks.passed = all(struct2array(checks));

results = struct();
results.time = t;
results.inductorCurrent = inductorCurrent;
results.outputVoltage = outputVoltage;
results.gate = gate;
results.steadyStateWindow = [windowStart windowEnd];
results.averageOutputVoltage = avgVout;
results.idealOutputVoltage = idealVout;
results.voltageErrorPercent = voltageErrorPercent;
results.outputVoltageRipple = voutPp;
results.averageInductorCurrent = avgIL;
results.minimumInductorCurrent = minIL;
results.maximumInductorCurrent = maxIL;
results.measuredDuty = measuredDuty;
results.inputPower = inputPower;
results.outputPower = outputPower;
results.efficiency = efficiency;
results.checks = checks;

printReport(results, Vin, D, fsw);
plotValidation(results, fsw);

if checks.passed
    fprintf('\nVALIDATION PASSED\n');
else
    fprintf('\nVALIDATION FAILED\n');
end
end

function value = trapzMean(t, x)
value = trapz(t, x) / (t(end) - t(1));
end

function value = zohAverage(t, x, tStart, tEnd)
segmentStart = t(1:end-1);
segmentEnd = t(2:end);
overlap = max(0, min(segmentEnd, tEnd) - max(segmentStart, tStart));
value = sum(overlap .* x(1:end-1)) / sum(overlap);
end

function printReport(r, Vin, D, fsw)
fprintf('\n=== Buck-Boost simulation validation ===\n');
fprintf('Input voltage:                 %8.3f V\n', Vin);
fprintf('Commanded duty cycle:          %8.3f\n', D);
fprintf('Steady-state window:           %6.1f to %.1f ms\n', ...
    r.steadyStateWindow(1) * 1e3, r.steadyStateWindow(2) * 1e3);
fprintf('Measured duty cycle:           %8.4f\n', r.measuredDuty);
fprintf('Average output voltage:        %8.3f V\n', r.averageOutputVoltage);
fprintf('Ideal output voltage:          %8.3f V\n', r.idealOutputVoltage);
fprintf('Ideal-voltage error:           %8.2f %%\n', r.voltageErrorPercent);
fprintf('Output voltage ripple (p-p):   %8.4f V\n', r.outputVoltageRipple);
fprintf('Average inductor current:      %8.3f A\n', r.averageInductorCurrent);
fprintf('Inductor current range:        %8.3f to %.3f A\n', ...
    r.minimumInductorCurrent, r.maximumInductorCurrent);
fprintf('Input power:                   %8.3f W\n', r.inputPower);
fprintf('Output power:                  %8.3f W\n', r.outputPower);
fprintf('Conversion efficiency:         %8.2f %%\n', 100 * r.efficiency);
fprintf('Switching frequency:           %8.1f kHz\n', fsw / 1e3);

names = fieldnames(r.checks);
fprintf('\nChecks:\n');
for k = 1:numel(names)
    if strcmp(names{k}, 'passed')
        continue;
    end
    fprintf('  %-28s %s\n', names{k}, ...
        passText(r.checks.(names{k})));
end
end

function text = passText(value)
if value
    text = 'PASS';
else
    text = 'FAIL';
end
end

function plotValidation(r, fsw)
tMs = r.time * 1e3;
windowStartMs = r.steadyStateWindow(1) * 1e3;

figure('Color', 'w', 'Position', [100 80 1100 760]);

subplot(3, 1, 1);
plot(tMs, r.outputVoltage, 'LineWidth', 1.1);
hold on;
yline(r.averageOutputVoltage, 'r--', 'Steady-state average');
yline(r.idealOutputVoltage, 'k:', 'Ideal -Vin D/(1-D)');
grid on;
xlabel('Time (ms)');
ylabel('Voltage (V)');
title('Output voltage and expected steady-state value');
legend('Simulation', 'Measured average', 'Ideal value', ...
    'Location', 'best');

subplot(3, 1, 2);
plot(tMs, r.inductorCurrent, 'LineWidth', 1.0);
grid on;
xlabel('Time (ms)');
ylabel('Current (A)');
title('Inductor current');

zoomStart = max(r.time(end) - 3 / fsw, r.time(end) - 1e-3);
zoomMask = r.time >= zoomStart;
subplot(3, 1, 3);
plot(r.time(zoomMask) * 1e6, ...
    r.outputVoltage(zoomMask), 'LineWidth', 1.1);
grid on;
xlabel('Time (us)');
ylabel('Voltage (V)');
title('Output voltage zoom: three switching periods');

exportgraphics(gcf, 'buck_boost_validation.png', ...
    'Resolution', 160);
fprintf('\nValidation image: %s\n', ...
    fullfile(pwd, 'buck_boost_validation.png'));
fprintf('Steady-state window begins at %.1f ms.\n', windowStartMs);
end
相关推荐
逆向编程1 天前
MATLAB高级数据结构:cell、struct与table深度应用
开发语言·数据结构·matlab
星夜离尘561 天前
MATLAB 刚体坐标变换:点、方向和逆变换为什么不能混用
matlab
Evand J2 天前
【PMSM滤波例程2】PMSM未知负载转矩观测EKF。MATLAB例程(负载增广EKF与实际仿真结果)。附下载链接
开发语言·matlab·电机
机器学习之心2 天前
基于贝叶斯物理信息神经网络的矿产预测与不确定性量化,MATLAB代码及复现报告
人工智能·神经网络·matlab·贝叶斯物理信息神经网络
Evand J3 天前
【PMSM例程4】PMSM电阻辨识与模型补偿EKF,MATLAB例程。附下载链接,包运行成功
开发语言·matlab·电机·ekf·卡尔曼·pmsm
slandarer3 天前
MATLAB | 文艺复兴:万圣节使用 MATLAB 绘制黑人抬棺动画
matlab·黑人抬棺
大江东去浪淘尽千古风流人物3 天前
【FLORA】机器人视频标注引擎解析:三模型如何产出动作指令与四类奖励信号
matlab·机器人·跨模态·遥感·图像匹配·1024程序员节·特征描述符
星夜离尘563 天前
MATLAB ADC 码流触顶诊断:命中数、缺口分段与削顶不可辨识性
开发语言·matlab
Evand J4 天前
【MATLAB例程】平面下,5基站对待定位点的定位。基于距离辅助下的AOA。完整代码下载链接
开发语言·matlab·平面·定位·导航·测距·aoa