How to effectively solve the problem of incompatibility of AppDesigner using symbol toolkit syms after packaging and publishing into exe and other executable files?

10 Ansichten (letzte 30 Tage)
How to effectively solve the problem of incompatibility of AppDesigner using symbol toolkit syms after packaging and publishing into exe and other executable files? And how can I change it in my code
cla(app.UIAxes)
P=app.PEditField.Value
P=P.*pi/180;
c=app.CKPaEditField.Value;
K=app.KEditField.Value;%侧压力系数
P_ya=app.P_yaEditField.Value;
F=app.FEditField.Value;
y=app.yKNm3EditField.Value;
R = app.RmEditField.Value;
L=app.LmEditField.Value;
L_s=app.L_smEditField.Value;
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_rad=rad2deg(pha);
H_vals = linspace(50, 250, 50);
pha_vals = double(subs(pha_rad, 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 / 2;
elseif K <= 2
pha_rad = 45;
else
pha_rad = 45 + P / 2;
end
% 限制 pha_rad 的值在 15° 和 65° 之间
pha_rad = max(15, min(pha_rad, 65));
pha_rad = pha_rad * (pi / 180);
switch app.DropDown.Value
case '竖直滑面法'
if app.Button_2.Value==1
Vc2=4.*pi.*R.^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 = '竖直滑面法示意图.png'; % 直接用路径
app.Image_2.ImageSource = '竖直滑面法示意图2.png'; % 直接用路径
elseif app.Button.Value==1
Vc1=4.*pi.*R.^3;%体积
Ac1=pi.*R.^2;%表面积
Lc1=2*pi.*R;%周长
sigma1=Lc1.*H.*K.*y.*H./2;%水平应力和∑σh
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 = '竖直滑面法示意图.png'; % 直接用路径
app.Image_2.ImageSource = '竖直滑面法示意图2.png'; % 直接用路径
end

Antworten (2)

Ronit
Ronit am 26 Sep. 2024
Hello @Nicholas
To address the incompatibility issues of using the "syms" function from the Symbolic Math Toolbox in MATLAB App Designer when packaging and publishing your application into an executable, I would suggest replacing symbolic calculations with numeric approximations and use Numerical Solvers or custom functions to solve equations.
  • Numerical Computation for "pha": Instead of using symbolic variables, calculate "pha" for each value of "H" using a "for loop" and store the results in "pha_vals".
H_vals = linspace(50, 250, 50);
pha_vals = zeros(size(H_vals));
% Compute pha for each H value numerically
for i = 1:length(H_vals)
H = H_vals(i);
term1 = 9 - 12 * (1 - P_ya * F / (y * H));
term2 = R * sqrt(term1) - 3 * R;
denominator = 2 * H;
pha_rad = atan(term2 / denominator);
pha_vals(i) = rad2deg(pha_rad);
end
  • Numerical Solver for "H": The "fzero" function is used to find the root of the equation numerically.
  • The equation to solve is an anonymous function "eq", which allows "fzero" to evaluate it for different values of "H".
% Define the equation as a function
eq = @(H) Wr2 + t2 - F * (F2 + Fbc2 + Fbr2);
% Solve the equation numerically
H_solution = fzero(eq, H_vals(1));
app.HEditField.Value = H_solution;
Please refer to the documentation of "fzero" for more details:
I hope it resolves your query!
  2 Kommentare
Nicholas
Nicholas am 26 Sep. 2024
It's not like that, my H is unknown, but your code gives the interval of H, right? That doesn't give us an exact H,Define an eq=@ (H), but I don't think that solves the problem of writing an app.thanks!

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 27 Sep. 2024
Break your code up into pieces.
In the first piece, write your symbolic calculations in terms of constants and symbolic variables (representing your inputs). If your code has branches based upon decisions about values calculated from the inputs, then write the forms using piecewise(). At the end, you arive at a symbolic expression (potentially with piecewise) of how inputs are translated to outputs.
Now, follow that block of code by using matlabFunction() with 'file' output. It is safer to turn 'optimize', off (historically optimization was broken.)
Execute the above first portion, getting out a .m file written in numeric processing form.
Now write your second part of the code, the app designer version. At the place in the code where you would have had the symbolic calculations, place a call to the generated .m from the first step.
You will now be able to do code generation on the second chunk of code, which will no longer have any symbolic work -- the symbolic work having been converted into a call to a .m file, where the generated .m file is the output of the matlabFunction() on the first part.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by