Best way to solve my implicit equation

17 views (last 30 days)
I'm looking for a way to solve my implicit equation without using syms. The reason is that I want to compile an application with this equation and I can't do it because Matlab Compiler doesn't work with symbolic math toolbox.
This is my equation:
syms R
g = ((1+nu)*(1-2*nu)*(1-1/(R/b0)^2))/((alpha-1)-(1-2*nu)*(1+alpha)/(R/b0)^2);
omega = -(2*(1-2*nu)*(1-nu^2))/(delta*(alpha-1))*((alpha+1/beta)-(nu/(1-nu))*(1+alpha/beta))/((alpha-1)-((1-2*nu)*(1+alpha)/(R/b0)^2))/(R/b0)^2;
S1 = 0;
for n=0:1:10
if n==gam
S = k*omega^n/factorial(n)*log(R/a);
S = (omega^n/(factorial(n)*(n-gam))*((R/a)^(k*(n-gam))-1));
eqn = (nn/gam)*((1-g/delta)^((beta+1)/beta)-(a0/R)^((beta+1)/beta)) == S1;
sol_R = vpasolve(eqn, R, [a0 Rpl_max]);

Accepted Answer

Walter Roberson
Walter Roberson on 27 Mar 2020
In an interactive session:
syms any of the names corresponding to variables you will need to change. Calculate lhs(eqn)-rhs(eqn) and assign that to a variable.
Now call matlabFunction on the variable. Pass it the 'file' option and a file name. For example EQN.m . Pass it the 'vars' option using a cell array. In the first entry of the cell array put a vector of variable names of the variables you want solved for, in the order you want returned. The rest of the variables, the ones that will be provided as numeric parameters rather than solved for, you can either put into a vector in the second cell entry, or you can list them one at a time as cell entries.
That done, switch to the code you will be using for building the compiled application. In it, build an anonymous function that takes a single parameter, and calls EQN passing that parameter straight through, and then passing the numeric values in the same order as you specified in the 'vars' option.
Now you can pass that function handle to fsolve.
Notice that the symbolic work is done only at the interactive session, and is used to write an m file that does not use the symbolic toolbox. Notice that the code to be compiled does not use the symbolic toolbox: it only calls the m file and that file is purely numeric.
By the way, it is safer if your interactive session code initializes S1 to sym(0) instead of 0.
Simone Pavan
Simone Pavan on 20 Apr 2020
Edited: Simone Pavan on 20 Apr 2020
Sorry Walter,
fzero works only if my function changes sign.
If this doesn't happen, i.e. R is empty, how can I solve my equation?
Using original algorithm and vpasolve I can find my solution.

Sign in to comment.

More Answers (1)

Torsten on 27 Mar 2020
Use "fzero" or "fsolve".

Community Treasure Hunt

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

Start Hunting!

Translated by