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

2 次查看(过去 30 天)
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
Walter Roberson 2022-1-18
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)
  3 个评论
Walter Roberson
Walter Roberson 2022-1-19
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

请先登录,再进行评论。

更多回答(1 个)

Voss
Voss 2022-1-18
编辑:Voss 2022-1-18
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

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by