You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Octave明渠水力计算器脚本问题:流量未在消息框显示

修复Octave明渠水力计算器的流量显示问题

运行以下Octave脚本时,程序仅在消息框中显示流速(velocity),无法按预期同时显示流量(flow_rate),且英制单位分支存在语法和内容错误:

total=0
choice=menu(' Welcome to Group 8 Open Channel Hydraulic Calculator click continue to start',' continue');
a=menu(' Please choose unit system', 'SI Units','U.S. Customary Units');
if a==1
  choice= menu ('Please choose shape of the channel', 'Rectangle', 'Triangle','Trapezoid');
  if (choice==1)
    prompt= {'channel top width,b (meter)',' Channel depth,y(meter)','Channel height,h(meter)','Channel length,L(meter)', 'Mannings coefficient,n'};
    dlg_title='Please insert channel dimension'
    num_lines=1;
    answer= inputdlg(prompt, dlg_title,num_lines);
     top_width=str2double(answer{1});
    channel_depth= str2double(answer{2});
    channel_height=str2double(answer{3});
    channel_length=str2double(answer{4});
    n=str2double(answer{5});
    Area= top_width*channel_depth;
    Wetted_Perimeter= top_width+2*channel_depth;
    Hydraulic_Radius= Area/Wetted_Perimeter;
    K=1.00;
    Slope= channel_height/channel_length;
    b=menu('Please choose formula', 'Chezy Formula', 'Mannings Formula');
    if(b==1)
    velocity= (1/n)*Hydraulic_Radius^(1/6)*sqrt(Hydraulic_Radius*Slope);
    elseif (b==2)
    velocity=(K/n)*(Hydraulic_Radius)^(2/3)*sqrt(Slope);
    endif
  flow_rate= velocity*Area;


  elseif (choice==2)
    prompt= {'pitch,z (meter)',' Channel depth,y(meter)','Channel height,h(meter)','Channel length,L(meter)', 'Mannings coefficient'};
    dlg_title='Please insert channel dimension'
    num_lines=1;
    answer= inputdlg(prompt, dlg_title,num_lines);
     pitch=str2double(answer{1});
    channel_depth= str2double(answer{2});
    channel_height=str2double(answer{3});
    channel_length=str2double(answer{4});
    n=str2double(answer{5});
    Area= pitch*(channel_depth^2);
    Wetted_Perimeter= 2*channel_depth*((1+pitch^2)^(1/2));
    Hydraulic_Radius= Area/Wetted_Perimeter;
    K=1.00;
    Slope= channel_height/channel_length;
    b=menu('Please choose formula', 'Chezy Formula', 'Mannings Formula');
    if(b==1)
    velocity= (1/n)*Hydraulic_Radius^(1/6)*sqrt(Hydraulic_Radius*Slope);
    elseif (b==2)
    velocity=(K/n)*(Hydraulic_Radius)^(2/3)*sqrt(Slope);
  endif
  flow_rate= velocity*Area;

  elseif(choice==3)
  prompt= {'pitch,z (meter)','Bottom width,b(meter)', 'Channel depth,y(meter)','Channel height,h(meter)','Channel length,L(meter)', 'Mannings coefficient'};
    dlg_title='Please insert channel dimension'
    num_lines=1;
    answer= inputdlg(prompt, dlg_title,num_lines);
     pitch=str2double(answer{1});
     bottom_width=str2double(answer{2});
    channel_depth= str2double(answer{3});
    channel_height=str2double(answer{4});
    channel_length=str2double(answer{5});
    n=str2double(answer{6});
    Area= (bottom_width+pitch*(channel_depth))*channel_depth;
    Wetted_Perimeter= bottom_width+2*channel_depth*((1+pitch^2)^(1/2));
    Hydraulic_Radius= Area/Wetted_Perimeter;
    K=1.00;
    Slope= channel_height/channel_length;
    b=menu('Please choose formula', 'Chezy Formula', 'Mannings Formula');
    if(b==1)
    velocity= (1/n)*sqrt(Hydraulic_Radius*Slope);
    elseif (b==2)
    velocity=(K/n)*(Hydraulic_Radius)^(2/3)*sqrt(Slope);
  endif
  flow_rate= velocity*Area;
  endif
