news 2026/8/6 2:19:41

水箱水位控制系统MATLAB实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
水箱水位控制系统MATLAB实现

水箱水位控制系统MATLAB实现,包括建模、控制器设计和仿真分析

水箱水位控制系统建模

系统微分方程

单容水箱系统可以用一阶惯性加纯滞后模型描述:

A·dh/dt = Q_in - Q_out

其中:

  • A:水箱截面积 (m²)
  • h:水位高度 (m)
  • Q_in:进水流量 (m³/s)
  • Q_out:出水流量 (m³/s)

MATLAB实现代码

1. 水箱系统模型定义

function[h,Q_in,t]=water_tank_system(controller_type,h_ref,sim_time)% 水箱水位控制系统仿真% 输入参数:% controller_type: 控制器类型 ('PID', 'Fuzzy', 'LQR')% h_ref: 目标水位 (m)% sim_time: 仿真时间 (s)% 系统参数A=2.0;% 水箱截面积 (m²)h0=0.5;% 初始水位 (m)Q_out=0.1;% 固定出水流量 (m³/s)Q_max=0.5;% 最大进水流量 (m³/s)Q_min=0;% 最小进水流量 (m³/s)% 仿真参数dt=0.1;% 采样时间 (s)t=0:dt:sim_time;N=length(t);% 初始化变量h=zeros(1,N);h(1)=h0;Q_in=zeros(1,N);e=zeros(1,N);e_int=0;% 误差积分项e_prev=0;% 上一次误差% 控制器参数switchcontroller_typecase'PID'Kp=1.5;Ki=0.1;Kd=0.5;case'Fuzzy'% 模糊逻辑控制器参数fis=create_fuzzy_controller();case'LQR'% 状态空间模型A_sys=-0.1;B_sys=1/A;C_sys=1;D_sys=0;Q_lqr=10;R_lqr=1;K_lqr=lqr(A_sys,B_sys,Q_lqr,R_lqr);end% 主仿真循环fork=1:N-1% 计算当前误差e(k)=h_ref-h(k);% 控制器计算switchcontroller_typecase'PID'% PID控制器e_int=e_int+e(k)*dt;e_deriv=(e(k)-e_prev)/dt;Q_in(k)=Kp*e(k)+Ki*e_int+Kd*e_deriv;e_prev=e(k);case'Fuzzy'% 模糊逻辑控制器Q_in(k)=evalfis(fis,[e(k),e(k)-e_prev]);e_prev=e(k);case'LQR'% LQR控制器Q_in(k)=-K_lqr*(h(k)-h_ref);end% 控制输入饱和限制Q_in(k)=max(Q_min,min(Q_max,Q_in(k)));% 系统动态更新 (欧拉法)dh_dt=(Q_in(k)-Q_out)/A;h(k+1)=h(k)+dh_dt*dt;% 添加测量噪声h(k+1)=h(k+1)+0.01*randn;end% 最后时刻的控制量Q_in(N)=Q_in(N-1);end

2. PID控制器设计

function[Kp,Ki,Kd]=pid_tuning_method(method,A,tau)% PID控制器参数整定% method: 整定方法 ('ZN', 'CC', 'IMC')% A: 水箱截面积, tau: 时间常数switchmethodcase'ZN'% Ziegler-Nichols方法Ku=2.5;Pu=2*pi*tau;Kp=0.6*Ku;Ki=2*Kp/Pu;Kd=Kp*Pu/8;case'CC'% Chien-Hrones-Reswick方法Kp=0.6*A/tau;Ki=Kp/(2.5*tau);Kd=0.5*Kp*tau;case'IMC'% 内模控制lambda=1.5*tau;% 滤波器时间常数Kp=A/lambda;Ki=Kp/tau;Kd=0;endfprintf('PID参数: Kp=%.3f, Ki=%.3f, Kd=%.3f\n',Kp,Ki,Kd);end

3. 模糊逻辑控制器

