Solve system of equations without Symbolic Math Toolbox for Compiler

7 次查看(过去 30 天)
I have a system of 3 equations with 3 unknowns (X_sym, AbS_sym, AbB_sym) in my code and I want to solve it for different combinations of variables stored in vlist.
At the moment, I am using 'syms' and 'vpasolve' and it works fine:
syms AbB_sym AbS_sym X_sym
symvar_AbB_sym = parallel.pool.Constant(AbB_sym);
symvar_AbS_sym = parallel.pool.Constant(AbS_sym);
symvar_X_sym = parallel.pool.Constant(X_sym);
parfor i = 1:size(vlist,1)
X_sym = symvar_X_sym.Value;
AbS_sym = symvar_AbS_sym.Value;
AbB_sym = symvar_AbB_sym.Value;
eqns = [(X_sym*AbB_sym*AbS_sym)/(vlist(i,4)*vlist(i,5)) + 2*(X_sym*AbS_sym^2)/vlist(i,5)^2 + ...
(X_sym*AbS_sym)/vlist(i,5) + AbS_sym == vlist(i,1),...
(X_sym*AbB_sym*AbS_sym)/(vlist(i,4)*vlist(i,5)) + 2*(X_sym*AbB_sym^2)/vlist(i,4)^2 + ...
(X_sym*AbB_sym)/vlist(i,4) + AbB_sym == vlist(i,2),...
(X_sym*AbB_sym*AbS_sym)/(vlist(i,4)*vlist(i,5)) + (X_sym*AbB_sym^2)/vlist(i,4)^2 + ...
(X_sym*AbS_sym^2)/vlist(i,5)^2 + ...
(X_sym*AbB_sym)/vlist(i,4) + (X_sym*AbS_sym)/vlist(i,5) + X_sym == vlist(i,3)];
S = vpasolve(eqns,[X_sym AbB_sym AbS_sym],[0 Inf; 0 Inf;0 Inf]);
X(i,1) = S.X_sym;
AbB(i,1) = S.AbB_sym;
AbS(i,1) = S.AbS_sym;
end
However, I cannot use this approach, as I cannot compile my app into a standalone app with syms and vpasolve.
I tried looking into 'matlabfunction' according to this article:
But I don't think it is applicable here since I have a system of equations. Am I right?
How can I modify the code so it could be compiled into a standalone app?
Any help is appreciated! Thanks!

采纳的回答

Torsten
Torsten 2022-5-12
Did you try "solve" on your system of equations with the numerical vlist coeffcients replaced also by symbolic variables ?
syms AbB_sym AbS_sym X_sym vlist1 vlist2 vlist3 vlist4 vlist5
eqns = [(X_sym*AbB_sym*AbS_sym)/(vlist4*vlist5) + 2*(X_sym*AbS_sym^2)/vlist5^2 + ...
(X_sym*AbS_sym)/vlist5 + AbS_sym == vlist1,...
(X_sym*AbB_sym*AbS_sym)/(vlist4*vlist5) + 2*(X_sym*AbB_sym^2)/vlist4^2 + ...
(X_sym*AbB_sym)/vlist4 + AbB_sym == vlist2,...
(X_sym*AbB_sym*AbS_sym)/(vlist4*vlist5) + (X_sym*AbB_sym^2)/vlist4^2 + ...
(X_sym*AbS_sym^2)/vlist5^2 + ...
(X_sym*AbB_sym)/vlist4 + (X_sym*AbS_sym)/vlist5 + X_sym == vlist3];
S = solve(eqns,[X_sym AbB_sym AbS_sym])
If this does not work, you will have to use the numerical solver "fsolve" for your system of equations.
  1 个评论
Daniel Wirth
Daniel Wirth 2022-5-13
Thanks for your answer!
Yes, I did try solving the equations with 'solve' first, but that didn't work...
I ended up going with your suggestion of using fsolve and it worked like a charm! Thanks a lot!!

请先登录,再进行评论。

更多回答(1 个)

Song-Hyun Ji
Song-Hyun Ji 2023-6-14
You can get the solution in the following answers page.
- How to deploy when using 'syms' and 'solve' with function input arguments to consist the equation in MATLAB Compiler

类别

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

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by