else a==2
  choice= menu ('Please choose shape of the channel', 'Rectangle', 'Triangle','Trapezoid');
  if (choice==1)
    prompt= {'channel top width,b (ft)',' Channel depth,y(ft)','Channel height,h(ft)','Channel length,L(ft)', 'Mannings coefficient,n'};
    dlg_title='Please insert channel dimension'
    num_lines=1;
    answer= inputdlg(prompt, dlg_title,num_lines);
     top_width=str2double(answer{1});
    channel_depth= str2double(answer{2});
    channel_height=str2double(answer{3});
    channel_length=str2double(answer{4});
    n=str2double(answer{5});
    Area= top_width*channel_depth;
    Wetted_Perimeter= top_width+2*channel_depth;
    Hydraulic_Radius= Area/Wetted_Perimeter;
    K=1.49;
    Slope= channel_height/channel_length;
    b=menu('Please choose formula', 'Chezy Formula', 'Mannings Formula');
    if(b==1)
    velocity= (1/n)*Hydraulic_Radius^(1/6)*sqrt(Hydraulic_Radius*Slope);
    elseif (b==2)
    velocity=(K/n)*(Hydraulic_Radius)^(2/3)*sqrt(Slope);
    endif
  flow_rate= velocity*Area;


  elseif (choice==2)
    prompt= {'pitch,z (meter)',' Channel depth,y(meter)','Channel height,h(meter)','Channel length,L(meter)', 'Mannings coefficient'};
    dlg_title='Please insert channel dimension'
    num_lines=1;
    answer= inputdlg(prompt, dlg_title,num_lines);
     pitch=str2double(answer{1});
    channel_depth= str2double(answer{2});
    channel_height=str2double(answer{3});
    channel_length=str2double(answer{4});
    n=str2double(answer{5});
    Area= pitch*(channel_depth^2);
    Wetted_Perimeter= 2*channel_depth*((1+pitch^2)^(1/2));
    Hydraulic_Radius= Area/Wetted_Perimeter;
    K=1.49;
    Slope= channel_height/channel_length;
    b=menu('Please choose formula', 'Chezy Formula', 'Mannings Formula');
    if(b==1)
    velocity= (1/n)*Hydraulic_Radius^(1/6)*sqrt(Hydraulic_Radius*Slope);
    elseif (b==2)
    velocity=(K/n)*(Hydraulic_Radius)^(2/3)*sqrt(Slope);
  endif
  flow_rate= velocity*Area;

  elseif(choice==3)
  prompt= {'pitch,z (meter)','Bottom width,b(meter)', 'Channel depth,y(meter)','Channel height,h(meter)','Channel length,L(meter)', 'Mannings coefficient'};
    dlg_title='Please insert channel dimension'
    num_lines=1;
    answer= inputdlg(prompt, dlg_title,num_lines);
     pitch=str2double(answer{1});
     bottom_width=str2double(answer{2});
    channel_depth= str2double(answer{3});
    channel_height=str2double(answer{4});
    channel_length=str2double(answer{5});
    n=str2double(answer{6});
    Area= (bottom_width+pitch*(channel_depth))*channel_depth;
    Wetted_Perimeter= bottom_width+2*channel_depth*((1+pitch^2)^(1/2));
    Hydraulic_Radius= Area/Wetted_Perimeter;
    K=1.49;
    Slope= channel_height/channel_length;
    b=menu('Please choose formula', 'Chezy Formula', 'Mannings Formula');
    if(b==1)
    velocity= (1/n)*sqrt(Hydraulic_Radius*Slope);
    elseif (b==2)
    velocity=(K/n)*(Hydraulic_Radius)^(2/3)*sqrt(Slope);
  endif
  flow_rate= velocity*Area;
endif
end
i = msgbox(['velocity =' num2str(velocity)],['flow rate=' num2str(flow_rate)]);

问题定位与修复步骤

1. 修复消息框显示逻辑(核心问题)

原代码中msgbox的第二个参数是窗口标题,不是显示内容,导致流量只出现在窗口标题栏,而消息框正文只有流速。需要将流速和流量合并为同一个字符串作为msgbox的第一个参数:

i = msgbox(['velocity = ' num2str(velocity) '\nflow rate = ' num2str(flow_rate)], 'Calculation Results');

用\n换行分隔两个参数,第二个参数设为清晰的窗口标题。

2. 修复英制单位分支的语法错误

原代码中else a==2是无效语法,Octave中else不能直接跟条件判断,需改为elseif a==2(适配未来扩展更多单位系统的场景):

% 替换原代码中的 else a==2
elseif a==2

3. 修正英制单位下的输入框单位显示错误

英制单位分支的三角形、梯形输入框仍标注(meter),需统一改为(ft):

  • 三角形prompt修改:
prompt= {'pitch,z (ft)',' Channel depth,y(ft)','Channel height,h(ft)','Channel length,L(ft)', 'Mannings coefficient'};
  • 梯形prompt修改:
prompt= {'pitch,z (ft)','Bottom width,b(ft)', 'Channel depth,y(ft)','Channel height,h(ft)','Channel length,L(ft)', 'Mannings coefficient'};

4. 验证flow_rate的计算位置

原代码中flow_rate= velocity*Area;的位置是正确的,位于每个渠道形状分支的公式选择endif之后,确保velocity计算完成后再计算流量,无需调整位置。

额外优化建议

  • 为变量添加初始化:在代码开头添加velocity=0; flow_rate=0;,避免极端情况下变量未定义报错。
  • 统一代码缩进:规范缩进可以提升代码可读性,避免逻辑嵌套错误。

内容的提问来源于stack exchange,提问作者Halim Jamil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 19:05:20