functionfis=create_fuzzy_controller()% 创建模糊逻辑控制器fis=newfis('WaterLevelControl');% 输入变量1: 误差efis=addvar(fis,'input','error',[-1,1]);fis=addmf(fis,'input',1,'NB','trapmf',[-1-1-0.6-0.3]);fis=addmf(fis,'input',1,'NS','trimf',[-0.5-0.20]);fis=addmf(fis,'input',1,'Z','trimf',[-0.100.1]);fis=addmf(fis,'input',1,'PS','trimf',[00.20.5]);fis=addmf(fis,'input',1,'PB','trapmf',[0.30.611]);% 输入变量2: 误差变化率defis=addvar(fis,'input','delta_error',[-0.5,0.5]);fis=addmf(fis,'input',2,'N','trapmf',[-0.5-0.5-0.2-0.05]);fis=addmf(fis,'input',2,'Z','trimf',[-0.100.1]);fis=addmf(fis,'input',2,'P','trapmf',[0.050.20.50.5]);% 输出变量: 控制量Q_infis=addvar(fis,'output','control',[-0.5,0.5]);fis=addmf(fis,'output',1,'NB','trapmf',[-0.5-0.5-0.3-0.1]);fis=addmf(fis,'output',1,'NS','trimf',[-0.2-0.10]);fis=addmf(fis,'output',1,'Z','trimf',[-0.0500.05]);fis=addmf(fis,'output',1,'PS','trimf',[00.10.2]);fis=addmf(fis,'output',1,'PB','trapmf',[0.10.30.50.5]);% 模糊规则ruleList=[11111;% 误差NB, 变化率N -> 控制NB12111;13211;21111;22311;23411;31211;32311;33411;41211;42411;43511;51411;52511;53511;];fis=addrule(fis,ruleList);end

4. 完整的仿真比较系统

functionwater_tank_control_comparison()% 水箱水位控制系统对比仿真close all;clc;% 仿真参数h_ref=1.0;% 目标水位 (m)sim_time=100;% 仿真时间 (s)fprintf('=== 水箱水位控制系统仿真 ===\n');fprintf('目标水位: %.1f m\n',h_ref);fprintf('仿真时间: %d s\n\n',sim_time);% 测试不同控制器controllers={'PID','Fuzzy','LQR'};colors={'r','b','g'};line_styles={'-','--',':'};figure('Position',[100,100,1400,900]);% 存储性能指标performance=struct();fori=1:length(controllers)controller_type=controllers{i};fprintf('仿真 %s 控制器...\n',controller_type);% 运行仿真[h,Q_in,t]=water_tank_system(controller_type,h_ref,sim_time);% 计算性能指标[ise,iae,itae,overshoot,settling_time]=...calculate_performance(h,h_ref,t);performance.(controller_type)=struct(...'ISE',ise,'IAE',iae,'ITAE',itae,...'Overshoot',overshoot,'SettlingTime',settling_time);% 绘制水位响应subplot(2,2,1);plot(t,h,[colors{i},line_styles{i}],'LineWidth',2);hold on;% 绘制控制输入subplot(2,2,2);plot(t,Q_in,[colors{i},line_styles{i}],'LineWidth',2);hold on;% 绘制误差subplot(2,2,3);error=h_ref-h;plot(t,error,[colors{i},line_styles{i}],'LineWidth',2);hold on;end% 完善图形1: 水位响应subplot(2,2,1);plot([t(1),t(end)],[h_ref,h_ref],'k--','LineWidth',1,...'DisplayName','目标水位');title('水位响应曲线');xlabel('时间 (s)');ylabel('水位高度 (m)');legend([controllers,{'目标水位'}],'Location','best');grid on;% 完善图形2: 控制输入subplot(2,2,2);title('控制输入 (进水流量)');xlabel('时间 (s)');ylabel('流量 (m³/s)');legend(controllers,'Location','best');grid on;% 完善图形3: 跟踪误差subplot(2,2,3);title('跟踪误差');xlabel('时间 (s)');ylabel('误差 (m)');legend(controllers,'Location','best');grid on;% 性能指标表格subplot(2,2,4);axis off;% 创建性能比较表格performance_data=zeros(length(controllers),5);fori=1:length(controllers)ctrl=controllers{i};perf=performance.(ctrl);performance_data(i,:)=[perf.ISE,perf.IAE,perf.ITAE,...perf.Overshoot,perf.SettlingTime];end% 显示性能表格column_names={'ISE','IAE','ITAE','超调量(%)','调节时间(s)'};row_names=controllers;uitable('Data',performance_data,...'ColumnName',column_names,...'RowName',row_names,...'Position',[300,50,450,100]);sgtitle('水箱水位控制系统 - 控制器性能比较','FontSize',14,'FontWeight','bold');% 显示性能总结fprintf('\n=== 性能指标总结 ===\n');fprintf('控制器\t\tISE\t\tIAE\t\tITAE\t\t超调量(%%)\t调节时间(s)\n');fprintf('-------------------------------------------------------------------\n');fori=1:length(controllers)ctrl=controllers{i};perf=performance.(ctrl);fprintf('%s\t\t%.3f\t\t%.3f\t\t%.3f\t\t%.1f\t\t%.1f\n',...ctrl,perf.ISE,perf.IAE,perf.ITAE,...perf.Overshoot,perf.SettlingTime);endendfunction[ise,iae,itae,overshoot,settling_time]=calculate_performance(h,h_ref,t)% 计算控制系统性能指标error=h_ref-h;dt=t(2)-t(1);% ISE: 误差平方积分ise=sum(error.^2)*dt;% IAE: 绝对误差积分iae=sum(abs(error))*dt;% ITAE: 时间乘绝对误差积分itae=sum(t.*abs(error))*dt;% 超调量max_overshoot=max(h)-h_ref;overshoot=max(0,(max_overshoot/h_ref)*100);% 调节时间 (进入±5%误差带的时间)error_band=0.05*h_ref;settled_indices=find(abs(error)<=error_band);if~isempty(settled_indices)% 找到持续保持在误差带内的时间点fori=1:length(settled_indices)ifall(abs(error(settled_indices(i):end))<=error_band)settling_time=t(settled_indices(i));break;endendelsesettling_time=t(end);endend

