Solve system of equations without Symbolic Math Toolbox for Compiler

10 views (last 30 days)
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!

Accepted Answer

Torsten
Torsten on 12 May 2022
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 Comment
Daniel Wirth
Daniel Wirth on 13 May 2022
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!!

Sign in to comment.

More Answers (1)

Song-Hyun Ji
Song-Hyun Ji on 14 Jun 2023
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

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!