# I dont understand, i try to use solve to find x in Matlab Function Block

1 view (last 30 days)
huy duc on 18 Jan 2022
Commented: Walter Roberson on 19 Jan 2022
function x = fcn(y)
sym a;
r1=7.3e-5;
r2=-1.1e-7;
s1=1.6e-1;
s2=1.38e-3;
s3=-1.6e-5;
t1=1.60e-2;
t2=-1.3;
t3=4.12e2;
A=0.25;
Tel=25;
sol =double(solve( 1.297 + (r1+r2)*Tel*a/A + (s1 + s2*Tel + s3*(Tel^2))*log(((t1 + t2/Tel + t3/(Tel^2))*a/A)+1)-y,a));
x = sol;

Walter Roberson on 18 Jan 2022
has shown you how to solve the equation using syms
However, syms requires the MATLAB Interpreter, and so is only available in a MATLAB Function block if you have rapid acceleration turned off completely (or sometimes on the next setting if you are careful); it cannot be used if you need code generation to C or C++ code or deployment.
If you need to generate code or you need deployment, then you need a different approach: you need to use fzero() or fsolve() to find the root of the expression, or you need to work with the formula to find the general solution.
MATLAB is not able to find the general formula, but using Maple, and letting the 1.297 be named C, then
r1=7.3e-5;
r2=-1.1e-7;
s1=1.6e-1;
s2=1.38e-3;
s3=-1.6e-5;
t1=1.60e-2;
t2=-1.3;
t3=4.12e2;
A=0.25;
Tel=25;
C = 1.297;
a = ((Tel^2*s3 + Tel*s2 + s1) * (Tel^2*t1 + Tel*t2 + t3) * ...
lambertw(Tel^3 * (r1+r2) * ...
exp((Tel^3 * (r1+r2) - t1 * (C-y) * Tel^2 - t2 * (C-y) * Tel - t3 * (C-y)) / ...
(Tel^2 * s3 + Tel * s2 + s1) / ...
(Tel^2 * t1 + Tel * t2 +t3)) / ...
(Tel^2 * s3 + Tel * s2 + s1) / ...
(Tel^2 * t1 + Tel * t2 + t3)) ...
- Tel^3 * (r1+r2)) ...
* A / Tel / (r1+r2) / (Tel^2 * t1 + Tel * t2 + t3)
Walter Roberson on 19 Jan 2022
syms x uel
f = uel - (3 + 3 * x + 5 * log(3 * x + 1))
f =
solve(f, x)
ans =
string(ans)
ans = "(5*wrightOmega(uel/5 - log(5) - 2/5))/3 - 1/3"
This tells you that there is a direct solution without using fsolve, using the wrightOmega function
However, wrightOmega is part of the Symbolic Toolbox, and so probably cannot have code generated for it.
You might, however, be able to take advantage of https://www.mathworks.com/matlabcentral/fileexchange/56407-wrightomegaq

_ on 18 Jan 2022
Edited: _ on 18 Jan 2022
Looks like you should use syms rather than sym
fcn(0)
ans = -0.4008
function x = fcn(y)
syms a;
r1=7.3e-5;
r2=-1.1e-7;
s1=1.6e-1;
s2=1.38e-3;
s3=-1.6e-5;
t1=1.60e-2;
t2=-1.3;
t3=4.12e2;
A=0.25;
Tel=25;
sol = double(solve( 1.297 + (r1+r2)*Tel*a/A + (s1 + s2*Tel + s3*(Tel^2))*log(((t1 + t2/Tel + t3/(Tel^2))*a/A)+1)-y,a));
x = sol;
end