matlab 我在尝试绘制图形时遇到错误,但我不确定代码中的问题是什么,错误是矢量必须具有相同的长度

nbysray5  于 2023-03-03  发布在  Matlab
关注(0)|答案(1)|浏览(216)
clear                                                                     %3015
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;

% Solve for t
t_vals = roots(double(fliplr(coeffs(eqn))));
t_val = t_vals(t_vals > 0); % pick positive root

% 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
R = 10; % in
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');
....

我试图绘制壁厚与内部压力的关系图,但收到以下输出和错误:圆柱段壁厚:in\n半球形封头壁厚:in\n压力容器可以在100到400 psi的范围内操作。\n错误使用绘图矢量必须具有相同的长度。
P2(第52行)图中的错误([100,400],[t_cyl,t_cyl],'B--');

6bc51xsx

6bc51xsx1#

1.-错误来自空的t_val

t_val为空,因为您调用fsolve的方式不返回任何根。
因此t_val为空,因为t_vals也为空。
修改的问题行:

close all;clear all;clc

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

% Volume equation

t=[0:.01:1]*R;
V = (L * pi * R^2) + ((4/3) * pi * R^3) - (pi * (R - t).^2 * L) - ((4/3) * pi * (R - 1.5*t).^3);

figure
plot(t,V-W/rho,[t(1) t(end)],[0 0])
grid on
xlabel('t [0 R]');ylabel('Volume')
hold on

fun1=@(x) (L * pi * R^2) + ((4/3) * pi * R^3) - (pi * (R - x).^2 * L) - ((4/3) * pi * (R - 1.5*x).^3)-W/rho;
x0=0
xn=fsolve(fun1,x0)

% xn =
%    0.124990373486403

plot(xn,0,'b*')
axis([0 .5 -1e3 1e3])

对于这个特定的函数**,将有效根约束在范围**[0 R]内,则只有正(或空)根。

t_val=xn;

2.-下面是您的其余代码:

% 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
R = 10; % in
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');

3.-现在,您可以从这里开始添加所需的任何其他图形。

相关问题