フィルターのクリア

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?
  1 件のコメント
Dmitry
Dmitry 2023 年 10 月 12 日
q can be are complex

サインインしてコメントする。

採用された回答

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 Center および File ExchangeSymbolic Math Toolbox についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by