模型预测控制(MPC)的Matlab代码

这段代码使用二次规划来解决MPC优化问题在每个时间步,并将系统的状态存储在x_history中用于绘图。所得到的图显示了系统状态随时间的演变,服从于MPC问题中指定的约束和目标。

% Define system parameters
A = [0.8, 0.2; 0.6, 0.4];
B = [0.2; 0.1];
C = [0.5, 0.5];
x0 = [0; 0];

% Define MPC parameters
horizon = 10;
Q = eye(2);
R = 0.1;

% Define optimization options
options = optimoptions('quadprog', 'Display', 'off');

% Initialize states and inputs
x = x0;
u = zeros(horizon, 1);

% Loop for a number of steps
for i = 1:100
    % Formulate and solve optimization problem
    H = 2 * blkdiag(Q, R);
    f = zeros(2 * horizon, 1);
    Aeq = [];
    beq = [];
    lb = [];
    ub = [];
    for j = 1:horizon
        if j == 1
            Aeq = [Aeq; C * x];
            beq = [beq; x0];
        else
            Aeq = [Aeq; C * A^(j-1)];
            beq = [beq; 0];
        end
        Aeq = [Aeq, zeros(size(Aeq, 1), horizon - j)];
        lb = [lb; -inf];
        ub = [ub; inf];
    end
    Aineq = [zeros(horizon, horizon), -eye(horizon)];
    bineq = zeros(horizon, 1);
    [z, fval, exitflag] = quadprog(H, f, Aineq, bineq, Aeq, beq, lb, ub, [], options);
    if exitflag ~= 1
        disp('Optimization failed');
        break;
    end
    u(1) = z(1);
    x = A * x + B * u(1);
    
    % Shift inputs for next iteration
    u = [u(2:end); 0];
    
    % Store states for plotting
    x_history(:, i) = x;
end

% Plot results
figure
hold on
plot(x_history(1, :), x_history(2, :), 'b');
xlabel('x1');
ylabel('x2');
title('Model Predictive Control');
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真的是小恐龙吗?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值