필터 지우기
필터 지우기

How to get numerical equation for my code?

조회 수: 2 (최근 30일)
Dmitry
Dmitry 2023년 10월 12일
댓글: Dyuman Joshi 2023년 10월 12일
I have the next matlab's code:
function [Y1] = roots_2021a(q)
syms r
Y=airy(1,-r)-q*airy(-r);
x=0:0.1:20;
y=double(subs(Y,r,x));
d=diff(y>0);
k=num2cell(find(d~=0)+1);
X=double(cellfun(@(z)vpasolve(Y==0,r,x(z)),k));
Y1 = unique(X*exp(1j*pi/3));
end
in matlab code works perfectly, but when i compile it in standaolne app i have the next error:
"syms, subs, vpasolve" are excluded from packaging for the MATLAB Runtime environment according to the MATLAB Compiler license. Either remove the file or function from your code, or use the MATLAB function "isdeployed" to ensure the function is not invoked in the deployed component.
What I did: using matlabFunction, I obtained an expression for y, then substituting q into it, I obtained them numerically. After that I could find points on the X axis where y > 0. But then how to substitute all this into vpasolve and get an expression for it - I don't understand....
code for y expression:
syms r q
Y=airy(1,-r)-q*airy(-r);
x=0:0.1:20;
y=subs(Y,r,x);
f = matlabFunction(y)
but how to get the same for all 'roots_2021a' function?

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 10월 12일
Convert the function to use numeric functions and operations -
%Random input
in=rand;
%Output corresponding to the input
Out1 = modified(in);
Out2 = roots_2021a(in);
As we are dealing with floating point numbers, thus using a tolerance to compare
%Comparison
all(abs(real(Out1)-real(Out2))<1e-10)
ans = logical
1
all(abs(imag(Out1)-imag(Out2))<1e-10)
ans = logical
1
norm(Out1-Out2)
ans = 1.3154e-14
function Y1 = modified(q)
Y = @(r) airy(1,-r)-q*airy(-r);
x = 0:0.1:20;
y = Y(x);
d = diff(y>0);
k = find(d~=0)+1;
X = arrayfun(@(in) fzero(@(r) Y(r), in), x(k));
Y1 = unique(X*exp(1j*pi/3));
end
function [Y1] = roots_2021a(q)
syms r
Y=airy(1,-r)-q*airy(-r);
x=0:0.1:20;
y=double(subs(Y,r,x));
d=diff(y>0);
k=num2cell(find(d~=0)+1);
X=double(cellfun(@(z)vpasolve(Y==0,r,x(z)),k));
Y1 = unique(X*exp(1j*pi/3));
end
  댓글 수: 4
Dmitry
Dmitry 2023년 10월 12일
편집: Dmitry 2023년 10월 12일
changed fzero() to fsolve() and it's works!
Dyuman Joshi
Dyuman Joshi 2023년 10월 12일
Use fsolve instead of fzero

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 10월 12일
Nothing in the symbolic toolbox can be compiled or had code generated for it. Nothing .
You need to use an interactive session to build your formula and use matlabFunction to convert it to a numeric function handle. You can use the 'file' option to write the code as a .m file -- if you do then watch out for the default 'optimize' option being on when 'file' is used, as historically there have been serious bugs in the optimizer, so commonly you want to turn the optimizer off when you use 'file'
Then in the MATLAB code that is to be compiled or had code generated for it, you would call the function that was written to file.
You would not take any result from such a function and use it in vpasolve() -- at least not in the session that is to be compiled. You would instead convert the vpasolve() to fzero() or fsolve() calls for numeric use.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by