# Solve equation without Symbolic Math Toolbox for Compiler

9 Ansichten (letzte 30 Tage)
Simone Pavan am 20 Apr. 2020
Beantwortet: Ameer Hamza am 20 Apr. 2020
I have a very long implicit equation. Actually I solve this equation using 'syms' and 'vpasolve'.
syms R % R is the only unknown variable, all others are known
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; % sommatoria manuale
for n=0:1:10
if n==gam
S = k*omega^n/factorial(n)*log(R/a);
else
S = (omega^n/(factorial(n)*(n-gam))*((R/a)^(k*(n-gam))-1));
end
S1=S1+S;
end
eqn = (nn/gam)*((1-g/delta)^((beta+1)/beta)-(a0/R)^((beta+1)/beta)) == S1;
sol_R = vpasolve(eqn, R, [a0 Rpl_max]); % solution
The problem is that I want to compile this equation in a standalone application but Matlab Compiler does not support Symbolic Math Toolbox.
First I tried to convert the symbolic expression into a anonymous funcion, as follow.
syms a a0 alpha0 b0 beta0 delta0 gam k nn nu0
syms R
g = ((1+nu0)*(1-2*nu0)*(1-1/(R/b0)^2))/((alpha0-1)-(1-2*nu0)*(1+alpha0)/(R/b0)^2);
omega = -(2*(1-2*nu0)*(1-nu0^2))/(delta0*(alpha0-1))*((alpha0+1/beta0)-(nu0/(1-nu0))*(1+alpha0/beta0))/((alpha0-1)-((1-2*nu0)*(1+alpha0)/(R/b0)^2))/(R/b0)^2;
S1 = sym(0);
for n=0:1:10
%piecewise is needed to test against a value to be determined later
S = piecewise(n == gam, ...
k*omega^n/factorial(n)*log(R/a), ...
(omega^n/(factorial(n)*(n-gam))*((R/a)^(k*(n-gam))-1)) );
S1=S1+S;
end
EQN = (nn/gam)*((1-g/delta0)^((beta0+1)/beta0)-(a0/R)^((beta0+1)/beta0)) - S1;
matlabFunction(EQN, 'file', 'EQN.m', 'vars', {R, [a, a0, alpha0, b0, beta0, delta0, gam, k, nn, nu0]})
After this, I tried to solve it in different ways:
1st way) I can't use vpasolve for function_handle.
2nd way) Using fzero instead of vpasolve I can find a solution, but only if function changes sign. Therefor sometimes I get errors.
parameters = [a, a0, alpha, b0, beta, delta, gam, k, nn, nu];
obj = @(R) EQN(R, parameters);
sol_R = fzero(obj, [a0 Rpl_max]);
3rd way) Using fsolve I got same error of vpasolve.
Is there a way to reach my goal?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Ameer Hamza am 20 Apr. 2020
I guess you are trying to run these lines
parameters = [a, a0, alpha, b0, beta, delta, gam, k, nn, nu];
obj = @(R) EQN(R, parameters);
sol_R = fzero(obj, [a0 Rpl_max]);
without clearing the variable EQN from your workspace. When MATLAB creates the anonymous function obj, it picks EQN from the workspace, which is a symbolic variable, and hence you get such error. Try to run the above lines after cleaning the variable EQN from workspace
clear EQN
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Calculus finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by