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

조회 수: 1 (최근 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일
opt = optimoptions('Algorithm', 'Levenberg-Marquardt');
iel = fsolve(f, uel, opt);
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