5. 鲁棒性测试函数

functionrobustness_test()% 鲁棒性测试:参数变化对控制器性能的影响close all;% 测试不同的系统参数areas=[1.5,2.0,2.5];% 不同水箱截面积outflows=[0.08,0.1,0.12];% 不同出水流速controllers={'PID','Fuzzy'};figure('Position',[100,100,1200,800]);% 测试截面积变化的影响subplot(2,2,1);fori=1:length(areas)A=areas(i);[h,~,t]=water_tank_system('PID',1.0,50);plot(t,h,'LineWidth',2);hold on;endtitle('PID控制器 - 不同水箱截面积');xlabel('时间 (s)');ylabel('水位 (m)');legend(['A='num2str(areas(1))'m²'],['A='num2str(areas(2))'m²'],...['A='num2str(areas(3))'m²'],'Location','best');grid on;subplot(2,2,2);fori=1:length(areas)A=areas(i);[h,~,t]=water_tank_system('Fuzzy',1.0,50);plot(t,h,'LineWidth',2);hold on;endtitle('模糊控制器 - 不同水箱截面积');xlabel('时间 (s)');ylabel('水位 (m)');legend(['A='num2str(areas(1))'m²'],['A='num2str(areas(2))'m²'],...['A='num2str(areas(3))'m²'],'Location','best');grid on;% 测试出水流速变化的影响subplot(2,2,3);fori=1:length(outflows)Q_out=outflows(i);[h,~,t]=water_tank_system('PID',1.0,50);plot(t,h,'LineWidth',2);hold on;endtitle('PID控制器 - 不同出水流速');xlabel('时间 (s)');ylabel('水位 (m)');legend(['Q_{out}='num2str(outflows(1))],['Q_{out}='num2str(outflows(2))],...['Q_{out}='num2str(outflows(3))],'Location','best');grid on;subplot(2,2,4);fori=1:length(outflows)Q_out=outflows(i);[h,~,t]=water_tank_system('Fuzzy',1.0,50);plot(t,h,'LineWidth',2);hold on;endtitle('模糊控制器 - 不同出水流速');xlabel('时间 (s)');ylabel('水位 (m)');legend(['Q_{out}='num2str(outflows(1))],['Q_{out}='num2str(outflows(2))],...['Q_{out}='num2str(outflows(3))],'Location','best');grid on;sgtitle('控制系统鲁棒性测试','FontSize',14,'FontWeight','bold');end

