Best way to solve my implicit equation
25 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:
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;
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]);
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.