matlab app encapsulation ?

36 次查看(过去 30 天)
yan
yan 2024-11-8,6:36
评论: yan 2024-11-14,5:11
I have failed several times in encapsulation because the source code uses syms, but I don't know how to change it, or all the changes make me change too much, is there a good solution
syms pha H
term1 = 9 - 12 * (1 - P_ya .* F ./ (y .* H));
term2 = R .* sqrt(term1) - 3 .* R;
denominator = 2 .* H;
% pha
pha = atan(term2 ./ denominator);
%
% disp(pha);
pha_rad1=rad2deg(pha);
H_vals = linspace(50, 250, 50);
pha_vals = double(subs(pha_rad1, H, H_vals));
% Plot H versus pha
% figure(1);
plot(app.UIAxes,H_vals, pha_vals, 'b-', 'LineWidth', 2);
xlabel(app.UIAxes,'H');
ylabel(app.UIAxes,'pha');
title(app.UIAxes,'Plot of pha vs H');
% grid on;
% disp(pha_rad);
%
if K < 1
pha_rad = 45 - (P.*180) /(2.*pi);
elseif K < 2
pha_rad = 45;
else
pha_rad = 45 + (P.*180) /(2.*pi) ;
end
%
pha_rad = max(15, min(pha_rad, 65));
pha_rad = pha_rad * (pi / 180);
switch app.DropDown.Value
case '1'
if app.Button_2.Value==1
Vc2=(4.*pi.*R.^3)./3+pi.*R.^2*(L-2.*R);%
Ac2=pi.*R.^2+(L-2.*R).*2.*R;
Lc2=2*pi.*R+(L-2*R).*2;%
sigma2=Lc2.*H.*K.*y.*H./2;
Fbc2=Vc2.*10;%
Fbr2=Ac2.*10.*(H-L_s);
F2=Ac2.*P_ya;
Wr2=y.*H.*Ac2;%
t2=Lc2.*c.*H+sigma2.*tan(P)
eq=Wr2+t2==F.*(F2+Fbc2+Fbr2)
H_solution = double(solve(eq, H));
positiveValues = H_solution(H_solution > 0);
app.HEditField.Value=positiveValues;
app.Image.ImageSource = '1'; % 直接用路径
app.Image_2.ImageSource = '1'; % 直接
elseif app.Button.Value==1
Vc1=(4.*pi.*R.^3)./3+pi.*R.^2*(L-2.*R);;%
Ac1=pi.*R.^2;%
Lc1=2*pi.*R;%
sigma1=Lc1.*H.*K.*y.*H./2;%
Fbc1=Vc1.*10;%
Fbr1=Ac1.*10.*(H-L_s);%
F1=Ac1.*P_ya;%
Wr1=y.*H.*Ac1;%
t1=Lc1.*c.*H+sigma1.*tan(P)
eq=Wr1+t1==F.*(F1+Fbc1+Fbr1)
H_solution = double(solve(eq, H));
positiveValues = H_solution(H_solution > 0);
app.HEditField.Value=positiveValues;
app.Image.ImageSource = '2'; % 直接用路径
app.Image_2.ImageSource = '2'; % 直接用路径
end
case '3'
if app.Button_2.Value==1
Wr2=y.*pi.*(R.^2.*H+R.*H.^2.*tan(pha_rad)+(H.^3.*tan(pha_rad).^2)./3-2.*R.^3./3)
F2=pi.*R.^2.*P_ya
eq=Wr2==F.*F2
H_solution =vpasolve(eq, H);
positiveValues = H_solution(H_solution > 0);
app.HEditField.Value=double(positiveValues);
app.Image.ImageSource = '5'; % 直接用路径
app.Image_2.ImageSource = '5'; % 直接用路径
elseif app.Button.Value==1
Vc1=H.*(R.*2.*L+(H.*tan(pha_rad).*2+R.*2).*L+sqrt(R.*2.*L.*(H.*tan(pha_rad).*2+R.*2)*L))/3;
Ac1=L.*R.*2;%
F1=Ac1.*P_ya;%
Wr1=y.*Vc1;%
eq=Wr1==F.*F1
H_solution =vpasolve(eq, H);
positiveValues = H_solution(H_solution > 0);
app.HEditField.Value=double(positiveValues);
app.Image.ImageSource = '4'; % 直接用路径
app.Image_2.ImageSource = '4'; % 直接用路径
%%显示结果
end

回答(1 个)

Sameer
Sameer 2024-11-8,7:16
Hi @yan
To tackle the encapsulation issue, you can use "anonymous functions" or "numerical computations" instead of symbolic computations.
Here's how you can do it:
% Define constants and parameters
% Define a function for pha calculation
calculate_pha = @(H) atan((R .* sqrt(9 - 12 * (1 - P_ya .* F ./ (y .* H))) - 3 .* R) ./ (2 .* H));
% Calculate pha values over a range of H
H_vals = linspace(50, 250, 50);
pha_vals = arrayfun(@(H) rad2deg(calculate_pha(H)), H_vals);
% Plot H versus pha
plot(app.UIAxes, H_vals, pha_vals, 'b-', 'LineWidth', 2);
xlabel(app.UIAxes, 'H');
ylabel(app.UIAxes, 'pha');
title(app.UIAxes, 'Plot of pha vs H');
% Calculate pha_rad based on K
if K < 1
pha_rad = 45 - (P .* 180) / (2 .* pi);
elseif K < 2
pha_rad = 45;
else
pha_rad = 45 + (P .* 180) / (2 .* pi);
end
pha_rad = max(15, min(pha_rad, 65)) * (pi / 180);
switch app.DropDown.Value
case '1'
if app.Button_2.Value == 1
Vc2 = (4 .* pi .* R.^3) / 3 + pi .* R.^2 * (L - 2 .* R);
Ac2 = pi .* R.^2 + (L - 2 .* R) .* 2 .* R;
Lc2 = 2 * pi .* R + (L - 2 * R) .* 2;
sigma2 = Lc2 .* H_vals .* K .* y .* H_vals / 2;
Fbc2 = Vc2 .* 10;
Fbr2 = Ac2 .* 10 .* (H_vals - L_s);
F2 = Ac2 .* P_ya;
Wr2 = y .* H_vals .* Ac2;
t2 = Lc2 .* c .* H_vals + sigma2 .* tan(P);
eq = Wr2 + t2 == F .* (F2 + Fbc2 + Fbr2);
H_solution = H_vals(eq > 0);
positiveValues = H_solution(H_solution > 0);
app.HEditField.Value = positiveValues;
app.Image.ImageSource = '1';
app.Image_2.ImageSource = '1';
end
end
Hope this helps!
  3 个评论
Walter Roberson
Walter Roberson 2024-11-10,5:57
The code you posted has
H_vals = linspace(50, 250, 50);
If that range is too small, make the range larger.
yan
yan 2024-11-14,5:11
Incorrect use of Depth_CAESs/Button_3 Pushed Function or variable 'H' is not recognized.Because I used syms H to define the variables of H earlier, the subsequent encapsulation problems occurred.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Communications Toolbox 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by