使用方法

  1. 运行主比较程序
water_tank_control_comparison();
  1. 测试鲁棒性
robustness_test();
  1. 单独测试特定控制器
[h,Q_in,t]=water_tank_system('PID',1.0,100);figure;subplot(2,1,1);plot(t,h);title('水位响应');subplot(2,1,2);plot(t,Q_in);title('控制输入');

参考代码 matlab实现水箱水位控制系统www.3dddown.com/csa/79942.html

性能分析要点

控制器类型优点缺点适用场景
PID简单可靠,参数整定明确对非线性系统适应性差线性系统,参数变化小的场合
模糊控制适应非线性,鲁棒性强设计复杂,缺乏系统方法复杂非线性系统
LQR最优控制,理论完整需要精确模型模型精确已知的线性系统
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/6 20:16:38

华为OD机试真题: 喊7的次数重排

华为OD机试真题: 喊7的次数重排 介绍 "喊7的次数重排"是一个常见的面试题目&#xff0c;通常用于考察候选人的编程能力和逻辑思维。这道题目的背景是一个经典的游戏&#xff1a;从1开始依次报数&#xff0c;但遇到包含数字7或是7的倍数时&#xff0c;需要喊“过”。…

作者头像 李华
网站建设 2026/8/6 20:59:44

网站风险词内容防控对网站运营有哪些作用和意义

网站风险词&#xff08;包括政治敏感词、违禁词、恶意推广词等&#xff09;的内容防控&#xff0c;对于网站运营而言&#xff0c;不仅是“排雷”&#xff0c;更是保障网站生存与发展的“生命线”。它从合规、安全、品牌、效率四个维度&#xff0c;对网站运营产生深远的影响。以…

作者头像 李华
网站建设 2026/8/6 22:13:52

JavaScript 中的安全编码:10 个关键实践

JavaScript 作为现代 Web 开发的核心语言&#xff0c;几乎无处不在——从简单的前端交互到复杂的 Node.js 后端应用。然而&#xff0c;正是这种广泛的应用使 JavaScript 成为攻击者的主要目标。本文旨在为开发者提供 10 个关键的安全编码实践&#xff0c;帮助构建更安全的 Java…

作者头像 李华
网站建设 2026/8/6 20:59:49

GEO 搜索优化系统源码定制化:账号管理板块接入开发实战​

在本地生活服务、O2O 平台、企业选址分析等场景中&#xff0c;GEO 搜索优化系统的核心价值是 “精准定位 高效筛选”&#xff0c;但多数开源或通用系统的痛点的是&#xff1a;账号权限混乱、数据隔离性差、操作无追溯 —— 比如销售账号能查看全区域客户数据&#xff0c;运维误…

作者头像 李华
网站建设 2026/8/6 5:48:24

深度测评:2025主流十大企业网盘

2025年的企业网盘市场宛如一片充满机遇与挑战的浩瀚海洋&#xff0c;正经历着前所未有的深刻变革。既有老牌巨头的持续进化&#xff0c;也有国产专业力量的稳步崛起。本文聚焦十大主流企业网盘&#xff0c;通过深度测评为您理清选型思路。 一、企业网盘的核心价值&#xff1a;…

作者头像 李华
网站建设 2026/8/6 22:10:57

面试复习题--jetpack 的理解

Android 应用的特性(客户端架构、移动端场景、系统适配性等),梳理Android 架构稳定性 & 合理性的专项判定体系,覆盖「稳定性核心指标」「架构合理性设计原则」「适配性评估」三大维度,附量化标准和落地检查项,适配从单体 App 到模块化 / 组件化架构的全场景。 Andro…

作者头像 李华