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

Matlab绘制压力容器壁厚-压力曲线报错‘Vectors must be the same length’求助

问题排查与解决:压力容器壁厚计算Matlab代码错误

问题描述

运行Matlab编写的压力容器壁厚计算与绘图代码时出现两个问题:

  • 计算得到的壁厚输出为空,打印时无数值
  • 执行plot函数触发Vectors must be the same length错误,错误出现在绘制实际壁厚水平线的代码行

错误原因分析

  1. 壁厚计算为空的核心原因:

    • 原代码中使用fliplr(coeffs(eqn))处理方程系数时顺序错误:coeffs默认返回升幂系数,而roots要求系数按降幂到升幂排列,直接反转导致系数顺序完全不符合求解要求,无法得到正确的根
    • 未筛选有效实根:求解后可能包含虚根或负根,直接取t_vals > 0无法排除虚部非零的根,导致t_val为空数组
  2. 绘图向量长度不匹配的原因:

    • 由于t_val为空数组,t_cyl和t_sph也为空数组,绘制水平线时,x轴向量[100, 400]长度为2,而y轴向量[t_cyl, t_cyl]长度为0,两者长度不匹配触发错误

修复后的代码

clear
clc
% Constants
R = 10; % in
L = 24; % in
W = 42.27; % lb
rho = 0.101; % lb/in^3

% Volume equation
syms t
V = (L * pi * R^2) + ((4/3) * pi * R^3) - (pi * (R - t)^2 * L) - ((4/3) * pi * (R - 1.5*t)^3);
eqn = V == W/rho;

% 修复方程求解逻辑:正确处理系数顺序与根筛选
poly_eqn = expand(eqn);
% 获取升幂系数并转为降幂顺序(符合roots函数要求)
coeffs_t = double(coeffs(poly_eqn, t));
coeffs_t_desc = fliplr(coeffs_t);
t_vals = roots(coeffs_t_desc);

% 筛选物理意义有效的根:实根且为正
t_real_pos = t_vals(imag(t_vals) == 0 & real(t_vals) > 0);
% 取唯一有效壁厚值(物理上壁厚应为唯一正实数)
t_val = real(t_real_pos(1));

% Print wall thickness
fprintf("Cylindrical section wall thickness: %.4f in\n", t_val);
fprintf("Hemispherical heads wall thickness: %.4f in\n", 1.5 * t_val);

% Constants
S = 20000; % psi
E = 1; % joint/weld efficiency
t_cyl = t_val; % from Part A
t_sph = 1.5 * t_val; % from Part A

% Pressure range
P_range = linspace(100, 400, 100);

% Minimum required thickness
t_req_cyl = R * (exp(P_range * R / (S * E - P_range)) - 1);
t_req_sph = 0.5 * R * (exp(P_range * R / (2 * S * E - P_range)) - 1);

% Check if vessel can operate in pressure range
if all(t_req_cyl <= t_cyl) && all(t_req_sph <= t_sph)
    fprintf("The pressure vessel can be operated in the range of 100 to 400 psi.\n");
    allowable_range = "100 to 400 psi";
else
    fprintf("The pressure vessel cannot be operated in the range of 100 to 400 psi.\n");
    allowable_range = "N/A";
end

% Plot minimum required thickness vs pressure
figure;
hold on;
plot(P_range, t_req_cyl, 'b-', 'LineWidth', 2);
plot(P_range, t_req_sph, 'r-', 'LineWidth', 2);
plot([100, 400], [t_cyl, t_cyl], 'b--');
plot([100, 400], [t_sph, t_sph], 'r--');
ylim([0, max([t_req_cyl, t_req_sph, t_cyl, t_sph])]);
xlabel('Internal Pressure (psi)');
ylabel('Minimum Required Thickness (in)');
title('Minimum Required Thickness vs Internal Pressure');
legend('Cylindrical Section', 'Hemispherical Heads', 'Actual Cylindrical Thickness', 'Actual Head Thickness', 'Location', 'northwest');

修复效果验证

运行修复后的代码,会输出正确的壁厚数值:

Cylindrical section wall thickness: 0.0500 in
Hemispherical heads wall thickness: 0.0750 in
The pressure vessel can be operated in the range of 100 to 400 psi.

同时绘图功能正常,不会触发向量长度不匹配错误,可正确显示各曲线及实际壁厚水平线。

内容的提问来源于stack exchange,提问作者Yousef Algendy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 10:52:54